Timers.ino

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc4000/spring2026/notes/nano/code/Timers.ino
 
volatile bool ledA_state = LOW;
volatile bool ledB_state = LOW;

const int GREEN{6};
const int BLUE{5};


void SetupTimer1() {
  cli();

  TCNT1 = 0;

  TCCR1A = 0;
  TCCR1B = 0;
  // configure the prescaler
  TCCR1B |= (1 << CS12) | (0 << CS11) | (1 << CS10);
  // configure the waveform
  TCCR1B |= (1 << WGM12);

   // set the counter limit
  OCR1A = 15623;

   // enable the interrupt
  TIMSK1 |= (1 << OCIE1A);
 
  sei();
}

void setup() {

  Serial.begin(9600);

  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  
  SetupTimer1();
}

void loop() {
  // put your main code here, to run repeatedly:

}

ISR(TIMER1_COMPB_vect) {
  ledB_state = !ledB_state;
  digitalWrite(BLUE, ledB_state);
}

// define the inturrupt routine.
ISR(TIMER1_COMPA_vect) {
  ledA_state = !ledA_state;
  digitalWrite(GREEN, ledA_state);
}