I'm currently using an ATmega32 to keep time, so I carefully read the datasheet and configured the Timer 1 to generate an interrupt each second.
According to the following formula on the datasheet, using a 16 MHz clock, a prescaler of 256, and OCR1A = 31249, I should get a frequency of 1 Hz in CTC mode:
\$ f_{OCnA} = \frac{f_{clk\_I/O}}{2\: \cdot \: N \cdot \: (1 \: + \: OCRnA)} \$
This formula is available on page 99 of the ATmega32 datasheet.
I've configured Timer 1 as follows:
TCCR1A = 0;
TCCR1B = (1<<WGM12)|(1<<CS12);
OCR1A = 31249;
TIMSK = (1<<OCIE1A);
Here is the interrupt code
ISR(TIMER1_COMPA_vect){
sec++;
}
Then, I use an LCD to continuously print the value of sec, and I get a timing of 0.5 seconds. In other words, for each second that passes, the timer generates two interrupts.
At first I thought maybe I got the fuses wrong, but that doesn't seem to be the case, as delays work just fine. Anyway, here's how I configured the fuses:
-U lfuse:w:0xff:m -U hfuse:w:0x99:m
I have absolutely no idea of what's going on, I've double checked everything, tried another ATmega32, tried another 16 MHz crystal oscillator, and I still get the same result.
Is the datasheet wrong, or am I missing something here?