Added interrupt support

Added interrupt support to shield_sdlog sketch
This commit is contained in:
driverblock 2014-07-30 09:18:03 -04:00
parent 5f0415ee68
commit defdba557d
1 changed files with 73 additions and 24 deletions

View File

@ -26,6 +26,11 @@ Adafruit_GPS GPS(&mySerial);
/* set to true to only log to SD when GPS has a fix, for debugging, keep it false */
#define LOG_FIXONLY false
// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
// Set the pins used
#define chipSelect 10
#define ledPin 13
@ -46,14 +51,14 @@ uint8_t parseHex(char c) {
// blink out an error code
void error(uint8_t errno) {
/*
/*
if (SD.errorCode()) {
putstring("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
*/
putstring("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
*/
while(1) {
uint8_t i;
for (i=0; i<errno; i++) {
@ -72,7 +77,7 @@ void setup() {
// for Leonardos, if you want to debug SD issues, uncomment this line
// to see serial output
//while (!Serial);
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
@ -82,10 +87,10 @@ void setup() {
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect, 11, 12, 13)) {
//if (!SD.begin(chipSelect)) { // if you're using an UNO, you can use this line instead
//if (!SD.begin(chipSelect)) { // if you're using an UNO, you can use this line instead
Serial.println("Card init. failed!");
error(2);
}
@ -102,11 +107,13 @@ void setup() {
logfile = SD.open(filename, FILE_WRITE);
if( ! logfile ) {
Serial.print("Couldnt create "); Serial.println(filename);
Serial.print("Couldnt create ");
Serial.println(filename);
error(3);
}
Serial.print("Writing to "); Serial.println(filename);
Serial.print("Writing to ");
Serial.println(filename);
// connect to the GPS at the desired rate
GPS.begin(9600);
@ -121,39 +128,80 @@ void setup() {
// Turn off updates on antenna status, if the firmware permits it
GPS.sendCommand(PGCMD_NOANTENNA);
// 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);
Serial.println("Ready!");
}
void loop() {
char c = GPS.read();
if (GPSECHO)
if (c) Serial.print(c);
// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
char c;
while (mySerial.available())
{
c = GPS.read();
// if you want to debug, this is a good time to do it!
#ifdef UDR0
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.
#endif
}
}
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() {
char c;
while (mySerial.available())
{
c = GPS.read();
if (GPSECHO)
if (c) Serial.print(c);
}
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trying to print out data
//Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
// Sentence parsed!
Serial.println("OK");
if (LOG_FIXONLY && !GPS.fix) {
Serial.print("No Fix");
return;
Serial.print("No Fix");
return;
}
// Rad. lets log it!
Serial.println("Log");
char *stringptr = GPS.lastNMEA();
uint8_t stringsize = strlen(stringptr);
if (stringsize != logfile.write((uint8_t *)stringptr, stringsize)) //write the string to the SD file
error(4);
error(4);
if (strstr(stringptr, "RMC")) logfile.flush();
Serial.println();
}
@ -161,3 +209,4 @@ void loop() {
/* End code */