ITG-ABI 21/22 26.04.2022 Teil 1 Lösungsvorschlag Aufgabe 2 Drehimpulsgeber und Servo

2.2 Mikrocontroller Teil

Verwendeter µC: ATMEL AVR 8 Bit RISC.

Zuordnungen zum µC ATmega16

Systemtakt 1MHz Zylkuszeit 1µs.

2.2.2 Timerinit 3P

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
}

2.2.4 Timer-ISR 4P

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;
  }
}

2.2.5 INT0-ISR 2P

ISR(INT0_vect){
  th=15;
}