cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F446RE bare metal TIM6 configuration for delay

STMnoob
Associate

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

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)
If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Super User

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)
If you feel a post has answered your question, please click "Accept as Solution".

Thank you, it works now :)