// 11.0592MHz XTAL //Sample rates: with 11.0592MHz xtal //4800baud - 3 x sample rate = 0xC5 //9600baud - 3 x sample rate = 0xE1 //9600baud - 2 x sample rate = 0xD0 //9600baud - 0xA1 #include "sdccF000.h" #define TRUE 1 #define FALSE 0 #define LCD_E P2_7 #define LCD_RS P2_6 #define LCD_RW P2_5 #define SMILEY_FACE 1 #define TX2 P0_6 //bit bashed UART2 TX #define RX2 P0_7 //bit bashed UART2 RX #define CR 10 #define LF 13 //Prototypes void config_mcu(void); void LCD_initialise(); void LCD_cleardisplay(); void LCD_newline(); void LCD_placecursor(unsigned char place); void LCD_command(unsigned char command); void LCD_display_string(unsigned char displaystring[],char start,char end); void send_LCD_character(unsigned char character); void Delay(); //Global variables int i = 0; int k = 0; unsigned char l; unsigned int j; char n = 0; __bit UART1_sample = FALSE; __bit byte_recieved = FALSE; __bit start_bit = FALSE; unsigned char state = 0; unsigned char bit_mask = 0x01; unsigned char rx_buffer[70]; //Put the GPS string in here unsigned char sample_count = 0; unsigned char enable = 2; unsigned char rx_byte = 0; unsigned char temp_data = 0; unsigned char m = 0; void main(void) { //setup the MCU's registers config_mcu(); LCD_initialise(); LCD_cleardisplay(); while(TRUE) { if(start_bit == FALSE) //Looking for a start bit { if(RX2 == TRUE && state == 0) state = 1; if(RX2 == FALSE && state == 1) { state = 2; start_bit = TRUE; TH0 = 0xFF; TL0 = 0xC5; TCON = 0x10; //enable the timer } } if (byte_recieved == TRUE) { TCON = 0x00; if (i > 69) { //A typical string from the GPS is: //$GPRMC,013321,V,4118.0547,S,17448.3144,E,000.0,000.0,280411,022.1,E*77 //Lat LCD_display_string(rx_buffer,16,17); send_LCD_character('.'); LCD_display_string(rx_buffer,18,19); LCD_display_string(rx_buffer,21,24); LCD_display_string(rx_buffer,26,26); //Time LCD_display_string(" ",0,1); LCD_display_string(rx_buffer,7,8); send_LCD_character(':'); LCD_display_string(rx_buffer,9,10); send_LCD_character(':'); LCD_display_string(rx_buffer,11,12); send_LCD_character(' '); LCD_newline(); //Long LCD_display_string(rx_buffer,28,30); send_LCD_character('.'); LCD_display_string(rx_buffer,31,32); LCD_display_string(rx_buffer,34,37); LCD_display_string(rx_buffer,39,39); //Date send_LCD_character(' '); LCD_display_string(rx_buffer,53,54); send_LCD_character('/'); LCD_display_string(rx_buffer,55,56); send_LCD_character('/'); LCD_display_string(rx_buffer,57,58); LCD_placecursor(0); i = 0; } rx_byte = 0; byte_recieved = FALSE; start_bit = FALSE; state = 0; } } } void config_mcu() { int i = 0; //Disable the WatchDog WDTCN = 0xDE; WDTCN = 0xAD; //UART_init - BUILT-IN UART // setup the UART for 4800, 8, N, 1 SCON = 0x52; CKCON = 0x24; T2CON = 0x34; RCAP2H = 0xFF; RCAP2L = 0xB8; TH2 = 0xFF; TL2 = 0xB8; //Timer 0 - use for 4800 baud bit-bashed UART TMOD = 0x01; TH0 = 0xFF; TL0 = 0xC5; //port_IO_init - change for your MCU - set up like this for an existing prototyping board // P0.0 - SCL (I2C), Open-Drain // P0.1 - SDA (I2C), Open-Drain // P0.2 - TX (UART), Push-Pull // P0.3 - RX (UART) XBR0 = 0x05; //UART assigned to Port0.2 and 0.3 XBR1 = 0x00; XBR2 = 0x40; //Weak pull ups enabled for Open Drain outputs PRT0CF = 0x7C; PRT1CF = 0xF0; //0-3 Open-Drain, 4-7 Push-Pull PRT2CF = 0xFF; PRT3CF = 0xFF; //oscillator_init OSCXCN = 0x67; for (i = 0; i < 3000; i++); // Wait 1ms for initialization while ((OSCXCN & 0x80) == 0); OSCICN = 0x08; //interrupts_init IE = 0x82; IP = 0x02; //Timer 0 high priority } void LCD_initialise() { LCD_E = 0; LCD_RW = 0; LCD_RS = 0; Delay(); LCD_command(0x38); //8bit, 2 lines, default font Delay(); LCD_command(0x08); //turn display off Delay(); LCD_command(0x0C); //turn display on, no cursor Delay(); LCD_cleardisplay(); Delay(); } void LCD_cleardisplay() { LCD_command(0x01); } void LCD_newline() { LCD_command(0xC0); } void LCD_placecursor(unsigned char place) { LCD_command(0x80 + place); } void LCD_command(unsigned char command) { LCD_RS = 0; //command mode P3 = command; LCD_E = 1; Delay(); LCD_E = 0; Delay(); LCD_RS = 1; //back to data mode } void LCD_display_string(unsigned char displaystring[],char start,char end) { l = start; while( l < end+1) { send_LCD_character(displaystring[l]); l++; } } void send_LCD_character(unsigned char character) { LCD_RW = 0; LCD_E = 0; LCD_RS = 1; //data mode P3 = character; LCD_E = 1; Delay(); LCD_E = 0; Delay(); } void Delay() { j = 0; while (j !=2000) { j++; } } /********** INTERRUPT ROUTINES ****************/ void Timer0_Isr (void) __interrupt (1) { //TX2 = !TX2; //to test period here is correct TH0 = 0xFF; TL0 = 0xC5; // 3 x 4800 baud with 11.0592MHz xtal sample_count++; if (sample_count > 26 && RX2 == TRUE) { sample_count = 0; m = 0; byte_recieved = TRUE; rx_buffer[i] = rx_byte; i++; } if((sample_count == 4) || (sample_count == 7)|| (sample_count == 10)|| (sample_count == 13)|| (sample_count == 16)|| (sample_count == 19)|| (sample_count == 22)|| (sample_count == 25)) { TH0 = 0xFF; TL0 = 0xC5; temp_data = RX2; //sample the bit temp_data = temp_data << m; //shift it to the right bit location m++; rx_byte += temp_data; //add it to the recieved byte temp_data = 0; } }