cancel
Showing results for 
Search instead for 
Did you mean: 

Motor speed estimation using TIM2 and TIM3 on STM32F407

zd9797
Associate II

I'm trying to connect an encoder with STM32F407VGT6. The encoder sends out A,B and Z pulses out. I can read both A-B and Z pulses using TIM2 and TIM3 respectively. Timer 2 is in encoder mode and TIM3 is in input PWM mode. 

I want to know if I'm correct:

A-B encoder signals give me the rotation data 
and 
Z signal will give out pulse with variable frequency.

If I read the time period saved in CCR1/CCR2 of TIM3, can I estimate the speed of the motor? If yes, how?

 

1 ACCEPTED SOLUTION

Accepted Solutions

I honestly have never looked at PWM Input mode <g>  Yep, looks like TIM3 CCR1 will give you the timer counts between Z pulses  And you can calculate RPM from that since you know the timer's clock freq and prescaler values.

But you still have to ensure that you get a Z pulse before the timer rolls over (i.e. in less than 65536 TIM3 counts).  Or else you need to detect rollovers and add 65536 to the CCR1 value for each time the timer rolls over.  Alas, in input PWM mode I don't see how you can get an interrupt on rollover (but, again, I haven't looked very closely).

View solution in original post

4 REPLIES 4
Bob S
Principal

The "Z" signal gives you 1 pulse per encoder revolution.  You don't need to use PWM Input mode on TIM3, you can use input capture mode (pick an edge, it doesn't matter).  Taking delta CCR1 values gives you the timer counts per encoder revolution.  If you know the timer clock freq (you should) and the ratio of encoder revolutions to motor revolutions, then it is a simple calculation to get the motor speed.

One caveat is you need to handle the case where the Z pulses are more than 65536 timer clock periods apart (i.e. the motor is "slow" compared to your timer clock).

Thanks for your reply. But now I'll have to handle this in interrupt service routine which is not what I want. I made TIM3 work as input pwm capture and it gives me the "delta of CCR1" as period is the time between two edges. What do you think?

I honestly have never looked at PWM Input mode <g>  Yep, looks like TIM3 CCR1 will give you the timer counts between Z pulses  And you can calculate RPM from that since you know the timer's clock freq and prescaler values.

But you still have to ensure that you get a Z pulse before the timer rolls over (i.e. in less than 65536 TIM3 counts).  Or else you need to detect rollovers and add 65536 to the CCR1 value for each time the timer rolls over.  Alas, in input PWM mode I don't see how you can get an interrupt on rollover (but, again, I haven't looked very closely).

Thanks Bob! I'll definitely look into that. :)