Arduino – blinky with Timer1 COMPA

Next Arduino blinky project. This version of “blinky” example is using Timer1 Compare Match A interrupt to make an onboard LED blink for roughly one second. The code is on Github, click here.

Parts Required

  • Arduino board (i.e. UNO)

Software

This code is written in C++ and can be compiled using an Arduino IDE. All information about how to compile this project is here.

/**
 * Copyright (c) 2019, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>
 * ArduinoUno/003
 * Blinky with Timer1 COMPA.
 */

#define LED_PIN     (13)

void setup() {
  pinMode(LED_PIN, OUTPUT); // set LED pin as output
  TCCR1A = 0; // clear register
  TCCR1B = _BV(WGM12); // set Timer1 to CTC mode
  TCCR1B |= _BV(CS12)|_BV(CS10); // set Timer1 prescaler to 1024
  TIMSK1 |= _BV(OCIE1A); // enable Timer1 COMPA interrupt
  OCR1A = 7812; // set value for Fx=1Hz, OCRnx = (16Mhz/(Fx * 2 * 1024) + 1)
  interrupts(); // enable global interrupts
}

void loop() {
  // do nothing
}

ISR(TIMER1_COMPA_vect) {
  digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle LED pin
}

Leave a Comment