Skip to main content
Explorer II
August 21, 2025
Solved

STM32F446RE bare metal TIM6 configuration for delay

  • August 21, 2025
  • 1 reply
  • 482 views

Hello everyone,

I am fairly new to embedded so bear with me. I am trying to understand bare metal programming.

So basically I managed to turn on the LED on soldiered on the board (PA5) but now am trying to create a delay using basic timer TIM6. My issue is that the LED is barely turning on for a very brief moment before turning off. I think my issue lies in how I am configuring the prescaler and auto-reload register. I looked up online and it says that the default clock speed is 16MHz, but I did not configure any clock source.

Here is my code down below which I used the reference manual to write:

 

GPIOA->ODR |= (1<<5); // Turning on LED

 

// Initialize TIM6

RCC->APB1ENR|=(1<<4);

TIM6->CR1|=(1<<0);

TIM6->PSC=16000-1;

TIM6->ARR=1000-1;

TIM6->CNT=0;

 

while(!(TIM6->SR & (1<<0))){}

TIM6->SR&=~(1<<0);

TIM6->CR1&=~(1<<0);

 

GPIOA->ODR &= ~(1<<5); //Turning off LED

 

 

Thank you.

 

    This topic has been closed for replies.
    Best answer by TDK

    ARR and PSC are preloaded and only take effect after the next update event.

    After writing to ARR/PSC, generate an update event using the EGR register, then clear the UIF flag. Then start the timer and wait for UIF again.

     

    Proper way to clear flags here is by writing. No read-modify-write.

    // using CMSIS defines:
    TIM6->SR = ~TIM_SR_UIF;
    
    // or if you must...
    TIM6->SR = ~(1 << 0)

    1 reply

    TDKAnswer
    Super User
    August 21, 2025

    ARR and PSC are preloaded and only take effect after the next update event.

    After writing to ARR/PSC, generate an update event using the EGR register, then clear the UIF flag. Then start the timer and wait for UIF again.

     

    Proper way to clear flags here is by writing. No read-modify-write.

    // using CMSIS defines:
    TIM6->SR = ~TIM_SR_UIF;
    
    // or if you must...
    TIM6->SR = ~(1 << 0)
    STMnoobAuthor
    Explorer II
    August 22, 2025

    Thank you, it works now :)