Exemple de programme timer.c :
#include <avr/io.h> // for the input/output register
#include <avr/interrupt.h>
#define PRESCALER 256
#define NB_TICK 12500
#define CTC1 WGM12 // Better name
int led_bit=0x01;
void init_ports(void){
DDRB |= led_bit;
PORTB |= 0x00;
}
void init_timer(){
TCCR1A = 0; // No output pin connected, no PWM mode enabled
TCCR1B = 1<<CTC1; // No input pin used, clear timer counter on compare match
#if (PRESCALER==8)
TCCR1B |= (1<<CS11);
#endif
#if (PRESCALER==64)
TCCR1B |= (1<<CS11 | 11<<CS10);
#endif
#if (PRESCALER==256)
TCCR1B |= (1<<CS12);
#endif
#if (PRESCALER==1024)
TCCR1B |= (1<<CS12 | 1<<CS10);
#endif
OCR1A = NB_TICK;
TCNT1 = 0;
TIMSK1 = (1<<OCIE1A); // No overflow mode enabled, no input interrupt, output compare interrupt
}
ISR(TIMER1_COMPA_vect){
PORTB ^= led_bit;
}
int main(void){
init_ports();
init_timer();
sei();
return 0;
}