File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <avr/io.h>
2
+ #include <util/delay.h>
3
+
4
+ #define is_pressed (reg , pos ) ((reg & (1 << pos)) == 0)
5
+
6
+ void set_bit (volatile uint8_t * reg , uint8_t pos , uint8_t value ) {
7
+ if (value == 0 ) return ;
8
+ * reg |= (1 << pos );
9
+ }
10
+
11
+ uint8_t on_press (uint8_t state , uint8_t reg , uint8_t pos , uint8_t action ) {
12
+ static uint8_t nb ;
13
+
14
+ if (state == 0 && is_pressed (reg , pos )) {
15
+ _delay_ms (20 );
16
+ state = 1 ;
17
+ nb += action ;
18
+ nb %= 16 ;
19
+ // Clear all led
20
+ PORTB &= (0xFF << 3 ) & ~(1 << 4 ); // 0b1111 1000 & 0x1110 1111
21
+ set_bit (& PORTB , PIN0 , nb & 1 );
22
+ set_bit (& PORTB , PIN1 , nb & 2 );
23
+ set_bit (& PORTB , PIN2 , nb & 4 );
24
+ set_bit (& PORTB , PIN4 , nb & 8 );
25
+ }
26
+ if (state == 1 && !is_pressed (reg , pos )) {
27
+ _delay_ms (20 );
28
+ state = 0 ;
29
+ }
30
+ return state ;
31
+ }
32
+ int main () {
33
+
34
+ uint8_t sw1_pressed = 0 ;
35
+ uint8_t sw2_pressed = 0 ;
36
+
37
+ DDRB = 0xFF | (1 << PIN4 );
38
+
39
+ while (1 ) {
40
+ sw1_pressed = on_press (sw1_pressed , PIND , 2 , 1 );
41
+ sw2_pressed = on_press (sw2_pressed , PIND , 4 , -1 );
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments