I’m trying to get the demo ISR program to work on a NANO board. The code compiles and downloads OK. I have verified that the code runs by turning on the LED. However, it appears that the interrupt service routine is not activated.
bool ledOn = false;
const unsigned long TIME_TICKS = 2000; // for 100 ms interrupts at 48 MHz
const bool PRESCALER = B00000011; // Prescaler of 64
const bool ENABLE_ISR = B00000010; // Enable the ISR to generate an interrupt
ISR (TIMER1_COMPA_vect)
{
ledOn = !ledOn; // toggle led
if (ledOn) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
} // End of ISR
void setup()
{
// flash the LED at the interrupt rate
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
ledOn = false;
TCCR1A = 0; // Init Timer1A
TCCR1B = 0; // Init Timer1B
TCCR1B |= PRESCALER; // Set Prescaler
OCR1A = TIME_TICKS; // Timer Compare1A Register
TIMSK1 |= ENABLE_ISR; // Enable Timer COMPA Interrupt
}
void loop()
{
// set up main function here
}
Probably not necessary, since there isn't a ISR vs main path conflict.
This however is quite important. Assigning any non-zero value to a "bool" will result in a value of 1 (true), so you'll have the wrong bit pattern.
Also not that you probably don't want "normal" mode (TCCR1A = 1B = 0), since that won't reset the count when you hit the match; the counter will keep going until it overflows. (OTOH, a count of 2000 won't be visible to the human eye.)