Switch de-bounce without delays

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Switch de-bounce without delays

Post by howard123 »

Hi all..
It may be required to use a push button de-bounce routine without the use of delay functions when timing restraints dictate.

This code can either run freely in the main loop or be called by a timed interrupt. I have used three buttons but a different quantity could be used by duplication of sections at the top and bottom of the routine.

The first section may not be required as shown and could be simpler. I have used it this way as my switches were not all on the same port. The next section is an up/down counter which collects the switch status and by counting, determines when the switch is bounce-free. The next section determines at which counts ON and OFF are positioned. These count values (0&5) are different to provide some level of hysteresis.

For my application the above settings had a maximum count value of about 10 and a hysteresis value of 5. This worked well for me but other values could be used depending on the characteristics of the switch and how often the routine runs. I have found that switches degrade in time and get more bouncy so try to get this to values where pushing a button produces the slightest discernible delay and that should be good enough. If this runs in the main loop you may have to adjust these values as you add additional code to your project, and the buttons become sluggish.

The next section is a leading edge detector which produces just one pulse rather that a continuous high when a button is pressed. This could also be changed or left out depending on how the switch is required to operate. The last section determines which output is valid.
Attachments
Switch de_bounce without delays.fcf
V5
(41.53 KiB) Downloaded 418 times

Post Reply