2025-08-20 5:22 PM
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.
Solved! Go to Solution.
2025-08-20 7:25 PM
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)
2025-08-20 7:25 PM
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)
2025-08-21 6:52 PM
Thank you, it works now :)