bouton-hal.c
Le bouton utilisateur de la carte (GPIOC/GPIO_PIN_13, actif HIGH) bascule la LED à chaque appui. L'anti-rebond par échantillonnage à 50 Hz est le patron standard en C embarqué, sans interruption ni temporisation bloquante.
Matériel requis
Une Nucleo H7 (STM32H743ZI, Cortex-M7 @ 480 MHz) programmee via ST-LINK (USB), broches 3,3 V. Rien pour commencer : le bouton utilisateur est câblé en usine. En option, un bouton externe vers 3,3 V.
Câblage
Bouton interne : déjà relié à GPIOC/GPIO_PIN_13 (bouton USER de la carte (PC13), actif a l'etat HIGH). Bouton externe : entre la broche et 3,3 V, résistance interne en pull-down.
Fonctionnement
Toutes les 20 ms, l'état de la broche est lu et mémorisé ; un passage 0→1 (front montant) déclenche HAL_GPIO_TogglePin. Deux échantillons consécutifs identiques filtrent les rebonds.
Exécution
Projet STM32CubeIDE base sur la Nucleo-H743ZI : le code se glisse dans main.c, les peripheriques se declarent dans le .ioc, puis Run via l'interface ST-LINK. Le moniteur serie du ST-LINK affiche les printf a 115200.
Variantes
Passer en interruption EXTI avec callback HAL_GPIO_EXTI_Callback, ou ajouter un appui long (compteur d'échantillons).
bouton-hal.c
// Bouton utilisateur + LED, anti-rebond par echantillonnage.
#include "main.h"
#define LED_PORT GPIOB
#define LED_PIN GPIO_PIN_0
#define BTN_PORT GPIOC
#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);
}
}
}
}