[time-nuts] Clock Correction Algorithm

Attila Kinali attila at kinali.ch
Fri Nov 25 07:38:34 UTC 2011


On Thu, 24 Nov 2011 16:32:04 +0000
Miguel Gonçalves <mail at miguelgoncalves.com> wrote:

> Hi Atilla!
> 
> On 24/11/2011, at 14:42, Attila Kinali <attila at kinali.ch> wrote:
> 
> > I'm not quite sure i understood you correctly, and i dont know anything
> > about the arduino and the avr32. But usually, you set a timer to do a
> > certain repetition rate. Ie raises an interrupt ever x clock cycles.
> > Or to put it differently, you let the timer run freely, but let it call
> > you when it's time to do something.
> 
> It's really easy: the timer counter is increased 62500 times per
> second so on each increase 16 us have passed. I set the start of the
> counter to 3036 to let it run to 65535. From 3036 to 65535 one second
> has passed at the 62500 Hz rate  When it overflows a routine is called
> that simply updates a global boolean flag.

[from another mail]
> > Does the counter needs reloading every overflow or it is automatically reloaded? depending on how you are done this, it can also provide some delays that apparently slows the clock.
> 
> It's manually reloaded with the 3036 value every time the routine is called.

If i'm guessing correctly this routine looks something like this:

void timer_routine()
{
	flag = true;
	load_timer(3036);
	start_timer();
}

Then this is the problem. The time until the routine is called is not
zero (due to interrupt latency) and not well defined. Which means,
you get your timer, running for 64k-3036 cycles, then you lose a few
cylcles when the timer_routine is called, and you lose another few cylces
until the timer is started again. 

The right way to do it, would be to set the timer into continous mode,
so that it runs freely and raises an interrupt every 64k-3036 cylces.
Then you only have to set the flag in your timer_routine:

void timer_routine()
{
	flag = true;
}

This way, the timer is independent of the interrupt latency, and the
latency of the timer_routine.

			Attila Kinali

-- 
Why does it take years to find the answers to
the questions one should have asked long ago?



More information about the time-nuts mailing list