Hi
I cannot understand why this is not interrupting at 1Hz:
void setup() {
DDRB |= B00100000; // pin 13 output
PORTB &= ~B00100000; // pin 13 LOW
cli();
TCNT1 = 0;
OCR1A = 15624; // = 16M / (1Hz * 1024) - 1
TCCR1A = 0;
TCCR1B = 0;
TCCR1B |= _BV(WGM12); // mode 4 => CTC with TOP=OCR1A
TCCR1B |= _BV(CS12) | _BV(CS10); // 1024 prescaler
TIMSK1 |= _BV(OCIE1A); // activate interrupt when TCNT1=OCR1A
sei();
}
ISR(TIMER1_COMPA_vect){ // should interrupt @1Hz (but is right now above 1kHz)
PORTB ^= B00100000; // switch pin 13
}
void loop() {
}
That is using Mode 4 of Timer1, it calls the interrupt but a lot faster (above 1kHz).
Trying to use Mode 12 works fine
void setup() {
DDRB |= B00100000; // pin 13 output
PORTB &= ~B00100000; // pin 13 LOW
cli();
TCNT1 = 0;
ICR1 = 15624; // = 16M / (1Hz * 1024) - 1
TCCR1A = 0;
TCCR1B = 0;
TCCR1B |= _BV(WGM12) | _BV(WGM13); // mode 12 => CTC with TOP=ICR1
TCCR1B |= _BV(CS12) | _BV(CS10); // 1024 prescaler
TIMSK1 |= _BV(ICIE1); // activate interrupt when TCNT=ICR1
sei();
}
ISR(TIMER1_CAPT_vect){ // interrupts @1Hz
PORTB ^= B00100000; // switch pin 13
}
void loop() {
}
Hoping someone can clarify what I missed here!
Thank you in advance!
PS: I am not trying to switch the LED 13 on and off - this is just so I can see whether the timer is working or not