Simple AVR Button Debounce
14
Jan
I needed a simple debounce for a push button wired directly to a digital input on an ATMega. I’m sure there are more advanced techniques but this is working well for me.
1 2 3 4 5 6 7 8 9 10 |
int ButtonPressed() { /* the button is pressed when BUTTON_BIT is clear */ if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) { delay_ms(DEBOUNCE_TIME); if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) return 1; } return 0; } |
Where bit_is_clear is defined in AVR-LIBC IO.H … Read More »