A Reusable Button Debounce Module
Buttons are a common component in many embedded systems, yet from a quick review of debounce algorithms they are typically not written to be portable or reused. A quick google search reveals that there are over 212,000 websites that examine how to debounce a button but most of these lack scalability and portability. Below is an example debounce module that is scalable, portable and can be used in nearly any application.
Click below to download the button debounce example:
How to configure the button module:
How to setup the button debounce module
Steps
1) Include the header and source file in your project
2) Define a gpio_config.h module with masks to access gpio pins
3) Define a gpio module containing a Dio_ChannelRead function
* Steps 2 and 3 could be substituted for a quick and dirty gpio driver
4) Define which I/O pins are buttons in ButtonList[] in button.c
5) Set the default state for each button in ButtonCurrentState
6) Set the debounce count for each button in ButtonDebounceCountList.
For example, if Button_Debounce is called in the 10ms task and the debounce is 40 ms then the count would be 4.
7) In button.h, update Button_t enum for the buttons present. If only one button then there should be one button defined in the enum.
8) Call Button_Init in the system initialization
9) Call Button_Debounce in the desired task. 10 millisecond task is recommended.
10) Write an application to use the button module. An example application that could be placed in a 10 ms task is below:
Example application:
Button_State_t Button1State = BUTTON_STATE_NOT_PRESSED;
// Debounce the switch(es)
Button_Debounce();
Button1State = Button_StateGet(BUTTON_PRIMARY);
if(Button1State == BUTTON_STATE_PRESSED)
{
Led_On(LED_RED);
}
else
{
Led_Off(LED_RED);
}
Struggling to keep your development skills up to date or facing outdated processes that slow down your team, raise costs, and impact product quality?
Here are 4 ways I can help you:
- Embedded Software Academy: Enhance your skills, streamline your processes, and elevate your architecture. Join my academy for on-demand, hands-on workshops and cutting-edge development resources designed to transform your career and keep you ahead of the curve.
- Consulting Services: Get personalized, expert guidance to streamline your development processes, boost efficiency, and achieve your project goals faster. Partner with us to unlock your team's full potential and drive innovation, ensuring your projects success.
- Team Training and Development: Empower your team with the latest best practices in embedded software. Our expert-led training sessions will equip your team with the skills and knowledge to excel, innovate, and drive your projects to success.
- Customized Design Solutions: Get design and development assistance to enhance efficiency, ensure robust testing, and streamline your development pipeline, driving your projects success.
Take action today to upgrade your skills, optimize your team, and achieve success.
