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"); } } }
GPSISR.cpp
- Committer:
- trevieze
- Date:
- 2017-03-01
- Revision:
- 6:f0c13bb7d266
- Parent:
- 4:7240a18102a6
File content as of revision 6:f0c13bb7d266:
/* 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 <string> #include "nmea.h" #include "GPSISR.h" NMEA nmea; //parse GPS Sentence. 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(); nmea.fusedata(rx_in); interrupt = 0; return; } bool GPS::dataready(void){ return nmea.isdataready(); } void GPS::read(void) { buffer.hours = nmea.getHour(); buffer.minutes = nmea.getMinute(); buffer.seconds = nmea.getSecond(); buffer.day = nmea.getDay(); buffer.month = nmea.getMonth(); buffer.year = nmea.getYear(); buffer.latitude = 0.0; buffer.longitude = 0.0; if (nmea.isdataready()) { buffer.latitude = nmea.getLatitude(); buffer.longitude = nmea.getLongitude(); buffer.speed = nmea.getSpeed(); buffer.altitude = nmea.getAltitude(); buffer.bearing = nmea.getBearing(); buffer.satellites = nmea.getSatellites(); } }