spacepaste

  1.  
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. void timer_init(void) {
  5. // wafeform generation mode bit, see page160
  6. // CTC Mode, TOP=OCR2A
  7. TCCR2A |= (1<<WGM21);
  8. // top value
  9. OCR2A = 15625;
  10. // set OC2A on compare match - from original code...useless?
  11. TCCR2A |= (1<<COM2A0) | (1<<COM2A1);
  12. // prescalar: 1024
  13. TCCR2B |= (1<<CS22) | (1<<CS21) | (1<<CS20);
  14. // if timer2 reaches the value in OCR2A, fire interrupt
  15. TIMSK2 |= (1<<OCIE2A);
  16. }
  17. #define LED1 PB0
  18. ISR(TIMER2_COMPA_vect) {
  19. PORTB ^= (1 << LED1); // toggle LED1
  20. }
  21. int main (void) {
  22. timer_init();
  23. sei();
  24. for (;;) {
  25. }
  26. }
  27.