2025-06-12 9:59 AM - last edited on 2025-06-12 12:10 PM by mƎALLEm
Hello,
I am trying to write some non-blocking motor driver code for a STM32F401Re board. Currently, I am setting the timers to PWM mode and adjusting the ARR to change the PWM frequency using an IRQ handler. This approach is working for TIM3 output, but TIM2 seems to stop outputting after one iteration of the while loop. I've been through the registers and it looks like the CC1IF isn't always being set for TIM2 and I'm not sure why. By adjusting the ARR, shouldn't both timers roll-over after hitting their new max value?
I've attached the main file and the A4988 driver file I'm writing. For the A4988 file, the methods in question are A4988_run() and A4988_IRQ_Handler().
Thanks!
Solved! Go to Solution.
2025-06-12 10:24 AM - edited 2025-06-12 10:25 AM
TIM2 is 32-bit, primarily.
Since ARR is preloaded, after you change it, ensure it takes effect immediately by generating an update event in the EGR register. The initial ARR value for TIM2 is 2^32-1 which means you'll be waiting a long time for it to update otherwise. Whereas TIM3 is only 2^16-1.
2025-06-12 10:24 AM - edited 2025-06-12 10:25 AM
TIM2 is 32-bit, primarily.
Since ARR is preloaded, after you change it, ensure it takes effect immediately by generating an update event in the EGR register. The initial ARR value for TIM2 is 2^32-1 which means you'll be waiting a long time for it to update otherwise. Whereas TIM3 is only 2^16-1.
2025-06-12 11:35 AM
Thanks, the interesting thing is that the code is functioning for transitions between a high -> low rpm but not in reverse. Additionally, the timer is running through the rest of my if loop with the correct ARR, it just is not outputting PWM signal.
2025-06-12 12:16 PM
Hey, realize I didn't respond with my other comment!
Thanks, adding the update event to the EGR register along with resetting the CNT to 0 seems to have fixed it!