Search code examples
cstm32microcontroller

STM32 blinking LED wrong register?


Has anyone an idea why the code doesn't work?

RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
GPIOB->MODER &= ~(0x3u << 6u);
GPIOB->MODER |= (0x1u << 6u);
for (int i = 0; i < 1000; i++) {
    GPIOB->ODR |= (0x1u << 0x3u);
}

I'm using a STM board (STM32L432KC) which has an integrated LED that is called pin PB3 (Port 26) but after flashing nothing happens. Actually there should shine a LED. Do I use the right register?


Solution

  • You're setting a bit in the ODR but never clearing it, so it won't flash, you will need a delay between on/off transitions to be able to visibly see it flashing.

    It's good practice to write to the BSRR register instead of the ODR too where possible, this allows you to avoid read-modify-write cycles.

    If that code is your whole main() then you might want to replace the for (int i = 0; i < 1000; i++) with a while(1), you generally don't want to return from main in an embedded context.