[time-nuts] Clock Correction Algorithm

Attila Kinali attila at kinali.ch
Thu Nov 24 08:48:57 EST 2011

```On Thu, 24 Nov 2011 12:56:31 +0000
Miguel Gonçalves <mail at miguelgoncalves.com> wrote:

> Right! That is what I am doing.
>
> The problem is the drift between adjustments. Arduino's clock is slow 25 ms every minute and if temperature changes it will surely be different.
>
> The proposed algorithm wants to correct that.

Oh.. sorry.. i misunderstood your question.

Yes the algorithm would work, though i'd rather use a pll style sytem.
Ie, measure the differnce in time between your clock and your refrence.
Calculate from this a correction value for the clock frequency (should
be a simple multiplication by a constant factor).
Low pass filter that correction value.

This should also do away with all those special cases in your algorithm,
as a PLL style algo is self stabilizing (unless you got the loop gain
too high*). You will get at most a constant difference between your clock
and the reference. This difference can be minimized by using a higher
gain in the feedback loop (at the cost of stability).

Also as Javier said, 400ppm is a quite high offset for a crystal.
Or are you using a RC oscillator based RTC? (a lot of on chip
RTCs are build that way)

On the other hand, i once used a Atmel SAM7 RTC (or RTT in Atmel lingo)
that is based on a RC Oscillator with +/- 30% accuracy. Using a simple
calibration step (powering up the crystal oscillator every hour and
using the crystal to measure the RTT frequency, setting this as new
correction value, then going back to sleep for an hour) got me below
100ppm over a day in my tests (office enviroment, temperature variation
probably below 2°C over a day, definitly less than 5°C, 30ppm crystal)

Attila Kinali

* this is a very very simplified view on PLL stability, but should be
ok in this (software) context. Please consult a textbook on PLL desing
for further information

--
The trouble with you, Shev, is you don't say anything until you've saved
up a whole truckload of damned heavy brick arguments and then you dump
them all out and never look at the bleeding body mangled beneath the heap
-- Tirin, The Dispossessed, U. Le Guin

```