I'm trying to use timer3 on an Arduino Mega 2560 in CTC mode. However, enabling CTC disables the code entirely.
Here is the function I call in setup() to setup the times.
void SetupInterrerupt() {
cli(); // Disable interrupts
// Setup timer
TCCR3A = 0; // set entire TCCR3A register to 0
TCCR3B = 0; // same for TCCR3B
// Setup compare match register
OCR3B = CMP_FAST; // CMP_FAST = 40
// turn on CTC mode:
TCCR3B |= (1 << WGM32); // Commenting out this line makes the code work
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR3B |= (1 << CS30) | (1 << CS32);
// enable timer compare interrupt:
TIMSK3 |= (1 << OCIE3B);
sei(); // Enable interrupts
Serial.println("Timers enabled");
}
And the interrupt vector.
// Timer 3 Interrupt
ISR(TIMER3_COMPB_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
// do stuff...
}
As is, the interrupt doesn't execute at all. As noted in the code, commenting out "TCCR3B |= (1 << WGM32);" actually executes the interrupt, except the frequency is more like 1Hz instead of 720.
I'm using the arduino-lmic library, as well as some other data structure libraries. I don't think they use any timers, but since lmic is designed for Uno's, I figured that using timers 3 and up would be fine.
I'm a little rusty on my timers unfortunately. Is anyone able to spot my mistake?
Thanks.