Arfternoon arduinoites
I'm sorta new to arduino but absolutely love it so far.
I'm making a project and i need to display microseconds.
What have is a perfect 1Hz output PPS pin from a GPS module that is meant to be accurate to 5nS.
I need it to reset the micro second time so the GPS time is accurate
I also have another button that when pressed, will save the exact time the button is pressed, more or less down to 10 micro seconds.
I have been racking my brain on how to do this for days now, and from many interweb searches, i cant find anything like it.
Would you guys be able to help me please?
I have the rest of the code working, like 4 pages so far haha but these are the last two things i need to do.
You have left out a critical piece of information - how often do you want the microsecond display to update? And once per microsecond is NOT the right answer.
I don't understand this
I need it to reset the micro second time so the GPS time is accurate
Sorry, it doesn't need to display the micro seconds, only have the time down to about 10 micro seconds ready so when the button goes high, it can save it, and the gps time only goes down to 10 milliseconds for me, so i was going to use the PPS to reset a micro timer to have micro and milli seconds and just use the gps seconds, min ...
To clarify some more (because i have written it badly haha), i want my adafruuit GPS module to give me GPS time, which is accurate to 10ms (which i have already done), then to get down to micro seconds, i need the internal clock to sync to the GPS PPS output which is accurate to 10ns.
I have searched heaps and cant really find anything that has a code that i understand.
Surely resting a microsecond timer when the PPS goes high isn't that hard.
And secondly (which is really a second topic), i want it to log the exact time down to 10 micro seconds when a button is pushed.
I should be able to work this out on my own though, i'm just taking one step at a time and want to get the time right first.
uncharted:
then to get down to micro seconds, i need the internal clock to sync to the GPS PPS output which is accurate to 10ns.
I still don't understand what you are trying to do. Perhaps you can explain what you want to do with the synchronized "clock". I'm not even sure what you mean by "internal clock".
AFAIK there is no single pin that can reset any of the Hardware Timers. I believe the only way to reset them is to write a byte to one of the timer registers. Between receiving / detecting the PPS output and writing to a register there are likely to be 3 or 4 Arduino clock-cycles involved, each taking 62.5 nano secs.
In any case as the Arduino's 16MHz clock ticks at 62.5 nano second intervals there is no prospect of 10ns accuracy.
Instead of trying to "reset" the micros() internal variables to zero (NOTE: "internal"), just take a timestamp when the PPS signal occurs:
ppsTime = micros();
When you want to know the current time, just subtract the current micros() from that timestamp:
currentMicros = micros() - ppsTime;
This works across the micros() rollover, too.
If you also want the UTC hours/minutes/seconds time from the GPS, you might try my NeoGPS library. It's smaller, faster, more reliable and more accurate than all other libraries. It also has a way to incorporate the PPS into the time calculations (see comments here).
The example SyncTime.ino shows how to display the current UTC time at 100ms intervals. It's fairly short.
Robin2:
That thought had crossed my mind but there is no way it would provide 10ns accuracy - would it even give 1000ns accuracy?
Cross it out of your mind. He said the GPS module has 10 ns accuracy, not that he's expecting the Arduino to have it.
Here's what I would do. Hook the PPS signal to one of the external interrupts and trigger it off of whichever edge it needs to be. In the ISR, take the current micros() value, subtract the previous micros() value from it, and now you've got a calibration for a micros_per_second value that you can use to extrapolate timings with. Also store the current GPS time.
Then, when an event comes in, you subtract it from the micros() time of the last PPS edge and you can use your calibration value to adjust the timer reading. Say you're getting a measurement of 987,468 micros() counts per second (your Arduino's a bit slow) and you get a button event 64,576 micros() counts after the last PPS. Put it through the map function like so
map( 64576, 0, 987468, 0, 1000000 );
And get an adjusted result of 65395 microseconds since the last PPS event. Just append that result to the time and you're done.
This is probably the best that you could do on this hardware without learning how to use the input capture peripheral.