1 | #include <stdint.h>
|
2 | #include <avr/io.h>
|
3 | #include <avr/delay.h>
|
4 | #include <avr/interrupt.h>
|
5 |
|
6 | #ifndef F_CPU
|
7 | #define F_CPU 1000000 // processor clock frequency
|
8 | #warning kein F_CPU definiert
|
9 | #endif
|
10 |
|
11 | #define KEY_DDR DDRD
|
12 | #define KEY_PORT PORTD
|
13 | #define KEY_PIN PIND
|
14 | #define KEY0 0
|
15 | #define KEY1 1
|
16 | #define KEY2 2
|
17 | #define ALL_KEYS (1<<KEY0 | 1<<KEY1 | 1<<KEY2)
|
18 |
|
19 | #define REPEAT_MASK (1<<KEY1 | 1<<KEY2) // repeat: key1, key2
|
20 | #define REPEAT_START 50 // after 500ms
|
21 | #define REPEAT_NEXT 20 // every 200ms
|
22 |
|
23 | #define LED_DDR DDRB
|
24 | #define LED_PORT PORTB
|
25 | #define LED0 1
|
26 | #define LED1 2
|
27 | #define LED2 3
|
28 |
|
29 | volatile uint8_t key_state; // debounced and inverted key state:
|
30 | // bit = 1: key pressed
|
31 | volatile uint8_t key_press; // key press detect
|
32 |
|
33 | volatile uint8_t key_rpt; // key long press and repeat
|
34 |
|
35 |
|
36 | ISR( TIMER0_OVF_vect ) // every 10ms
|
37 | {
|
38 | static uint8_t ct0, ct1, rpt;
|
39 | uint8_t i;
|
40 |
|
41 | TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
|
42 |
|
43 | i = key_state ^ ~KEY_PIN; // key changed ?
|
44 | ct0 = ~( ct0 & i ); // reset or count ct0
|
45 | ct1 = ct0 ^ (ct1 & i); // reset or count ct1
|
46 | i &= ct0 & ct1; // count until roll over ?
|
47 | key_state ^= i; // then toggle debounced state
|
48 | key_press |= key_state & i; // 0->1: key press detect
|
49 |
|
50 | if( (key_state & REPEAT_MASK) == 0 ) // check repeat function
|
51 | rpt = REPEAT_START; // start delay
|
52 | if( --rpt == 0 ){
|
53 | rpt = REPEAT_NEXT; // repeat delay
|
54 | key_rpt |= key_state & REPEAT_MASK;
|
55 | }
|
56 | }
|
57 |
|
58 | ///////////////////////////////////////////////////////////////////
|
59 | //
|
60 | // check if a key has been pressed. Each pressed key is reported
|
61 | // only once
|
62 | //
|
63 | uint8_t get_key_press( uint8_t key_mask )
|
64 | {
|
65 | cli(); // read and clear atomic !
|
66 | key_mask &= key_press; // read key(s)
|
67 | key_press ^= key_mask; // clear key(s)
|
68 | sei();
|
69 | return key_mask;
|
70 | }
|
71 |
|
72 | ///////////////////////////////////////////////////////////////////
|
73 | //
|
74 | // check if a key has been pressed long enough such that the
|
75 | // key repeat functionality kicks in. After a small setup delay
|
76 | // the key is reported being pressed in subsequent calls
|
77 | // to this function. This simulates the user repeatedly
|
78 | // pressing and releasing the key.
|
79 | //
|
80 | uint8_t get_key_rpt( uint8_t key_mask )
|
81 | {
|
82 | cli(); // read and clear atomic !
|
83 | key_mask &= key_rpt; // read key(s)
|
84 | key_rpt ^= key_mask; // clear key(s)
|
85 | sei();
|
86 | return key_mask;
|
87 | }
|
88 |
|
89 | ///////////////////////////////////////////////////////////////////
|
90 | //
|
91 | // check if a key is pressed right now
|
92 | //
|
93 | uint8_t get_key_state( uint8_t key_mask )
|
94 |
|
95 | {
|
96 | key_mask &= key_state;
|
97 | return key_mask;
|
98 | }
|
99 |
|
100 | ///////////////////////////////////////////////////////////////////
|
101 | //
|
102 | uint8_t get_key_short( uint8_t key_mask )
|
103 | {
|
104 | cli(); // read key state and key press atomic !
|
105 | return get_key_press( ~key_state & key_mask );
|
106 | }
|
107 |
|
108 | ///////////////////////////////////////////////////////////////////
|
109 | //
|
110 | uint8_t get_key_long( uint8_t key_mask )
|
111 | {
|
112 | return get_key_press( get_key_rpt( key_mask ));
|
113 | }
|
114 |
|
115 | int main( void )
|
116 | { volatile int counter=0;
|
117 | LED_PORT = 0x00;
|
118 | LED_DDR = 0xFF;
|
119 |
|
120 | // Configure debouncing routines
|
121 | KEY_DDR &= ~ALL_KEYS; // configure key port for input
|
122 | KEY_PORT |= ALL_KEYS; // and turn on pull up resistors
|
123 |
|
124 | TCCR0 = (1<<CS02)|(1<<CS00); // divide by 1024
|
125 | TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
|
126 | TIMSK |= 1<<TOIE0; // enable timer interrupt
|
127 |
|
128 | sei();
|
129 |
|
130 | while(1){
|
131 | if( get_key_short( 1<<KEY1 ))
|
132 | counter++;
|
133 |
|
134 |
|
135 | if( get_key_long( 1<<KEY1 ))
|
136 | counter=0;
|
137 |
|
138 |
|
139 | if(counter==0)
|
140 | LED_PORT = 0x00;
|
141 |
|
142 |
|
143 | if(counter==1)
|
144 | LED_PORT |= 1<<LED0;
|
145 |
|
146 |
|
147 | if(counter==2)
|
148 | LED_PORT |= (1<<LED0)| (1<<LED1);
|
149 |
|
150 |
|
151 | if(counter==3)
|
152 | LED_PORT |= (1<<LED0)| (1<<LED1) | (1<<LED2);
|
153 | _delay_ms(500);
|
154 | LED_PORT ^= (1<<LED0)| (1<<LED1) | (1<<LED2);
|
155 |
|
156 |
|
157 | }
|
158 | }
|