I have the following code which according to the doc sets sets the timer to CTC(clearTimerOnCompareMatch) mode with a prescale of 128. I basicaly am trying to have the compare ISR fire once every ms. According to the datasheet on page 95 the formula to find the frequency of compare firing is
f=f(i/o)/ 2*prescale* (1+ocr0)
f(i/o) in my case is 16,000,000
therefore f = 16,000,000/2*128*(1+OCR0) and if OCR0 = 125 f = 496 which means it is firing every 2 ms right?
OCR0 needs to be 62 to give me a 1 ms firing right?
If you are using the asynchronous clock source ie 32768hz is the formulae the same? if so a OCR0 of 32 and prescale is 1 then f = 32768/2*1* (1+32) also yields 496hz or every 2ms right?
SIGNAL(SIG_OUTPUT_COMPARE0)
{
ms_count++;
....
}
void
init_timers(void)
{
TIFR |= BV(OCF0);
TCCR0 =
BV(WGM01)|BV(CS02)|BV(CS00); /* CTC, prescale = 128 */
TCNT0 =
0;
TIMSK |= BV(OCIE0);
OCR0 =
125;
}