This is an example of the section added to the 1mS interrupt; this was for a Bluetooth link, but it's still incoming serial data, same as a GPS:
In the main program:
Initialise a buffer at the start of the program:
rs232_buffer bt_rx;
C:
while(BT_RX_DATA) // If the UART has a character ready
{
char c;
disable_interrupts(INTR_GLOBAL);
c = fgetc(BT); // Read the character
buf_put(&bt_rx, c); // Put it in the buffer
if(c == '\r') // If the character was end-of-line,
{
buf_put(&bt_rx, '\0'); // Store a null
bt_rx_ready = true; // And set the variable for the main program
}
enable_interrupts(INTR_GLOBAL);
}
In the main program:
Initialise a buffer at the start of the program:
rs232_buffer bt_rx;
C:
// In the main loop:
if(bt_rx_ready)
{
process_bt();
}
// and somewhere else
void process_bt(void)
{
// Last char read was a CR; get the buffer data.
// Read until either a CR is found or the buffer is empty
// (or 80 chars have been read, if garbage).
// LF is ignored and dumped.
int8 i;
char c;
char *p;
char *q;
char parse[16];
i = 0;
c = '\0';
p = bbuf;
do
{
c = buf_get(&bt_rx);
if(c > 0x20 && c < 0x7f)
{
if(c >= 0x41 && c <= 0x5A) // Upper case letters
{
c |= 0x20;
}
*p++ = c;
i++;
}
} while(c && c != '\r' && i < 60);
// String with all spaces removed & line feed omitted.
// Ensure it's a terminated string to avoid overruns..
*p++ = '\0';
// Should now have a valid string in bbuf.
// Check it's not junk or too short to be valid first:
if(i < 2) return;
// Have a command string.
//
// The do whatever you do with the GPS string once received..
//
}