I am in need of a little assistance with a project that I am apart of. The problem that we have is we need a precise time for when we discharge our Current Impulse Generator. To do this we are using an Ultimate GPS Breakout V3 for the GPS time, but we need to know the time in microseconds. We are using the Arduino Uno which has a speed of 16MHz. The task that I am stuck on is that I need to count how many clock cycles happen in 1 second. I need a counter that counts the clock cycles every second and resets back to zero when 1 second is reached, so we will know how many clock cycles have passed in the second that the system is discharged. The GPS time will tell us the hours, minutes, and seconds in which we discharged the system. Here is the code that I have already compiled together (I am using the Adafruit GPS library for the code that I have):
#include <Adafruit_GPS.h>
#include <SPI.h>
// what's the name of the hardware serial port?
#define GPSSerial Serial
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false
uint32_t timer = millis();
uint32_t millisecondtime;
boolean toggle1 = 0;
const int buttonPin = 4;
const int ledPin = 13;
int buttonState = 0;
int t;
void setup()
{
//while (!Serial); // uncomment to have the sketch wait until Serial is ready
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.println("Adafruit GPS library basic test!");
cli(); //stops interrupts
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
// uncomment this line to turn on only the "minimum recommended" data
//GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
// For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
// the parser doesn't care about other sentences at this time
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
// For the parsing code to work nicely and have time to sort thru the data, and
// print it out we don't suggest using anything higher than 1 Hz
// Request updates on antenna status, comment out to keep quiet
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
//This is the interrupt for 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0 //TCNT1 counts system clock ticks
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); //allow interupts
// Ask for firmware version
//GPSSerial.println(PMTK_Q_RELEASE);
}
ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
t+=1;
if(t==10)
{
t=0;
}
//if (toggle1){
//digitalWrite(ledPin,HIGH);
//toggle1 = 0;
//Serial.println(TCNT1);
//}
//else{
// digitalWrite(ledPin,LOW);
// toggle1 = 1;
//}
}
void loop() // run over and over again
{
//buttonState = digitalRead(buttonPin);
//Serial.println(buttonState);
//if(buttonState == HIGH)
//{
// digitalWrite(ledPin, HIGH);
//}
//else
//{
// digitalWrite(ledPin, LOW);
//}
//delay(100);
Serial.println(t);
// 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 (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
}
// approximately every 2 seconds or so, print out the current stats
if (micros() - timer > 1000000) {
//timer = micros(); // reset the time
//millisecondtime = timer;
if (timer <= 1000000)
{
timer = micros();
}
else //if (millisecondtime < 2000)
{
timer = 0;
}
Serial.println("\nMilliseconds");
Serial.println(millisecondtime);
Serial.println("\nTimer");
Serial.println(timer);
Serial.print("\nTime: ");
if (GPS.hour < 10) { Serial.print('0'); }
Serial.print(GPS.hour, DEC); Serial.print(':');
if (GPS.minute < 10) { Serial.print('0'); }
Serial.print(GPS.minute, DEC); Serial.print(':');
if (GPS.seconds < 10) { Serial.print('0'); }
Serial.print(GPS.seconds, DEC); Serial.print('.');
if (GPS.milliseconds < 10) {
Serial.print("00");
} else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
Serial.print("0");
}
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
Serial.print("Location: ");
float lat = (int) (GPS.latitude/100);
lat += (GPS.latitude/100 - lat)/0.6;
Serial.print(lat,6); Serial.print(GPS.lat);
Serial.print(", ");
float lon = (int) (GPS.longitude/100);
lon += (GPS.longitude/100 - lon)/0.6;
char content = GPS.lon;
if(content == ('W'))
lon = lon * -1;
else
lon = lon * 1;
Serial.print(lon,6); Serial.println(GPS.lon);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}
//This prints out the total time the program is running
//Serial.print("\nMilliseonds");
//Serial.println(timer);
}
I have already tried to play with interrupts to see if I could do the job, but I am not able to figure it out. Any help would be appreciated.