blink

C code posted by iii
created at 23 Feb 20:14

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <avr/io.h>
#include <avr/interrupt.h>

#define LED PC5

#define set_bit(reg, bit) (reg |= (1 << bit))
#define clear_bit(reg, bit) (reg &= ~(1 << bit))
#define toggle_bit(reg, bit) (reg ^= (1 << bit))

ISR (TIMER1_COMPA_vect) {
  toggle_bit(PORTC, LED);
}

void setup_timer() {
  // setup Timer/Counter Control Register
  // as Clear Timer on Compare
  TCCR1B |= 1 << WGM12;

  // use Timer/Counter Control Register
  // to start timer at FCU/64
  TCCR1B |= (1 << CS10) | (1 << CS11);

  // use Output Compare Register
  // to set timer compare value
  OCR1A = 2499;

  // use Timer Interrupt Mask to
  // enable the compare interrupt
  TIMSK1 = 1 << OCIE1A;

  // enable global interrupts
  sei();
}

int main() {
  // enable output to LED
  set_bit(DDRC, LED);

  setup_timer();

  while (1) {
  }

  return 0;
}
866 Bytes in 2 ms with coderay