I am playing with a basic timer 1 program on an Arduino UNO that’s suppose to blink the on-board LED in so that I can understand exactly how to set up timer interrupts. When I run this, the LED never blinks (the ISR is apparently never called) and setup() seems to repeatedly be called (the "Init..." message is continuously sent to the serial monitor). I assume that some error is causing the board to repeatedly be reset.
Can someone take a look at this to see if I’m missing anything? By the way, when I use the Timer1 class it works fine, therefore I assume it’s something I’m doing (or not doing) rather than some hardware issue.
Attached is a copy of the code.
Thanks,
Jack
TimerInterruptTest1.ino (622 Bytes)
The line "#if 0" means "Throw away all the lines between here and #endif". No wonder the code doesn't do what you expect!
void setup()
{
Serial.begin(115200);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
noInterrupts(); // disable all interrupts
#if 0
TCCR1A = 0;
TCCR1B = 0;
OCR1A = 15264;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS10); // 1024 prescaler
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
#endif
interrupts(); // enable all interrupts
Serial.println("Init...");
}
void loop()
{
}
ISR(Timer1_COMPA_vect) // timer compare interrupt service routine
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
I don't know if the OP has changed his code or not, but I see #if 1.
The issue I see with the code is that the syntax for the ISR is wrong
//ISR(Timer1_COMPA_vect)
ISR(TIMER1_COMPA_vect)
void setup()
{
Serial.begin(115200);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
noInterrupts(); // disable all interrupts
#if 1
TCCR1A = 0;
TCCR1B = 0;
OCR1A = 15264;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS10); // 1024 prescaler
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
#endif
interrupts(); // enable all interrupts
Serial.println("Init...");
}
void loop()
{
}
//ISR(Timer1_COMPA_vect) // timer compare interrupt service routine
ISR(TIMER1_COMPA_vect)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
Cattledog - Yes that was it - Thank you very much. Good Catch! I looked at the code and looked at the code and looked at it again, and just didn't see it. I'm surprised the compiler didn't complain about that typo.
Re: The #if 0... Yes that was in the example I originally posted (a remnant of my debugging), but I changed it after reviewing my post. However having said that, that code snippet being turned off would not have explained the apparent constant resets I was seeing.
As an aside, how did you guys insert the code in your reply vs as an attachment? That makes a post much more readable.
Thanks again,
Jack