main.c


1
#include <stdint.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include <util/delay.h>
5
#include "key.h"
6
 
7
typedef uint8_t  u08; 
8
volatile uint8_t i;
9
volatile u08 stellung;
10
 
11
12
ISR(TIMER2_OVF_vect) /* veraltet: SIGNAL(SIG_OVERFLOW0) */
13
{
14
u08 countdown;
15
  if (TCCR2 == 5){ // Prescaler 1024 (nach 20ms)
16
    TCNT2=151; //131x weiterzählen +20x für die noch zu verbrauchenden takte der schleife
17
    TCCR2= (1<<CS22); // prescaler 64 für genau 1ms
18
    PORTB |= (1 << PB2); 
19
    
20
  }
21
  else{
22
    for(countdown = 0xff; countdown !=0; countdown --){
23
  
24
      if(stellung == countdown){
25
        PORTB &= ~(1 << PB2); 
26
      }
27
    _delay_us(95);
28
    }
29
    TCNT2 = 100;
30
    TCCR2 = 5; // presc 1024 für 20ms
31
  }
32
}
33
34
35
void initAVR( void ) {
36
  stellung = 0;
37
  
38
  TCCR0 |= (1<<CS02)|(1<<CS00);      // divide by 1024
39
  TCCR2 |= (1<<CS22)|(1<<CS20)|(1<<CS21);      //1024
40
  TIMSK |= (1<<TOIE0)|(1<<TOIE2);        // enable timer interrupt
41
 
42
  KEY_DDR &= ~ALL_KEYS;                
43
  KEY_PORT |= ALL_KEYS;                
44
  DDRB = 0xff;
45
  PORTB = 0x00; //pull-up deaktiviert
46
    sei();
47
} 
48
49
50
51
int main( void )
52
{
53
 
54
 initAVR();
55
 
56
  
57
  
58
  for(;;) {                                       
59
 
60
    if( get_key_press( 1<<KEY0 )){
61
     
62
    stellung= stellung +5;
63
    if(stellung >=255){
64
      stellung = 0;
65
    }
66
  }
67
  
68
 
69
    if( get_key_short( 1<<KEY1 )){
70
      
71
    if(stellung != 0){
72
    stellung = stellung -5;
73
    }
74
    
75
    
76
  }
77
 
78
    
79
  }
80
}