I've read more information about the timers and now, some questions are puzzling me.
I've set the timer2 prescaler to 64 with:
TCCR2B |= (1<<CS22);
TCCR2B &= ~((1<<CS21) | (1<<CS20));
and I'm using the script below to generate a frequency of 1Hz:
16,000,000 / 64 / (256 - 6) / 1000 = 1
crystal/prescale/(timer - resetting preload)/count variable
ISR(TIMER2_OVF_vect) {
TCNT2 = 6;
int_counter += 1;
if (int_counter == 1000) {
second+=1;
int_counter = 0;
}
};
Unfortunately, what I get is nothing close to 1000ms/cycle. I get around 2018ms/cycle.
First, why the 18ms delay; and second, why is the period double of what It should be?
I have tried other prescalers, like 32 and 128, and what I got is a proportional delay (respectively 9ms and 36ms), with the period still double of what it should (mathematically) be.
Now, I tried to change the Counter resetting preload to different values. Setting TCNT2 to values from 0 (not touching it at all) to 255 (1 tick away from reset...) gave me the following weird results:
at 0 preload, the period is 2040ms.
at 255 preload, the period is 1024ms.
using other preload values produced a linear transition from 1024ms to 2040ms... as if there was another counter running behind the scenes and going through a full cycle before my timer is activated...
is it the TCCR2A running behind the TCCR2B ?.. Now, I'm getting a bit confused