I'm using an ATMega32 with a GPS module to display some data on a LCD display (longitude and latitude). The GPS module sends a string of data every second at 9600 bps. The string is a NMEA sentence, starting with a $ sign and I use that character to synchronize the receiver (AVR UART).
this is the code I use:
// GPS.h
void GPS_read(char *sentence)
{
while ((*sentence = USART_receive()) != '$')
;
USART_receive_string(++sentence);
}
// USART.h
unsigned char USART_receive(void)
{
while (!(UCSRA & (1<<RXC)))
;
return UDR;
}
void USART_receive_string(char *string)
{
do
{
*string = USART_receive();
} while (*string++ != '\n'); // NMEA sentences are <CR><LF> terminated
*string = '\0';
}
I pass a char array to GPS_read and then display the string on the LCD. depending on the timing I choose for displaying the data, I get some rubbish data consisting of a $G and a \n character.
I'm making some mistake here, but it's been two days and I can't figure out what I am doing wrong (I'm a novice embedded programmer :) )
please help! thanks Luca