#include <avr/io.h>     // Definitionen laden 
#include <util/delay.h> // CPU Frequenz einstellen! 
#include <avr/interrupt.h>

#include "checkKeys.h"   // Header einbinden

/*
Taktzahl: 1MHz/300Hz = 3333
Vorteiler: 3333/256 = 13 -> 64 -> 3
OCR0A: 3333/64 -1 = 51
Probe: (51+1)*64 = 3328 ok
*/
unsigned char helligkeit = 0; // Helligkeit der LED

int main(){         // Hauptprogramm 
  PORTB=0x1;        // alle LED0 aus 
  DDRB= 0x1;         // PB0 als Ausgang
  TCCR0A |= 1<<WGM01; // Timer im CTC-Mode
  TCCR0B |= 3;      // Vorteiler 1: Phi/1; 2: Phi/8; 3: Phi/64; 4: Phi/256; 5: Phi/1024
  TIMSK |= 1<<OCIE0A; // Timer/Counter0 Output Compare Match A Interrupt Enable
  OCR0A = 51;       // Wert bei dem der Timer wieder auf 0 gesetzt wird
  sei();            // globale Interruptfreigabe
  while(1){         // Endlosschleife 
    checkKeys(0b11,0b11); // Maske,NLogik
    if(keyEnter&1){ // Taste0 gedrueckt?
      if(helligkeit>0)helligkeit--;
    }
    if(keyEnter&2){ // Taste1 gedrueckt?
      if(helligkeit<3)helligkeit++;
    }
  } 
  return 0; 
}

ISR(TIMER0_COMPA_vect){ // Interrupt Service Routine
  static unsigned char level = 0; // zaehlt die Helligkeitslevel
  if(level>=helligkeit) PORTB|=1; // LED0 aus
  else PORTB&=~1;                 // LED0 an
  level++;
  if (level >= 3) level = 0;
}