In this use case, a simple debounce filter on a button will be set up.
Setup steps
Prerequisites
For the setup code of this use case to work, the following must be added to the project:
- System Clock Management
- Programmable Multilevel Interrupt Controller
- General Purpose Input/Output
- Real Time Counter (RTC)
- Configuration info for the timeout service must be added to the conf_timeout.h file (located in the config folder):
#define TIMEOUT_CLOCK_SOURCE_HZ 1024
#define TIMEOUT_COUNT 1
#define TIMEOUT_TICK_HZ 100
- Configuration info for the RTC driver must be added to the conf_rtc.h file (located in the config folder):
#define CONFIG_RTC_PRESCALER RTC_PRESCALER_DIV1_gc
#define CONFIG_RTC_CLOCK_SOURCE CLK_RTCSRC_ULP_gc
Example code
The following must be added to the project:
#define DEBOUNCE_TIMEOUT 0
#define DEBOUNCE_TICKS (50 * TIMEOUT_TICK_HZ / 1000)
Add to application initialization:
Workflow
- Initialize system clock:
- Initialize the PMIC driver:
- Initialize timeout service:
Example code
Add to application C-file:
bool button_previous_state_pressed = false;
while (1) {
if (button_previous_state_pressed != button_pressed) {
}
if (button_pressed) {
}
}
}
Workflow
- Create a variable to hold state of push button:
- Create a variable to hold previous state of push button:
bool button_previous_state_pressed;
- Get button state:
- Check if button state has changed since last iteration:
if (button_previous_state_pressed != button_pressed)
- Start debounce timeout:
- Set previous state of button:
- Check if debounce timeout has expired:
- Check if button is pressed down:
- Toggle led: