//************************************************************************
//* GPS Clock
//* by Mark Sproul
//* March 6, 2010
//************************************************************************
#include
#include
#include
#include "WConstants.h"
#include "wiring.h"
#include "HardwareSerial.h"
//#include
#include
#define kTX_Pin 4
#define kRX_Pin 5
#define kPOWER_Pin 6 //* was 6
#define kLED1_PIN 7
#define kLED2_PIN 8
#define kGPSRATE 4800
//AFSoftSerial gpsSerial = AFSoftSerial(kTX_Pin, kRX_Pin);
SoftwareSerial gpsSerial = SoftwareSerial(kTX_Pin, kRX_Pin);
#define kNMEAbuffSize 90 // plenty big
char gNMEAbuffer[kNMEAbuffSize];
char gNMEAbuffidx;
int gNMEAhour;
int gNMEAminute;
int gNMEAsecond;
#define _OUTPUT_TO_CONSOLE_
//************************************************************************
void setup()
{
Serial.begin(57600);
Serial.println();
Serial.println("GPS Clock");
pinMode(kLED1_PIN, OUTPUT);
pinMode(kLED1_PIN, OUTPUT);
//* turn on the GPS
pinMode(kPOWER_Pin, OUTPUT);
digitalWrite(kPOWER_Pin, LOW); // pull low to turn on!
gpsSerial.begin(kGPSRATE); //* open GPS port
Serial.println("GPS Power turned on");
gNMEAbuffidx = 0;
}
//************************************************************************
//* $GPGGA,003509.000,4027.4299,N,07430.2057,W,1,04,2.0,40.9,M,-34.1,M,,0000*56
//* $GPGSA,A,3,32,14,31,30,,,,,,,,,4.0,2.0,3.5*37
//* $GPRMC,003509.000,A,4027.4299,N,07430.2057,W,0.48,346.15,070310,,*15
//* $GPGGA,003510.000,4027.4297,N,07430.2055,W,1,04,2.0,40.9,M,-34.1,M,,0000*52
//* $GPGSA,A,3,32,14,31,30,,,,,,,,,4.0,2.0,3.5*37
//* $GPGSV,3,1,12,25,70,326,,31,70,008,28,32,51,276,35,01,48,314,26*77
//* $GPGSV,3,2,12,05,42,054,,14,40,112,22,16,37,195,23,20,28,304,28*70
//* $GPGSV,3,3,12,30,23,045,28,06,18,074,,07,16,062,,22,09,168,*7D
//* $GPRMC,003510.000,A,4027.4297,N,07430.2055,W,0.35,144.00,070310,,*1F
//* $GPGGA,003511.000,4027.4291,N,07430.2051,W,1,04,2.0,40.8,M,-34.1,M,,0000*50
//* $GPGSA,A,3,32,14,31,30,,,,,,,,,4.0,2.0,3.5*37
//* $GPRMC,003511.000,A,4027.4291,N,07430.2051,W,1.38,153
//************************************************************************
void ProcessNMEA(char *nmeaBuffer)
{
long nmeaSentanceType;
long ii;
char hourString[4];
char minString[4];
char secString[4];
if ((nmeaBuffer[0] == '$') && (nmeaBuffer[1] == 'G'))
{
// Serial.println(nmeaBuffer);
nmeaSentanceType = 0;
for (ii = 0; ii < 4; ii++)
{
nmeaSentanceType = nmeaSentanceType << 8;
nmeaSentanceType += nmeaBuffer[2 + ii];
}
// Serial.println(nmeaSentanceType, HEX);
switch(nmeaSentanceType)
{
// case 'PGGA':
case 0x50474741:
// Serial.println(nmeaBuffer);
ii = 7;
hourString[0] = nmeaBuffer[ii++];
hourString[1] = nmeaBuffer[ii++];
hourString[2] = 0;
minString[0] = nmeaBuffer[ii++];
minString[1] = nmeaBuffer[ii++];
minString[2] = 0;
secString[0] = nmeaBuffer[ii++];
secString[1] = nmeaBuffer[ii++];
secString[2] = 0;
Serial.print("\x0c");
Serial.print(hourString);
Serial.print(":");
Serial.print(minString);
Serial.print(":");
Serial.print(secString);
Serial.println(" Zulu");
gNMEAhour = atoi(hourString);
gNMEAminute = atoi(minString);
gNMEAsecond = atoi(secString);
if (gNMEAsecond == 0)
{
Serial.print("\x07"); //* send a bell
}
gNMEAhour += 19; //* adjust to local time
digitalWrite(kLED1_PIN, HIGH); //* turn on LED
break;
// case 'PGSV':
case 0x50475356:
digitalWrite(kLED1_PIN, LOW); //* turn on LED
break;
}
}
}
//************************************************************************
void loop()
{
char gpsChar;
gpsChar = gpsSerial.read();
if (gpsChar == 0x0d)
{
//* if its a CR, process the line
gNMEAbuffer[gNMEAbuffidx++] = 0;
ProcessNMEA(gNMEAbuffer);
gNMEAbuffidx = 0;
}
else if ((gpsChar >= 0x20) && (gNMEAbuffidx < kNMEAbuffSize))
{
//* valid char store it
gNMEAbuffer[gNMEAbuffidx++] = gpsChar;
//* first char MUST be '$'
if (gNMEAbuffer[0] != '$')
{
gNMEAbuffidx = 0;
}
}
}
|