Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created February 20, 2018 18:42
Show Gist options
  • Select an option

  • Save ShawnHymel/d36e527928994150f5fc3cba20dd2161 to your computer and use it in GitHub Desktop.

Select an option

Save ShawnHymel/d36e527928994150f5fc3cba20dd2161 to your computer and use it in GitHub Desktop.
Blinky with Timer Interrupt
// Pins
const int led_pin = PB5;
// Counter and compare values
const uint16_t t1_load = 0;
const uint16_t t1_comp = 31250;
void setup() {
// Set LED pin to be output
DDRB |= (1 << led_pin);
// Reset Timer1 Control Reg A
TCCR1A = 0;
// Set CTC mode
TCCR1B &= ~(1 << WGM13);
TCCR1B |= (1 << WGM12);
// Set to prescaler of 256
TCCR1B |= (1 << CS12);
TCCR1B &= ~(1 << CS11);
TCCR1B &= ~(1 << CS10);
// Reset Timer1 and set compare value
TCNT1 = t1_load;
OCR1A = t1_comp;
// Enable Timer1 overflow interrupt
TIMSK1 = (1 << OCIE1A);
// Enable global interrupts
sei();
}
void loop() {
delay(500);
}
ISR(TIMER1_COMPA_vect) {
PORTB ^= (1 << led_pin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment