Hello,
I'd like to implement a ten-millisecond counter on my MCU so that the cycle count representing ten milliseconds could be arbitrary. I need this to account for inaccuracies in the internal clock frequency.
For example my bus frequency is 36.864.000Hz and each 10ms period would be represented by 368.640 cycles. I get a reference time from a GPS module and I'd like to change this cycle count accordingly.
My problem is I don't know how to build a timer mechanism that would fire every n cycles, where n can be over 65536. I've tried to use PIT but it needs the timeout to be n * m cycles so a cycle count that is e.g. a prime number cannot be used and the resolution in general would be the minimum of n and m.
Using ECT this could be achieved by dynamically adjusting the compare register with the modulus of the desired cycle count but it seems inaccurate and inelegant.
Kind regards,
Seppo
Solved! Go to Solution.
Is ECT not accurate? How? Do you mean you are going to prescale ECT clock? Just use least 1:1 prescaler and you won't lose accuracy or ECT resolution. Arbitrary timeout periods can be both generated and measured using ECT. Resolution and accuracy is as good as bus clock.
isr_10ms()
{
static short ect_overflow_counter;
if(ect_overflow_counter)
{
ect_overflow_counter--;
}
else
{
ect_overflow_counter = 368640L / 65536;
TCx += 368640L % 65536;
increment_10ms_counter();
}
clear_TCx_flag();
}
Is ECT not accurate? How? Do you mean you are going to prescale ECT clock? Just use least 1:1 prescaler and you won't lose accuracy or ECT resolution. Arbitrary timeout periods can be both generated and measured using ECT. Resolution and accuracy is as good as bus clock.
isr_10ms()
{
static short ect_overflow_counter;
if(ect_overflow_counter)
{
ect_overflow_counter--;
}
else
{
ect_overflow_counter = 368640L / 65536;
TCx += 368640L % 65536;
increment_10ms_counter();
}
clear_TCx_flag();
}
Perfect.
Thank you!