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