1

The following timer code running on a 8Mhz ATMega32u4 should toggle the onboard LED every second, and calling the ISR code manually does so. Yet, the ISR does not seem to get called. Could anyone please help me getting the timer to work?

#define LED_BUILTIN 13


void setup()
{
  cli();

  // set output pin
  DDRC |= (1 << PC7);

  // Don't toggle any pins
  TCCR1A = 0;

  // Clear timer once matched, pre-scaler of 8
  TCCR1B |= ((1 << WGM12) | (1 << CS10) | (1 << CS12));

  TCCR1C = 0;

  // (Clock / (pre-scaler * rate)) - 1
  // (8MHz / (8 * 100)) - 1
  OCR1A = 7800;

  // Enable channel A interrupts
  TIMSK1 |= (1 << OCIE1A);

  // reset interrupt flag
  TIFR1 |= (1 << OCF1A);

  // set counter to 0
  TCNT1 = 0;

  // Enable interrupts globally
  sei();
}

bool on = true;
ISR(TIMER1_COMPA_vect)
{
  // toggle pin
  PORTC ^= (1 << PC7);
}

void loop()
{
  delay(1000);
}
```
Daniel D.
  • 39
  • 2

0 Answers0