/* ** Program COMPASS1.C ** ** This program interfaces the serial port with the V2X Compass Module. ** Compass bearings are continually taken and sent to RF */ #include <16F876.H> #fuses HS,NOPROTECT,NOWDT #use delay (clock = 20000000) #define COMPASS_SDO PIN_C4 #define COMPASS_RESET PIN_B1 #define COMPASS_PC PIN_B2 #define COMPASS_EOC PIN_B0 #define COMPASS_SCLK PIN_B4 long data = 0; int read_done = 0; /* works properly*/ void reset_compass(){ //set the signal lines output_high(COMPASS_RESET); output_high(COMPASS_PC); output_high(COMPASS_SCLK); //reset the compass output_low(COMPASS_RESET); delay_ms(15); output_high(COMPASS_RESET); //delay before the first read delay_ms(550); } void init_read_compass(){ //output_high(COMPASS_RESET); //give a pulse to compass output_low(COMPASS_PC); delay_ms(15); output_high(COMPASS_PC); } #int_ext void read_compass() { short int j; int i; //output_high(COMPASS_RESET); //delay at least 10 msec before reading delay_ms(10); //PC and SS are connected together! output_low(COMPASS_PC); //create the first 8 cycles for ( i = 0; i < 8; i++) { output_low (COMPASS_SCLK); delay_us(15); output_high (COMPASS_SCLK); delay_us(15); j = input(COMPASS_SDO); data = (data<<1) | j; } //this delay is application dependent delay_ms(5); //create the second 8 cycles for ( i = 0; i < 8; i++) { output_low (COMPASS_SCLK); delay_us(15); output_high (COMPASS_SCLK); delay_us(15); j = input(COMPASS_SDO); data = (data<<1) | j; } //set SS high again (SS and PC are connected) output_high(COMPASS_PC); read_done = 1; } #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7) void main(){ byte messageByte [7]; int i; messageByte[0] = 50; //To/From messageByte[1] = 1; //packet ID messageByte[2] = 4; //message length messageByte[3] = 2; //start of message indicator messageByte[6] = 3; //end of message indicator ext_int_edge( l_to_h ); enable_interrupts(global); enable_interrupts(ext_int); //printf("\r\nStarted!\n\r"); reset_compass(); //printf("Compass resetted!\n\r"); delay_ms(2000); while(TRUE){ init_read_compass(); //printf("read_done: %U\n\r", read_done); delay_ms(150); if (read_done){ //printf("Data: %lu\n\r", data); messageByte[4] = data >> 8; messageByte[5] = data; for (i = 0; i < 7; ++i){ putchar(messageByte[i]); } if (messageByte[1] < 7) ++messageByte[1] ; else messageByte[1] = 1; read_done = 0; } delay_ms(3000); } }