I need a timer to generate interrupts every 10 ms, so I can implement a simple RTOS on the Zumo 32u4.
I already looked at the Zumo 32U4 Robot User’s Guide 8 “AVR Timers” section and decided I could live without the buzzer, so picked Timer-4 for my purpose.
Using the ATmega32U4 datasheet and other resources, I came up with the following code:
#define BSP_TICKS_PER_SEC 100
ISR(TIMER4_COMPA_vect) {
OS_ProcessTick(0); // process time events for tick rate 0
}
void onStartup()
{
TCCR4A = 0U;
// Prescale by 1/1024
TCCR4B = (1U << CS43 ) | (0U << CS42) | (1U << CS41) | (1U << CS40);
// Enable TIMER compare Interrupt for OCR4A
TIMSK4 = (1U << OCIE4A);
// Count up until you get the equivalent of BSP_TICKS_PER_SEC.
// This is just a convenient way to say later if I want
OCR4A = (F_CPU / BSP_TICKS_PER_SEC / 1024U) - 1;
// Timer/Counter 4 Interrupt(s) initialization
TIMSK4 = (0<<OCIE4D) | (1<<OCIE4A) | (0<<OCIE4B) | (0<<TOIE4);
TCNT4 = 0U;
}
I am driving an LED (using ledRed() function) in ProcessTick(). I switching on the LED for what I think should be one second (100 ticks), then switching off for one second (100 ticks). The frequency that I observe with the LED is actually about 1.5 seconds. Where am I making the error?
Thanks!