cancel
Showing results for 
Search instead for 
Did you mean: 

TIM2 and TIM3 Behaivoural Differences

teberle
Visitor

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

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.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
TDK
Super User

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.

If you feel a post has answered your question, please click "Accept as Solution".
teberle
Visitor

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.

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!