1 | /************************************************************************/
|
2 | /* */
|
3 | /* Debouncing 8 Keys */
|
4 | /* Sampling 4 Times */
|
5 | /* With Repeat Function */
|
6 | /* */
|
7 | /* Author: Peter Dannegger */
|
8 | /* danni@specs.de */
|
9 | /* */
|
10 | /************************************************************************/
|
11 |
|
12 | #include <stdint.h>
|
13 | #include <avr/io.h>
|
14 | #include <avr/interrupt.h>
|
15 | #include <avr/wdt.h>
|
16 |
|
17 | #ifndef F_CPU
|
18 | #define F_CPU 8000000 // processor clock frequency
|
19 | #warning kein F_CPU definiert
|
20 | #endif
|
21 |
|
22 | #define KEY_DDR DDRA
|
23 | #define KEY_PORT PORTA
|
24 | #define KEY_PIN PINA
|
25 | #define KEY0 0
|
26 | #define KEY1 1
|
27 | #define KEY2 2
|
28 | #define ALL_KEYS (1<<KEY0 | 1<<KEY1 | 1<<KEY2)
|
29 |
|
30 | #define REPEAT_MASK (1<<KEY1 | 1<<KEY2) // repeat: key1, key2
|
31 | #define REPEAT_START 50 // after 500ms
|
32 | #define REPEAT_NEXT 20 // every 200ms
|
33 |
|
34 | #define LED_DDR DDRB
|
35 | #define LED_PORT PORTB
|
36 | #define LED0 0
|
37 | #define LED1 1
|
38 | #define LED2 2
|
39 |
|
40 | volatile uint8_t key_state; // debounced and inverted key state:
|
41 | // bit = 1: key pressed
|
42 | volatile uint8_t key_press; // key press detect
|
43 |
|
44 | volatile uint8_t key_rpt; // key long press and repeat
|
45 |
|
46 |
|
47 | ISR( TIMER0_OVF_vect ) // every 10ms
|
48 | {
|
49 | static uint8_t ct0, ct1, rpt;
|
50 | uint8_t i;
|
51 |
|
52 | TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
|
53 |
|
54 | i = key_state ^ ~KEY_PIN; // key changed ?
|
55 | ct0 = ~( ct0 & i ); // reset or count ct0
|
56 | ct1 = ct0 ^ (ct1 & i); // reset or count ct1
|
57 | i &= ct0 & ct1; // count until roll over ?
|
58 | key_state ^= i; // then toggle debounced state
|
59 | key_press |= key_state & i; // 0->1: key press detect
|
60 |
|
61 | if( (key_state & REPEAT_MASK) == 0 ) // check repeat function
|
62 | rpt = REPEAT_START; // start delay
|
63 | if( --rpt == 0 ){
|
64 | rpt = REPEAT_NEXT; // repeat delay
|
65 | key_rpt |= key_state & REPEAT_MASK;
|
66 | }
|
67 | }
|
68 |
|
69 | ///////////////////////////////////////////////////////////////////
|
70 | //
|
71 | // check if a key has been pressed. Each pressed key is reported
|
72 | // only once
|
73 | //
|
74 | uint8_t get_key_press( uint8_t key_mask )
|
75 | {
|
76 | cli(); // read and clear atomic !
|
77 | key_mask &= key_press; // read key(s)
|
78 | key_press ^= key_mask; // clear key(s)
|
79 | sei();
|
80 | return key_mask;
|
81 | }
|
82 |
|
83 | ///////////////////////////////////////////////////////////////////
|
84 | //
|
85 | // check if a key has been pressed long enough such that the
|
86 | // key repeat functionality kicks in. After a small setup delay
|
87 | // the key is reported being pressed in subsequent calls
|
88 | // to this function. This simulates the user repeatedly
|
89 | // pressing and releasing the key.
|
90 | //
|
91 | uint8_t get_key_rpt( uint8_t key_mask )
|
92 | {
|
93 | cli(); // read and clear atomic !
|
94 | key_mask &= key_rpt; // read key(s)
|
95 | key_rpt ^= key_mask; // clear key(s)
|
96 | sei();
|
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 |
|
116 | void WDT_Prescaler_Change(void)
|
117 | {
|
118 | cli();
|
119 | wdt_reset();
|
120 | /* Start timed equence */
|
121 | WDTCSR |= (1<<WDCE) | (1<<WDE);
|
122 | /* Set new prescaler(time-out) value = 64K cycles (~0.5 s) */
|
123 | WDTCSR = (1<<WDE) | (1<<WDP3) | (1<<WDP0);
|
124 | sei();
|
125 | }
|
126 |
|
127 | void WDT_off(void)
|
128 | {
|
129 | cli();
|
130 | wdt_reset();
|
131 | /* Clear WDRF in MCUSR */
|
132 | MCUSR &= ~(1<<WDRF);
|
133 | /* Write logical one to WDCE and WDE */
|
134 | /* Keep old prescaler setting to prevent unintentional time-out */
|
135 | WDTCSR |= (1<<WDCE) | (1<<WDE);
|
136 | /* Turn off WDT */
|
137 | WDTCSR = 0x00;
|
138 | sei();
|
139 | }
|
140 |
|
141 | void led_lauflicht() {
|
142 | uint8_t i = LED_PORT;
|
143 | i = (i & 0x07) | ((i << 1) & 0xF0);
|
144 | if( i < 0xF0 )
|
145 | i |= 0x08;
|
146 | LED_PORT = i;
|
147 | }
|
148 |
|
149 |
|
150 | int main( void )
|
151 | {
|
152 | WDT_off();
|
153 |
|
154 | LED_DDR = 0xFF;
|
155 | LED_PORT = 0xFF;
|
156 |
|
157 | // Configure debouncing routines
|
158 | KEY_DDR &= ~ALL_KEYS; // configure key port for input
|
159 | KEY_PORT |= ALL_KEYS; // and turn on pull up resistors
|
160 |
|
161 | TCCR0B = (1<<CS02)|(1<<CS00); // divide by 1024
|
162 | TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
|
163 | TIMSK0 |= 1<<TOIE0; // enable timer interrupt
|
164 |
|
165 | sei();
|
166 |
|
167 | while(1){
|
168 |
|
169 | //wdt_reset();
|
170 | if( get_key_short( 1<<KEY1 ))
|
171 | LED_PORT ^= 1<<LED1;
|
172 |
|
173 | if( get_key_long( 1<<KEY1 ))
|
174 | LED_PORT ^= 1<<LED2;
|
175 |
|
176 | //led_lauflicht();
|
177 | // single press and repeat
|
178 |
|
179 | if( get_key_press( 1<<KEY2 ) || get_key_rpt( 1<<KEY2 )){
|
180 | uint8_t i = LED_PORT;
|
181 |
|
182 | i = (i & 0x07) | ((i << 1) & 0xF0);
|
183 | if( i < 0xF0 )
|
184 | i |= 0x08;
|
185 | LED_PORT = i;
|
186 | }
|
187 | }
|
188 | }
|