[time-nuts] Clock Correction Algorithm
Miguel Gonçalves
mail at miguelgoncalves.com
Thu Nov 24 16:32:04 UTC 2011
Hi Atilla!
On 24/11/2011, at 14:42, Attila Kinali <attila at kinali.ch> wrote:
> On Thu, 24 Nov 2011 14:21:37 +0000
> Miguel Gonçalves <mail at miguelgoncalves.com> wrote:
>
>
>> I am using an Arduino Uno that presumably is running at 16 MHz.
>>
>> I am using a 16 bit timer with a 256 pre-scaler:
>>
>> 16000000 / 256 = 62500 Hz
>>
>> 1 / 62500 = 16 us
>>
>> 65536 - 62500 = 3036
>>
>> I am setting the counter to 3036 and let it overflow after 65535.
>
> 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.
In my main loop I just have something like
while (true) {
while (!flag) {}
/* a second has passed... this code is executed between seconds */
flag = false;
}
> How do you check for the overflow? If you check a flag in your main loop,
> this would explain the "huge" difference, as this polling will add a
> (not so) constant delay each time the timer overflows, until you start
> your timer again.
>
I have to check this flag. I do all the processing between seconds. I believe the interrupt service routine should be kept as short as possible.
Also I checked with a simple counter and the above loop (the inner one) is executed 480000 times per second. I also know that if I don't measure this it will run much more. Right?
Thanks for your help!
Cheers,
Miguel
More information about the Time-nuts_lists.febo.com
mailing list