Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

GPS pseudocode

Status
Not open for further replies.

Daljeet12

Member level 4
Member level 4
Joined
Jun 16, 2018
Messages
78
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
648
I am trying to understand basic logic of GPS program. how the microcontroller read and Parse the GPS data

I have created following pseudocode. I am having difficulty in interrupt service routine. I am looking help to develop interrupt service route

C:
#define MAX_SENTENCE_LENGTH 20

 
  void main(void)
  {

    init();

    while(1)
    {
        if((strncmp("GPGSV",GPSmsg,5)==0)
        { // if found GPGSV
            extract lat and lon etc.
        }
        
        if((strncmp("GGA",GPSmsg,3)==0)
        { // if found GGA
          //  extract data.
        }
    }
  }
 
  void interrupt(void)
  {
    if(RI==1)
    {  // if character is received..
        temp = SBUF;  //  read and save it
        RI=0;         // and clear interrupt flag
 
        if(temp == '$')
            {  // look for '$' to start the sentence,
                GPSmsg[ix++]= temp;  // save character in buf
            }           
        }   
    }
 

Hi,

it´s not clear what you want to do within the ISR.

Basically the ISR should be short (in time). Thus the usual way is to declare a flag (volatile, global) and set the flag in the ISR.
Then in main the flag gets polled and the task is performed in mail loop context.

****
Btw: a design starts with defining the requirments, then do a flow chart, ... and so on.
I usually do this still with a pencil and some sheets of paper.

Klaus
 

Each hardware platform suggests an approach differing from the other, so that without knowing which uC you are using it is not possible to analyze precisely if it is applicable or not. For example, ordinary 8-bit microcontrollers have a limited amount of RAM, so buffering sentences for further processing can turn into bottleneck to your application. I don't remember the typical rate at which GPS sentences are sent ( ~75 bytes each second ? ), but if you need to get the measurements in a 'realtime' fashion on a core limited in hardware resources and having to deal with many other tasks, I recommend considering parsing the commands on the fly - within the ISR. Laborious and unstructured really, but optimized in processing, since headers prefixed with the same characters will not significantly increse the overhead.
 

Hint:
when you receive '$' set 'ix' to zero and increment it as each new character is received. That will add each new character to the GPSmsg string. Remember that in the C language a string is terminated with a zero so think about how your string comparison will find the end of the message if the terminator is missing.

Brian.
 

The shown code doesn't. It only copies '$' to GPSmsg. Please reconsider.
That's what I am asking for help understanding
Hint:
when you receive '$' set 'ix' to zero and increment it as each new character is received. That will add each new character to the GPSmsg string. Remember that in the C language a string is terminated with a zero so think about how your string comparison will find the end of the message if the terminator is missing.

Brian.

->Remember that in the C language a string is terminated with a zero so think about how your string comparison will find the end of the message if the terminator is missing.

Code:
GPSmsg[ix]= '\0';


C:
#define MAX_SENTENCE_LENGTH 20
volatile char GPSmsg[ix];
 
  void main(void)
  {

    init();

    while(1)
    {
        if((strncmp("GPGSV",GPSmsg,5)==0)
        { // if found GPGSV
            extract lat and lon etc.
        }
        
        if((strncmp("GGA",GPSmsg,3)==0)
        { // if found GGA
          //  extract data.
        }
    }
  }
 
  void interrupt(void)
  {
    if(RI==1)
    {  // if character is received..
        temp = SBUF;  //  read and save it
        RI=0;         // and clear interrupt flag
 
        if(temp == '$')
            {  // look for '$' to start the sentence,
                ix = 0;
                
            }   
         else
            {
                GPSmsg[ix++]= temp;  // save character in buf
            }           
        }   
    }
 

Hi,
That's what I am asking for help understanding
You could do yourself a favour by:
* writing what you expect it to do
* write how you test it
* write what is not as expected

Klaus
 
Last edited:

You are close in your new code at post #8 but the zero terminator might still cause problems. I'm not going to write the code for you but the principle is:

1. Allocate at least one extra space in the storage for GPSmag so it can hold the terminator
2. initialize your index into the message to zero.
3. Use the ISR to read the character.
4. if the character is '$' make ix = 0
otherwise
5. Store the character at GPSmsg[ix]
6. Increment ix
7. Store zero (as in 0x00) in GPSMsg[ix]

Ideally (its your option) dimension the storage like this:
char GPSmsg[MAX_SENTENCE_LENGTH +1];
and start step 5 above with:
if(ix < MAX_SENTENCE_LENGTH)
this will ensure you don't overrun the storage if a further '$' isn't received before the storage is full.

Brian.
 

You are close in your new code at post #8 but the zero terminator might still cause problems. I'm not going to write the code for you but the principle is:

1. Allocate at least one extra space in the storage for GPSmag so it can hold the terminator
2. initialize your index into the message to zero.
3. Use the ISR to read the character.
4. if the character is '$' make ix = 0
otherwise
5. Store the character at GPSmsg[ix]
6. Increment ix
7. Store zero (as in 0x00) in GPSMsg[ix]
Brian.

Understood till this logic what to do next ?
C:
#define MAX_SENTENCE_LENGTH 20

 volatile char GPSmsg[ix];
  void main(void)
  {

    init();

    while(1)
    {
        if((strncmp("GPGSV",GPSmsg,5)==0)
        { // if found GPGSV
            extract lat and lon etc.
        }
        
        if((strncmp("GGA",GPSmsg,3)==0)
        { // if found GGA
          //  extract data.
        }
    }
  }
 
  void interrupt(void)
  {
    if(RI==1)
    {  // if character is received..
        temp = SBUF;  //  read and save it
        RI=0;         // and clear interrupt flag
 
        if(temp == '$')
            {  // look for '$' to start the sentence,
                ix = 0;
                
            }   
         else
            {
                GPSmsg[ix++]= temp;  // save character in buf
                
                if(ix == MAX_SENTENCE_LENGTH))
                {
                    GPSmsg[ix]= '\0'; 
                }
            }           
        }   
    }
 

... and not
if(ix == MAX_SENTENCE_LENGTH))
but
if(ix < MAX_SENTENCE_LENGTH)

The intention is to prevent any more characters being added to GPSmsg that could make it overflow. If a '$' didn't arrive to reset ix, it would keep increasing and start to store characters beyond the end of GPSmsg.

This is a snippet of similar code in one of my devices, i have changed my variable names to your so you can see the principle but you need to adapt it to your program. In my application it was triggered by an ISR but isn't inside the ISR itself:
Code:
{
    char temp = SBUF;
    
    if(temp == '$') 
    {
        ix = 0;
        *GPSmsg = 0;
        return;
    }
    
    if(temp == CR) 
    {
        GPSDataReceived = 1;
        return;
    }

    if(ix < MAX_SENTENCE_LENGTH - 1)
    {
        GPSmsg[ix++] = temp;
        GPSmsg[ix] = 0;
    }
}

Note that in my case CR (#defined as 0x0D) is the end character of the data being sent, you may need to change it to match the end character of your GPS data.

Brian.
 

Hi,

I ask myself what this thread is about.

There are many GPS/NMEA parsers around. Why not use them, why not adjust them for your needs?

Or is it for learning?
Then I can only repeat: Do first things first. A flow chart a diagram, paper, pencil ... is the least one can expect.

Or is it that you want others do your job?

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top