]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Adafruit_GPS/examples/echo/echo.pde
first commit
[arduino] / books / pdummies / Libraries / Adafruit_GPS / examples / echo / echo.pde
1 // Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
2 //
3 // This code just echos whatever is coming from the GPS unit to the
4 // serial monitor, handy for debugging!
5 //
6 // Tested and works great with the Adafruit Ultimate GPS module
7 // using MTK33x9 chipset
8 //    ------> http://www.adafruit.com/products/746
9 // Pick one up today at the Adafruit electronics shop 
10 // and help support open source hardware & software! -ada
11
12 #include <Adafruit_GPS.h>
13 #if ARDUINO >= 100
14  #include <SoftwareSerial.h>
15 #else
16   // Older Arduino IDE requires NewSoftSerial, download from:
17   // http://arduiniana.org/libraries/newsoftserial/
18 // #include <NewSoftSerial.h>
19  // DO NOT install NewSoftSerial if using Arduino 1.0 or later!
20 #endif
21
22 // Connect the GPS Power pin to 5V
23 // Connect the GPS Ground pin to ground
24 // If using software serial (sketch example default):
25 //   Connect the GPS TX (transmit) pin to Digital 3
26 //   Connect the GPS RX (receive) pin to Digital 2
27 // If using hardware serial (e.g. Arduino Mega):
28 //   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
29 //   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3
30
31 // If using software serial, keep these lines enabled
32 // (you can change the pin numbers to match your wiring):
33 #if ARDUINO >= 100
34   SoftwareSerial mySerial(3, 2);
35 #else
36   NewSoftSerial mySerial(3, 2);
37 #endif
38 Adafruit_GPS GPS(&mySerial);
39 // If using hardware serial (e.g. Arduino Mega), comment
40 // out the above six lines and enable this line instead:
41 //Adafruit_GPS GPS(&Serial1);
42
43
44 // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
45 // Set to 'true' if you want to debug and listen to the raw GPS sentences
46 #define GPSECHO  true
47
48 // this keeps track of whether we're using the interrupt
49 // off by default!
50 boolean usingInterrupt = false;
51 void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
52
53 void setup()  
54 {    
55   // connect at 115200 so we can read the GPS fast enuf and
56   // also spit it out
57   Serial.begin(115200);
58   Serial.println("Adafruit GPS library basic test!");
59
60   // 9600 NMEA is the default baud rate for MTK - some use 4800
61   GPS.begin(9600);
62   
63   // You can adjust which sentences to have the module emit, below
64   
65   // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
66   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
67   // uncomment this line to turn on only the "minimum recommended" data for high update rates!
68   //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
69   // uncomment this line to turn on all the available data - for 9600 baud you'll want 1 Hz rate
70   //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_ALLDATA);
71   
72   // Set the update rate
73   // 1 Hz update rate
74   //GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
75   // 5 Hz update rate- for 9600 baud you'll have to set the output to RMC or RMCGGA only (see above)
76   GPS.sendCommand(PMTK_SET_NMEA_UPDATE_5HZ);
77   // 10 Hz update rate - for 9600 baud you'll have to set the output to RMC only (see above)
78   //GPS.sendCommand(PMTK_SET_NMEA_UPDATE_10HZ);
79
80   // Request updates on antenna status, comment out to keep quiet
81   GPS.sendCommand(PGCMD_ANTENNA);
82
83   // the nice thing about this code is you can have a timer0 interrupt go off
84   // every 1 millisecond, and read data from the GPS for you. that makes the
85   // loop code a heck of a lot easier!
86   useInterrupt(true);
87   
88   delay(1000);
89 }
90
91 // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
92 SIGNAL(TIMER0_COMPA_vect) {
93   char c = GPS.read();
94   // if you want to debug, this is a good time to do it!
95   if (GPSECHO)
96     if (c) UDR0 = c;  
97     // writing direct to UDR0 is much much faster than Serial.print 
98     // but only one character can be written at a time. 
99 }
100
101 void useInterrupt(boolean v) {
102   if (v) {
103     // Timer0 is already used for millis() - we'll just interrupt somewhere
104     // in the middle and call the "Compare A" function above
105     OCR0A = 0xAF;
106     TIMSK0 |= _BV(OCIE0A);
107     usingInterrupt = true;
108   } else {
109     // do not call the interrupt function COMPA anymore
110     TIMSK0 &= ~_BV(OCIE0A);
111     usingInterrupt = false;
112   }
113 }
114
115
116 void loop()                     // run over and over again
117 {
118    // do nothing! all reading and printing is done in the interrupt
119 }