Software Switch Debouncing

Words
112
Reading
1 min
Listen
Play
8y

How can we easily debounce those pesky mechanical switches without using external components and without tying up processor cycles in a wait loop?
The answer is easy. We can use a virtual shift register. The I/O pin the switch is connected to is sampled at periodic intervals and its logic level at that instant is shifted into a virtual shift register. Once all bits of the shift register are 0 or 1, the switch is considered to have settled into an on or off state, respectively.

The flow diagram to debounce the switches (code omitted to keep it architecture-independent):

Debounce.png

The flow diagram and code to process the output of the debouncing function:

Debouncing.png

Software Switch Debouncing | Ecency