Archive for the ‘AVR’ Category

Set up AVR timer interrupt

/* initialize timer interrupt */
void init_isr_timers() {
  /* OC1A/OC1B disconnected */
  TCCR1A = 0;
  /* CTC mode, top in OCR1A / Timer clock = CLK/1024 */
  TCCR1B = (1<<WGM12)|(1<<CS12)|(1<<CS10);
  /* set OCR1A top value for 20Hz (10 periods in one second) */
  /*   0x0168 for CPU @ 7.3728MHz */
  OCR1A = 0x0168;
  /* enable Timer/Counter 1 interrupt */
  TIMSK = (1<<OCIE1A);
}

/* timer/counter 1 interrupt handler */
ISR(TIMER1_COMPA_vect) {
  /* Ohhh, lovely! :) */
}

int main() {
  /* initialize timer interrupt */
  init_isr_timers();
  /* enable interrupts */
  sei();
 
  /* main loop */
  for(;;) {}
}