Verwendeter µC: ATMEL AVR 8 Bit RISC.
Systemtakt 1MHz Zylkuszeit 1µs.
void Timer_INIT(){ // Für 8 Bit Timer 0
TCCR0A |= 1<<WGM01; // Timer im CTC-Mode
TCCR0B |= 1; // Vorteiler 1: /1; 2: /8; 3: /64; 4: /256; 5: /1024
TIMSK |= 1<<OCIE0A; // Timer/Counter0 Output Compare Match A Interrupt Enable
OCR0A = 99; // Wert bei dem der Timer wieder auf 0 gesetzt wird
th=15;
sei(); // globale Interrupt-Freigabe
}
void Timer_INIT(){ // Für 16 Bit Timer 1
TCCR1B |= 1<<WGM12; // Timer im CTC-Mode mit OCR1A als Top
TCCR1B |= 1; // Vorteiler 1: /1; 2: /8; 3: /64; 4: /256; 5: /1024
TIMSK |= 1<<OCIE1A; // Timer/Counter1 Output Compare Match A Interrupt Enable
OCR1A = 99; // Wert bei dem der Timer wieder auf 0 gesetzt wird
th=15;
sei(); // globale Interrupt-Freigabe
}
ISR(TIMER0_COMPA_vect){
static char t=0; // persistente lokale Variable
t++;
if(t==th){ // Pulsweite erreicht
PORTB &= ~(1<<PB0); // Servo ist an PB0 angeschlossen, PB0<-0
}
if(t==200){ // Periode um
PORTB |= 1<<PB0; // PB0<-1
t=0;
}
}
ISR(INT0_vect){
th=15;
}