Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Calculate absolute time based on cycles and RTC

I'm developing a low power data logging device. It samples sensors at the frequency of 20Hz, and also has a real time clock for absolute time (the logged time need to be exact down to ms).

I think that querying time for every samples would be wasteful of energy. One solution is calculate time based on number of cycles, and after a fixed duration (10 minutes) query RTC to update the time.

Here is my attempt:

#define INTERVAL 12000 //10 minutes: 10*60*20 samples
int count = 0;
int checkPointMillis;
int checkPointRTC;
// timer interrupt at 20Hz
ISR() {
 int a = analogRead(A0);
 int time = millis() - checkPointMillis + checkPointRTC;
 // write data to a buffer
 if (count > INTERVAL) {
 count = 0;
 checkPointMillis = millis();
 checkPointRTC = getRTCTime();
 }
}

Is this a correct approach?

Answer*

Draft saved
Draft discarded
Cancel
5
  • The approach of adjusting the drift count due to 6250+ deviation can keep time at perhaps one part per thousand. However, adjusting the TOP value would allow correction in increments of about one part per 12500. In other words, the value loaded into OCR1A should slowly change, over a period of minutes or hours, to produce correct interrupt rate. Commented May 1, 2017 at 15:33
  • @JamesWaldby-jwpat7: I don't understand your comment "[...] can keep time at perhaps one part per thousand". A properly implemented PLL has exactly zero long term drift (it keeps time at zero parts per million), with only short term variations (phase noise) which depend on the clocks and the PLL time constant. One part in 12500 is 80 ppm, very crude compared with the 4 ppm resolution you get using input capture. Commented May 1, 2017 at 19:44
  • EB, I misunderstood part of your answer and withdraw most of my comment. It isn't clear to me what control the PLL will vary, what its resolution is, etc. Perhaps in your answer you could show a typical PLL-corrected time = ... calculation. Commented May 1, 2017 at 20:07
  • @JamesWaldby-jwpat7: Added some options for the PLL algorithm. That was a looong digression... Commented May 3, 2017 at 18:13
  • EB, yes. Great exposition; I'd upvote your answer again if I could. Commented May 3, 2017 at 18:20

AltStyle によって変換されたページ (->オリジナル) /