2024-07-23 09:32 AM
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?
2024-07-23 10:12 AM
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).
2024-07-24 10:18 PM
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?
2024-08-01 02:02 PM
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).