@@ -24,23 +24,23 @@ volatile int avr_online_state;
2424
2525void timer0_ISR (void ) interrupt 1
2626{
27- static float avg_button = 100 ;
28- static float avg_avr_online = 100 ;
27+ static float avg_button = 10 ;
28+ static float avg_avr_online = 10 ;
2929
3030 int new_button_state ;
3131 int new_avr_online_state ;
3232
3333 // Increment Counter to shorten overflow time
34- TH0 = (65536 - 1000 )/ 256 ; // TODO: Calculation?! Old: 1ms = 1000MZ, davon das High-Byte in TH0
35- TL0 = (65536 - 1000 )% 256 ; // das Low-Byte in TL0
34+ TH0 = (65536 - 1000 ) / 256 ; // TODO: Calculation?! Old: 1ms = 1000MZ, davon das High-Byte in TH0
35+ TL0 = (65536 - 1000 ) % 256 ; // das Low-Byte in TL0
3636
37- // Button entprellen
38-
39-
40- avg_button = (avg_button * 9 + I_BUTTON * 100 ) / 10 ;
37+ // Button debouncing using exponential moving average
38+ // ? avg = alpha * avg + (1 - alpha) * input, alpha < 1
39+ avg_button = 0.9 * avg_button + 0.1 * ( I_BUTTON * 10 );
40+ // avg_button = (avg_button * 9 + I_BUTTON * 100) / 10;
4141 avg_avr_online = (avg_avr_online * 9 + I_AVR_ONLINE * 100 ) / 10 ;
4242
43- if (avg_button > 50 ) {
43+ if (avg_button > 5 ) {
4444 new_button_state = HIGH ;
4545 } else {
4646 new_button_state = LOW ;
@@ -52,34 +52,29 @@ void timer0_ISR (void) interrupt 1
5252 new_avr_online_state = LOW ;
5353 }
5454
55-
5655 if (new_button_state != button_state ) {
5756 button_state = new_button_state ;
5857 button_changed = 1 ;
58+ } else {
59+ button_changed = 0 ;
5960 }
6061
6162 if (new_avr_online_state != avr_online_state ) {
6263 avr_online_state = new_avr_online_state ;
6364 }
64-
6565}
6666
6767
6868void setup ()
6969{
70- // P33 (I_AVR_ONLINE) to Input only (HiZ)
71- //P3M0 &= ~(0 << 3);
72- //P3M1 |= 1 << 3;
70+ // Keep P33 (I_AVR_ONLINE) and P34 (I_BUTTON) as Quasi-Bidirectional
71+ // since we need the internal PullUps
7372
74- // P34 (I_BUTTON) to Input only (HiZ)
75- //P3M0 &= ~(0 << 4);
76- //P3M1 |= 1 << 4;
77-
78- // P35 (O_BOOST) to PushPull Output
73+ // P35 (O_BOOST): PushPull Output
7974 P3M0 |= 1 << 5 ;
8075 P3M1 &= ~(0 << 5 );
8176
82- // P32 (O_AVR_BUTTON) to PushPull Output
77+ // P32 (O_AVR_BUTTON): PushPull Output
8378 P3M0 |= 1 << 2 ;
8479 P3M1 &= ~(0 << 2 );
8580
@@ -88,19 +83,18 @@ void setup()
8883 button_changed = 0 ;
8984 button_state = HIGH ;
9085 avr_online_state = LOW ;
91-
86+
9287 // Init I/O's
9388 O_AVR_BUTTON = HIGH ;
9489 O_BOOST = LOW ;
95- I_BUTTON = 1 ; // quasi-bidirectional => pullups!
96- I_AVR_ONLINE = 1 ; // quasi-bidirectional => pullups!
90+ I_BUTTON = 1 ; // set to weak high => pullups!
91+ I_AVR_ONLINE = 1 ; // set to weak high => pullups!
9792
9893 // Timers
99- TMOD = (TMOD & 0xF0 ) | 0x01 ; // Set T/C0 Mode 1, 16Bit, No Auto Reload
94+ TMOD = (TMOD & 0xF0 ) | 0x01 ; // Set T/C0 Mode 1, 16Bit, Manual Reload
10095 ET0 = 1 ; // Enable Timer 0 Interrupts
10196 TR0 = 1 ; // Start Timer 0 Running
10297 EA = 1 ; // Global Interrupt Enable
103-
10498}
10599
106100void delay (int ms )
@@ -128,12 +122,11 @@ void main()
128122 {
129123 switch (state ) {
130124 case SLEEP :
131-
132125 // TODO: POWER SAVING OPTIONS
133126 // deactivate timer, enable interrupt2, ...
134127
135128 if (button_changed && button_state == LOW ) {
136- button_changed = 0 ;
129+ // button_changed = 0;
137130 O_AVR_BUTTON = LOW ;
138131 delay (1 ); // otherwise the fw hangs up
139132 O_BOOST = HIGH ;
@@ -145,7 +138,7 @@ void main()
145138
146139 case BOOT :
147140 O_AVR_BUTTON = button_state ;
148-
141+
149142 if (avr_online_state == HIGH )
150143 state = ONLINE ;
151144
@@ -159,7 +152,7 @@ void main()
159152 if (avr_online_state == LOW ) {
160153 O_BOOST = LOW ;
161154 state = SLEEP ;
162- button_changed = 0 ; // clear the button event, otherwise you cannot shut down
155+ // button_changed = 0; // clear the button event, otherwise you cannot shut down
163156 }
164157 break ;
165158
0 commit comments