1
votes

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

2

2 Answers

0
votes

Have you checked that your baudrate of TX and RX is correct ? Check for frame errors as well.

-1
votes

There are a few errors with your code, try this:

You haven't included your char array declaration, but I would advise you use an indexer to keep track of which element you are reading and/ or writing to within your array.

unsigned char Sentence[*Insert array size here*];
unsigned char Indexer = 0;

and as for your functions, I'd say your USART_receive() function is fine, but try...

void GPS_read(char *sentence)
{
    unsigned char Data = USART_receive();   // Read initial value
    while (Data != '$')                     // while Data is not a '$' ...
        Data = USART_receive();             // ... Read the USART again until it is

    sentence[Indexer++] = Data;
    USART_receive_string(sentence);        
}

void USART_receive_string(char *string)
{
    unsigned char Data = USART_receive();

    while (Data != '\n')
    {
        *string[Indexer++] = Data;
        Data = USART_receive();
    }
    *string[Indexer] = '\n';
}