1

I am trying to find a simple timer0 interrupt example, but none of those work. Neither this code which I tried to run:

boolean toggle0 =0;
void setup() {
pinMode(8, OUTPUT);
 cli();
 // Set timer1 interrupt at 1 Hz
 TCCR1A = 0; // Set entire TCCR1A register to 0
 TCCR1B = 0; // Same for TCCR1B
 TCNT1 = 0; // Initialize counter value to 0
 // Set compare match register for 1 Hz increments
 OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
 // Turn on CTC mode
 TCCR1B |= (1 << WGM12);
 // Set CS12 and CS10 bits for 1024 prescaler
 TCCR1B |= (1 << CS12) | (1 << CS10);
 // Enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 sei();
}
ISR(TIMER0_COMPA_vect){ // Change the 0 to 1 for timer1 and 2 for timer2
 if (toggle0){
 digitalWrite(8,HIGH);
 toggle0 = 0;
 }
 else{
 digitalWrite(8,LOW);
 toggle0 = 1;
 }
}
void loop() {
}

What am I doing wrong?

asked Dec 12, 2016 at 21:03
1
  • You know you don't need to use interrupts for this. You can instruct the timer to change the output pin when the counter overflow or reaches OCR1A. You do this with the COM1A1 COM1A0 bit in the TCCR1A register. Though you'd have to use pin 9 (or 10) instead of pin 8. Commented Dec 13, 2016 at 11:38

2 Answers 2

1

The main problem is you are using the wrong interrupt handler. You are setting up Timer1, but the interrupt handler is for Timer0:

boolean toggle0 = 0;
void setup() {
pinMode(8, OUTPUT);
 cli();
//set timer1 interrupt at 1Hz
 TCCR1A = 0;// set entire TCCR1A register to 0
 TCCR1B = 0;// same for TCCR1B
 TCNT1 = 0;//initialize counter value to 0
 // set compare match register for 1hz increments
 OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
 // turn on CTC mode
 TCCR1B |= (1 << WGM12);
 // Set CS12 and CS10 bits for 1024 prescaler
 TCCR1B |= (1 << CS12) | (1 << CS10);
 // enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 sei();
}
ISR(TIMER1_COMPA_vect){ //change the 0 to 1 for timer1 and 2 for timer2
 toggle0 = !toggle0;
 digitalWrite(8, toggle0);
}
void loop() {
}
answered Dec 12, 2016 at 21:17
2
  • Thank you kindly, can you please update it to work on a timer0 intrerupt? Commented Dec 12, 2016 at 21:44
  • @adrya407 I wouldn't recommend it, as the Timer0 is used for millis(), delays and so on. If you change ticks from 1.024ms (overflow interrupt), to CTC with exactly 1s (compare match interrupt), then overflow will not work anymore at all. Commented Dec 12, 2016 at 21:51
0

Arduino timers are reserved for buid-in functions:

Timer0 is reserved fire a millisecond interrupt for the millisecond counter Timer1 is reserved for measuring time passed since the last reboot Timer2 is reserved for pwm timing

So, using these timers is not a good suggestion if you plan to use above options. For me, Timer0 seems a good choice, as microsecond timing is a bit of an overkill.

Here you may find your solution: https://learn.adafruit.com/multi-tasking-the-arduino-part-2/timers

answered Feb 17, 2023 at 15:36
1
  • 4
    On the UNO all three of these timers are involved in PWM timing. Where the claim about Timer1 come from? Commented Feb 17, 2023 at 16:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.