main.c


1
#include <stdint.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
5
#define OC0_F 263 //Hertz
6
#define F_TOSC 32768
7
#define T0_PRE 1
8
9
#define OC0_VAL(_foc) (F_TOSC/(_foc * 2 * T0_PRE) -1)
10
11
int main(void)
12
{
13
  DDRB = (1<<PB5);
14
  
15
  ASSR = (1<<AS0);
16
  OCR0 = OC0_VAL(OC0_F); //preload for CTC
17
  TCCR0 = (1<<WGM01)|(1<<COM00)|(1<<CS00); // Prescaler:1, CTC, toggle on compare
18
/*  wait for timer-tick IF necessary (i.e. you want to change TCCR0/OCR0/TCNT0):
19
  while(ASSR & ((1<<TCN0UB)|(1<<OCR0UB)|(1<<TCR0UB)) )
20
    ;
21
*/
22
  TIMSK = (1<<OCIE0);
23
  sei();
24
  while(1)
25
  {
26
  //  mainloop
27
  }
28
  return 0;
29
}
30
ISR(TIMER0_COMP_vect)
31
{
32
  PORTB ^= (1<<PB5);
33
}