/********************************* * masterpic.c * * Walid Mnif, Tamer Shadid, Lim Seang * ECE4006 Summer 2003 iBlueBot ******************************** */ #include /******************************* * PIC Configuration Settings ******************************* */ __CONFIG(1,HS); // HS oscillator __CONFIG(2,PWRTEN & BOREN & BORV42 & WDTDIS); // Resets & disable watchdog __CONFIG(3,CCP2RC1); // CCP2 to RC1 (rather than to RB3) __CONFIG(4,LVPDIS & DEBUGEN); // RB5 enabled for I/O void TransferToPic(void); void SendByte(char); void SendByte2(); void TransferSPI(char); void Initial(); void BlinkAlive(void); void LoopTime(void); void char2b(char b); char ALIVECNT; // Counter for blinking "Alive" LED char frompda; char frompic='0'; char topic='x'; int counter=200; bit interrupted; int RA1LED = 0; int RA2LED = 0; int RA3LED = 0; void main() { Initial(); SendByte(frompic); while(1) { if(!(--RA2LED)){ RA2 = 0; } if(!(--RA1LED)){ RA1= 0; } if(!(--RA3LED)){ RA3 = 0; } if(counter==0) { counter =100; TransferSPI(0b00000011); if(frompic != 0b00000011){ frompic = frompic+0x01; SendByte(frompic); //char2b(frompic); RA3 = 1; RA3LED = 10; } } counter--; //interrupted = 0; //}//end if interrupted LoopTime(); BlinkAlive(); } }//end main void Initial() { ADCON1 = 0b10001110; // Enable PORTA & PORTE as digital I/O pins TRISA = 0b11100001; // Set I/O for PORTA TRISB = 0b11001100; // Set I/O for PORTB TRISC = 0b10010010; // Set I/O for PORTC TRISD = 0b00001111; // Set I/O for PORTD TRISE = 0b00000100; // Set I/O for PORTE PR2 = 222; // Set B=223 for Timer2 scalar interrupted=0; T2CON = 0b00110111; // Set A=16, C=7 for Timer 2 scalar IPEN=1; TMR2IP=0; ALIVECNT = 250; ///UART initalization SPBRG=32; TXSTA=0b00100100; RCSTA=0b10010000; TXIE=0; RCIF=0; RCIE=1; RA1 = 0; //test led's RA2 = 0; TXIP=0; RCIP=1; ////SPI settings SSPCON1=0b00100000; SSPSTAT=0b01000000; RB5=1; SSPIF=0; GIEH=1; GIEL=1; } /*******************************************8 * SendByte(char); * sends the inputed char through uart; */ void SendByte(char tosend) { TXREG=tosend; while(TXIF==0) { } TXREG=0; } void SendByte2() { TXREG=SSPBUF; while(TXIF==0) { } TXREG=0; } /**************************************** * TransferSPI(char); * will transfer topic to pic 2 and get byte from it */ void TransferSPI(char tosend) { RA1 = 1; RA1LED=10; RB5=0; SSPBUF=tosend; SSPIF=0; //SendByte('m'); while(SSPIF==0); { } frompic=SSPBUF; // SendByte2(); // SendByte('k'); RB5=1; } /******************************** * LoopTime() * * This subroutine waits for the TMR2IF flag to indicate that 10ms have passed * since the last time this occurred. ******************************** */ void LoopTime() { while (!TMR2IF) { } // Wait until TMR2IF flag is set TMR2IF = 0; // Clear flag } /******************************* * BlinkAlive() * * This subroutine briefly blinks the LED next to the PIC every * two-and-a-half * seconds. ******************************* */ void BlinkAlive() { RA4 = 1; // Turn off LED if (!(--ALIVECNT)) // Decrement loop counter, return if not 0 { ALIVECNT = 250; // Reinitialize ALIVECNT RA4 = 0; // Turn on LED for ten ms every 2.5 sec } } /*******************************************8 * high priority interrupt set when receiving something from pda */ void interrupt HiPriISR() { interrupted =1; topic=RCREG; RA2=1; RA2LED=20; LoopTime(); TransferToPic(); //RA2=0; //////////////////////////////// RCREG=0; /*RB5=0; SSPBUF=topic; SSPIF=0; while(SSPIF==0) { } LoopTime(); frompic=SSPBUF; RB5=1;*/ topic=0; } void TransferToPic() { RB5=0; SSPIF=0; SSPBUF=topic; //RA1=1; while(SSPIF==0) { } //RA1=0; //frompic=SSPBUF; RB5=1; } void interrupt low_priority LoPriISR() // Low-priority ISR { } void char2b(char b) { int i; //SendByte(''); for( i=7; i>=0;i--) { //SendByte('t'); if(b & 0x1) { //num[i]=1; SendByte('1'); // AfxMessageBox(_T("1")); } else { //num[i]=0; SendByte('0'); // AfxMessageBox(_T("0")); } b>>=1; } SendByte('\n'); }