clignotante.c
The reference blink in bare C with STM32 HAL: the board LED on pin 13 toggles every 500 ms thanks to the 1 ms HAL SysTick. The skeleton is the one generated by STM32CubeIDE, ready to compile.
Hardware
A Blue Pill STM32F103C8T6 (Cortex-M3 @ 72 MHz, 64 KB flash) powered by micro-USB or external 3.3 V; programmed over SWD or the serial bootloader. None: the user LED (LED PC13 en mode sink : LOW allume, HIGH eteint) is enough. The board is programmed via its USB connector (ST-LINK or bootloader).
Wiring
Nothing to wire: internal LED on GPIOC/GPIO_PIN_13, active LOW (logique active-low : LOW allume la LED).
How it works
HAL_Init() sets up SysTick (1 ms tick); HAL_GetTick() acts as a non-blocking clock. HAL_GPIO_WritePin drives the push-pull pin. __WFI() sleeps the CPU between ticks.
Running it
STM32CubeIDE project (STM32F103C8) or Arduino (STM32 core): program over SWD (ST-LINK, DAPLink adapter) or the serial bootloader (BOOT0 to 1). Note: PC13 LED is active LOW.
Variants
Create the project from the ST-provided .ioc (or an equivalent one), or use the LL library for leaner code.
clignotante.c
// Clignotement LED en STM32 HAL (projet genere par STM32CubeIDE/.ioc).
#include "main.h"
/* Broche de la LED utilisateur de la carte. */
#define LED_PORT GPIOC
#define LED_PIN GPIO_PIN_13
/* LED active a l'etat LOW (logique active-low : LOW allume la LED). */
uint32_t dernierBlink = 0;
uint8_t etat = 0;
int main(void) {
HAL_Init(); // SysTick 1 ms + priorites NVIC
SystemClock_Config(); // horloge generee depuis CubeMX
__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; // sortie push-pull
g.Pull = GPIO_NOPULL;
g.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &g);
for (;;) {
if (HAL_GetTick() - dernierBlink >= 500) { // 500 ms
dernierBlink = HAL_GetTick();
etat ^= 1;
HAL_GPIO_WritePin(LED_PORT, LED_PIN, etat ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
__WFI(); // attend l'interruption SysTick : consomme moins
}
}