Hi everyone! I'm working on with Arduino Mega 2560 and Adafruit Ultimate Breakout. I found this code (for Arduino UNO) and modified it a little bit so that I can use it to Mega 2560. My initial goal is to get the Counter and the Time. However, I am only getting the time and not the counter. I followed this:
MEGA "5V" to GPS "VIN"
MEGA "GND" to GPS "GND"
MEGA "18" to GPS "RX"
MEGA "19" to GPS "TX"
MEGA "12" to GPS "PPS" // I am trying to use Timer 1.
I also tried using 8, 48 and 49 as input pins but I still don't get any counter.
I'm a newbie here. Can you help me? Thank you!
Here is my code:
#define pin_led 13
#define pin_interrupt 2
#include "Arduino.h"
#include "Adafruit_GPS.h"
Adafruit_GPS gps;
// Global variables and defines
#define GPSECHO true
uint32_t timer = millis(); //timer to read & print out the current stats
boolean usingInterrupt = false;
int cycle = 0;
volatile unsigned t1captured = 0;
volatile unsigned t1capval = 0;
volatile unsigned t1ovfcnt = 0;
volatile unsigned long t1time;
volatile unsigned long t1last = 0;
#define BUFFER_SIZE 32
volatile unsigned long int buffer[BUFFER_SIZE];
volatile byte head = 0;
volatile byte tail = 0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600); // GPS communication at 9600 baud
TCCR1A = 0x0; // put timer1 in normal mode
TCCR1B = 0x2; // change prescaler to divide clock by 8
// clear any pending capture or overflow interrupts
TIFR1 = (1<<ICF1) | (1<<TOV1);
// Enable input capture and overflow interrupts
TIMSK1 |= (1<<ICIE1) | (1<<TOIE1);
pinMode(8, INPUT); // Feed the signal in here
}
void loop() {
byte newhead;
if(head != tail) {
newhead = (head + 1) % BUFFER_SIZE;
Serial.println(buffer[newhead]);
head = newhead;
}
if (! usingInterrupt) {
// read data from the GPS in the 'main loop'
char c = gps.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO){
}
}
// 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 trytng 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
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 1 second or so, print out the current stats
if (millis() - timer > 1000) {
timer = millis(); // reset the timer
printTime(); // to print the time
}
}
ISR(TIMER1_OVF_vect) {
t1ovfcnt++; // keep track of overflows;
}
ISR(TIMER1_CAPT_vect) {
unsigned long t1temp;
// combine overflow count with capture value to create 32 bit count
// calculate how long it has been since the last capture
// stick the result in the global variable t1time in 1uS precision
t1capval = ICR1;
t1temp = ((unsigned long)t1ovfcnt << 16) | t1capval;
t1time = (t1temp - t1last) >> 1;
t1last = t1temp;
tail = (tail + 1) % BUFFER_SIZE;
buffer[tail] = t1time;
}
void printTime()
{
Serial.print("Time:");
Serial.print(gps.hour); Serial.print(":"); // print out time (crudely!)
Serial.print(gps.minute); Serial.print(":");
Serial.print(gps.seconds); Serial.print('.');
Serial.print(gps.milliseconds);
Serial.print(" Counter:");
Serial.print(t1capval);
Serial.println();
}