bouton-hal.c
The board user button (GPIOA/GPIO_PIN_13, active HIGH) toggles the LED on each press. 50 Hz sampling debounce is the standard embedded-C pattern, with no interrupt nor blocking delay.
Hardware
A Nucleo F4 board (STM32F401RE/F411RE, Cortex-M4) programmed via ST-LINK (USB), 3.3 V pins. Nothing to start with: the user button is factory-wired. Optionally an external button to 3.3 V.
Wiring
Internal button: already tied to GPIOA/GPIO_PIN_13 (bouton USER de la carte, actif a l'etat HIGH). External button: between the pin and 3.3 V, internal pull-down.
How it works
Every 20 ms the pin state is read and stored; a 0→1 transition (rising edge) calls HAL_GPIO_TogglePin. Two identical consecutive samples filter the bounce.
Running it
Create a STM32CubeIDE project (pick Nucleo-F401RE or F411RE), paste the code into main.c, configure peripherals in the .ioc, then Run. The ST-LINK serial terminal shows printf at 115200.
Variants
Move to an EXTI interrupt with HAL_GPIO_EXTI_Callback, or add a long-press (sample counter).
bouton-hal.c
// Bouton utilisateur + LED, anti-rebond par echantillonnage.
#include "main.h"
#define LED_PORT GPIOA
#define LED_PIN GPIO_PIN_5
#define BTN_PORT GPIOA
#define BTN_PIN GPIO_PIN_13
/* Bouton actif a l'etat HIGH. */
uint32_t tEchantillon = 0;
uint8_t stable = 0, precedent = 0;
int main(void) {
HAL_Init();
SystemClock_Config();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef g = {0};
g.Pin = LED_PIN; g.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(LED_PORT, &g);
g.Pin = BTN_PIN; g.Mode = GPIO_MODE_INPUT; g.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(BTN_PORT, &g);
for (;;) {
if (HAL_GetTick() - tEchantillon >= 20) { // 50 Hz
tEchantillon = HAL_GetTick();
precedent = stable;
stable = (HAL_GPIO_ReadPin(BTN_PORT, BTN_PIN) == GPIO_PIN_SET);
if (stable && !precedent) { // front montant
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
}
}
}
}