Path: EDN Asia >> Design Centre >> IC/Board/Systems Design >> Guide to reusable debounce (and other) code
IC/Board/Systems Design Share print

Guide to reusable debounce (and other) code

15 May 2015  | Jacob Beningo

Share this page with your friends

Step #3: Identify the button states
Every application will have different requirements on what states a button may occupy. Very simple applications might only have NOT_PRESSED and PRESSED. A complex application may have far more states, such detecting when a button is released or how many times it has been pressed within a defined time window. A properly defined API doesn't care about these different states but can easily handle all the possible states in the implementation or through the configuration layer. An example enumeration of the possible button states for a typical application can be seen in figure 3.

Figure 3: An example of possible button states for an application.

Step #4: Select the implementation type
Defining an API is a great way to abstract out the implementation details of how a button debounce algorithm works, but there comes a time when a developer needs to decide what those details will be. There are many different ways to debounce a button in software. The simplest is to read the pin state, delay for some debounce period, and then read the pin again. Using the delay method isn't particularly efficient, however, nor is it always correct. More common implementations for a button debounce will periodically sample the pin and only after "x" samples have consecutively read the same will the button be considered debounced. The implementation you choose can be dependent upon the initial requirements and the target application.

Step #5: Flowchart the design
The easiest way to get a handle on the implementation details is to flowchart the implementation before ever writing a single line of code. Flowcharting allows a developer to wrap their mind around the code that they are about to write. The temptation always exists to bypass flowcharting and instead jump straight to writing code. Writing code first, however, always results in having to rewrite the code over and over. Figure 4 shows some example flowcharts for some common button debouncing algorithms.

Figure 4: Button debounce implementation flowcharts.

Step #6 – Now, write the code
Writing the embedded software should only occur after the API and the flowchart have been developed. With the flowchart in hand, writing the embedded software becomes as easy as translating the flowchart into the developer's language of choice. An example of the Button_Debounce function can be seen in figure 5.

Figure 5: Button_Debounce implementation.

Step #7: Test and validate
Finally, after working so hard on the previous six steps, it is now time to test and validate that the selected implementation and design actually work. One way to test the debounce algorithm is to pick a low cost development kit, such as an STM32 Nucleo board, and write some application code that will debounce the onboard button and turn an LED on. For a test keep it simple and turn the LED on if the button is debounced as being in the pressed state, and turn the LED off otherwise.

Writing code that is reusable doesn't need to take more time or cost more than code written to be a one-off. Spending a little bit of time up front to think through an API, select an implementation, and flowchart the design will save more time on the back end than this upfront investment. These steps have demonstrated how a reusable button algorithm can be developed but these same steps and strategies can also be applied to nearly any component that a developer would use or develop for an embedded system.

About the author
Jacob Beningo is a Certified Software Development Professional (CSDP) whose expertise is in embedded software. He works with companies to decrease costs and time to market while maintaining a quality and robust product.

 First Page Previous Page 1 • 2

Want to more of this to be delivered to you for FREE?

Subscribe to EDN Asia alerts and receive the latest design ideas and product news in your inbox.

Got to make sure you're not a robot. Please enter the code displayed on the right.

Time to activate your subscription - it's easy!

We have sent an activate request to your registerd e-email. Simply click on the link to activate your subscription.

We're doing this to protect your privacy and ensure you successfully receive your e-mail alerts.

Add New Comment
Visitor (To avoid code verification, simply login or register with us. It is fast and free!)
*Verify code:
Tech Impact

Regional Roundup
Control this smart glass with the blink of an eye
K-Glass 2 detects users' eye movements to point the cursor to recognise computer icons or objects in the Internet, and uses winks for commands. The researchers call this interface the "i-Mouse."

GlobalFoundries extends grants to Singapore students
ARM, Tencent Games team up to improve mobile gaming

News | Products | Design Features | Regional Roundup | Tech Impact