/* 4 MHz Systemtakt, 8Bit-Timer 0, Overflow-Interrupt, Blinken mit 1 Hz (0,5s an; 0,5s aus) an PB0
Taktzahl:      Phi*Sollzeit = 4Mhz*500ms = 2.000.000
Vorteiler:     Taktzahl/Werteanzahl = 2.000.000/256 = 7813! => 1024
ISRTeilfaktor: Taktzahl/Werteanzahl/Vorteiler = 2.000.000/256/1024 = 7,6 Aufrunden: 8
Vorsteller:    Werteanzahl - Taktzahl/Vorteiler/ISRTeilfaktor = 256 - 2.000.000/1024/8 = 11,9 => 12
Probe:         (Werteanzahl-Vorsteller)*Vorteiler/Phi*ISRTeilfaktor =(256-12)*1024/4MHz*8 = 499,7 ms
*/
#include <avr/io.h>
#include <avr/interrupt.h>

int main(){          // Hauptprogramm
  PORTB = 0xff;
  DDRB = 0xff;
  TCCR0B = 5;        // Systemtakt durch 1024
  TIMSK |= 1<<TOIE0; // TimerOverflowInterruptEnable0
  sei();             // globale Interruptfreigabe
  while(1);          // Endlosschleife
  return 0;
}

ISR(TIMER0_OVF_vect){ // Interrupt Service Routine
  static unsigned char teiler=0;
  TCNT0 = 12;         // wieder vorstellen
  teiler++;
  if(teiler>=8){      // Teilfaktor in ISR
    teiler=0;
    PORTB ^= 1;           // Ausgang PB0 invertieren (PINB = 1)
  }
}