A GPS serial interrupt service routine that has an on the fly nmea parser. Works with a STM32F411RE and a Adafruit GPS logger.
Dependents: Bicycl_Computer_NUCLEO-F411RE Bicycl_Computer_NUCLEO-L476RG
Fork of GPS by
main.cpp
#include "mbed.h" #include "GPSISR.h" #define PIN_RX_GPS PA_12 //GPS Shield RX pin #define PIN_TX_GPS PA_11 //GPS Shield TX pin Serial pc(USBTX, USBRX); // Set up serial interrupe service handler for gps characters. GPS MyGPS(PIN_TX_GPS,PIN_RX_GPS, 9600); int main() { while (1) { if (MyGPS.dataready()) { MyGPS.read(); pc.printf("NMEA has valid data"); pc.printf("Sats : %d \n", MyGPS.buffer.satellites); pc.printf("%d-%d-%d\n", MyGPS.buffer.month, MyGPS.buffer.day, MyGPS.buffer.year); pc.printf("%d:%d:%d\n", MyGPS.buffer.hours, MyGPS.buffer.minutes, MyGPS.buffer.seconds); } else { pc.printf("NMEA has no valid data"); } } }
Diff: GPSISR.cpp
- Revision:
- 1:4b5ffae743c0
- Child:
- 2:dbc6c0789611
diff -r 15611c7938a3 -r 4b5ffae743c0 GPSISR.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPSISR.cpp Fri Feb 10 17:57:45 2017 +0000 @@ -0,0 +1,50 @@ +/* mbed EM-406 GPS Module Library + * Copyright (c) 2008-2010, sford + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "GPSISR.h" + +GPS::GPS(PinName tx, PinName rx, int Baud) : _gps(tx, rx) { + _gps.baud(Baud); + _gps.attach(this,&GPS::Rx_interrupt, Serial::RxIrq); + printf("Interrupt Attached"); +} + +void GPS::getline() { + while(_gps.getc() != '$'); // wait for the start of a line + for(int i=0; i<256; i++) { + msg[i] = _gps.getc(); + if(msg[i] == '\r') { + msg[i] = 0; + return; + } + } + error("Overflowed message limit"); + return; +} + +// Interupt Routine to read in data from serial port +void GPS::Rx_interrupt(void) { + interrupt = 1; + rx_in = _gps.getc(); + interrupt = 0; + return; +}