//RF RX Code // //#case #include <16C84.H> #include // Configure PIC to use: HS clock, no Watchdog Timer, // no code protection, enable Power Up Timer // #fuses HS,NOWDT,NOPROTECT,PUT // tell compiler clock is 4MHz. This is required for DELAY_MS() // and for serial I/O, all of which use software delay loops. // #use DELAY(clock=4000000) // declare that we'll manually establish the data direction of // each I/O pin on port A & B. // #use fast_io(A) #use fast_io(B) // Standard definitions for the irx2_1 board // #define RS232_XMIT PIN_B1 // (output) RS232 serial transmit #define RS232_RCV PIN_B5 // (input) RS232 serial receive #define RED_LED PIN_B2 // (output) Red LED (low true) #define IR_LED PIN_B3 // (output) Infrared LED (low true) #define IR_SENSOR PIN_B4 // (input) IR sensor (Sharp IS1U30) #define RF_RCV PIN_A2 // (input) RF input // Macros to simplify I/O operations // #define RED_LED_ON output_low(RED_LED) #define RED_LED_OFF output_high(RED_LED) #define IR_LED_ON output_low(IR_LED) #define IR_LED_OFF output_high(IR_LED) #define RS_232_ON output_high(RS232_XMIT) #define RS_232_OFF output_low(RS232_XMIT) // Default tri-state port direction bits: all PORT A bits are // output except bit 2, all bits on PORT B are output except // for IR_SENSOR (bit 4) and RC232_RCV (bit 5). // #define IRX_A_TRIS 0b00000100 #define IRX_B_TRIS 0b00110000 // Array to store current RF Data. char RF_Data[6]; // Inform printf() and friends of the desired baud rate // and which pins to use for RF I/O. // #use rs232(baud=1200, xmit=PIN_A0, rcv=RF_RCV, INVERT) char get_RF() { return (getc()); } // Inform printf() and friends of the desired baud rate // and which pins to use for serial I/O. // #use rs232(baud=9600, xmit=RS232_XMIT, rcv=RS232_RCV) void put_RS232(char c) { putc(c); } char get_RS232() { return(getc()); } boolean RF_RX() { char ch; char data[6]; int i, j; ch = get_RF(); while(ch != 0xF0) // Keep looping until we get a 0xF0 (bin ----____) ch = get_RF(); while(ch == 0xF0) // find end of 0xF0's ch = get_RF(); // Now at the start of the header & data slicer is in sync. // Next line implies addresses starting with 255 are illegal. if (ch == 0xFF) { for (i = 0; i < 6; i++) data[i] = get_RF(); // Have data, now need to validate it i = data[0] + data[1] + data[2]; i = i % 256; j = data[1] + data[2] + data[3]; j = j % 256; if ((i == data[4]) && (j == data[5])) { for (i = 0; i < 6; i++) // Copy validated data to global structure RF_data[i] = data[i]; return (true); } } return (false); } void main() { unsigned int i, loop; char ch; // since we've declared #use fast_io(B) (above), we MUST // include a call to set_tris_b() at startup. // DISABLE_INTERRUPTS(GLOBAL); // Don't interrupt set_tris_a(IRX_A_TRIS); set_tris_b(IRX_B_TRIS); for (i = 0; i < 4; i++) { RED_LED_ON; // reality check at startup to indicate we are alive delay_ms(125); RED_LED_OFF; delay_ms(125); } while(1) { if (RF_RX()) { RED_LED_ON; printf("Loc %d", RF_data[0]); for (i = 1; i < 4; i++) printf(".%d",RF_data[i]); printf("\r\n"); RED_LED_OFF; } } }