#include <avr/io.h>     // Definitionen laden 
#include <util/delay.h> // CPU Frequenz einstellen! 
#include <avr/interrupt.h>

#include "checkKeys.h"   // Header einbinden

/*
Taktzahl: 1MHz/1500Hz = 667
Vorteiler: 667/256 = 2,6 -> 8 -> 2
OCR0A: 667/8 -1 = 82
Probe: (82+1)*8 = 664 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 |= 2;      // 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 = 82;       // 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<15)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 >= 15) level = 0;
}