From ec67db3624a0d4f836ae90a7567257290110b56f Mon Sep 17 00:00:00 2001 From: Ladyada Date: Wed, 28 Mar 2012 12:39:08 -0400 Subject: [PATCH] Echoing example --- examples/echo/echo.pde | 101 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 examples/echo/echo.pde diff --git a/examples/echo/echo.pde b/examples/echo/echo.pde new file mode 100644 index 0000000..586435d --- /dev/null +++ b/examples/echo/echo.pde @@ -0,0 +1,101 @@ +// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver +// +// This code just echos whatever is coming from the GPS unit to the +// serial monitor, handy for debugging! +// +// Tested and works great with the Adafruit Ultimate GPS module +// using MTK33x9 chipset +// ------> http://www.adafruit.com/products/746 +// Pick one up today at the Adafruit electronics shop +// and help support open source hardware & software! -ada + +#include + +// these are for Arduino 1.0 +#include +SoftwareSerial mySerial(3, 2); + +// if using Arduino v23 or earlier, uncomment these +// two lines and comment out the above. You will +// need to install NewSoftSerial +// #include +// NewSoftSerial mySerial(3, 2); + +// Connect the GPS Power pin to 5V +// Connect the GPS Ground pin to ground +// Connect the GPS TX (transmit) pin to Digital 3 +// Connect the GPS RX (receive) pin to Digital 2 +Adafruit_GPS GPS(&mySerial); + +// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console +// Set to 'true' if you want to debug and listen to the raw GPS sentences +#define GPSECHO true + +// this keeps track of whether we're using the interrupt +// off by default! +boolean usingInterrupt = false; + +void setup() +{ + // connect at 115200 so we can read the GPS fast enuf and + // also spit it out + Serial.begin(115200); + Serial.println("Adafruit GPS library basic test!"); + + // 9600 NMEA is the default baud rate for MTK - some use 4800 + GPS.begin(9600); + + // You can adjust which sentences to have the module emit, below + + // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude + GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); + // uncomment this line to turn on only the "minimum recommended" data for high update rates! + //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); + // uncomment this line to turn on all the available data - for 9600 baud you'll want 1 Hz rate + //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_ALLDATA); + + // Set the update rate + // 1 Hz update rate + //GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); + // 5 Hz update rate- for 9600 baud you'll have to set the output to RMC or RMCGGA only (see above) + GPS.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ); + // 10 Hz update rate - for 9600 baud you'll have to set the output to RMC only (see above) + //GPS.sendCommand(PMTK_SET_NMEA_UPDATE_10HZ); + + // the nice thing about this code is you can have a timer0 interrupt go off + // every 1 millisecond, and read data from the GPS for you. that makes the + // loop code a heck of a lot easier! + useInterrupt(true); + + delay(1000); +} + +// Interrupt is called once a millisecond, looks for any new GPS data, and stores it +SIGNAL(TIMER0_COMPA_vect) { + char c = GPS.read(); + // if you want to debug, this is a good time to do it! + if (GPSECHO) + if (c) UDR0 = c; + // writing direct to UDR0 is much much faster than Serial.print + // but only one character can be written at a time. +} + +void useInterrupt(boolean v) { + if (v) { + // Timer0 is already used for millis() - we'll just interrupt somewhere + // in the middle and call the "Compare A" function above + OCR0A = 0xAF; + TIMSK0 |= _BV(OCIE0A); + usingInterrupt = true; + } else { + // do not call the interrupt function COMPA anymore + TIMSK0 &= ~_BV(OCIE0A); + usingInterrupt = false; + } +} + + +void loop() // run over and over again +{ + // do nothing! all reading and printing is done in the interrupt +}