cancel
Showing results for 
Search instead for 
Did you mean: 

How can I measure encoder velocities using pulse width measurements (low RPM)?

Filip Vranes
Associate II

Hi,

I am using the STM32F303RBTx MCU and currently have a quadrature (4x) encoder set up on TIM2. You can find my STM32CubeMX configuration settings below:

0693W00000FDMeOQAX.png 

I would like to implement a tachometer for this encoder. I am aware of the method of periodically measuring the delta encoder counts for a given time period, but this is only effective at higher RPMs.

I would like to implement an effective tachometer at low RPMs as well. My idea for this was to measure the time difference between two consecutive signal edges on one of the encoder channels.

I tried setting up a slave timer (TIM15) to synchronize with TIM2 (master timer), using Compare Pulse as the Trigger Event Selection (TRGO), but so far I'm having no luck with this.

I also looked into setting up a PWM input timer, but this would require having the encoder signal electrically routed to the appropriate timer pin on the micro, which I have not done.

Maybe I'm missing something in the datasheet/reference manual that would help solve this problem for me. Any advice would be much appreciated. Thanks!

4 REPLIES 4
TDK
Guru

Using an IC timer mode would be the best method.

Another method would be to set up one or both pins as EXTI triggers and use a high resolution internal timer such as CYCCNT to calculate the delta between edges. The hardware can support this but last I checked, HAL could not, so you will need to write some code to set up the pins this way. At higher rates of speed, the EXTI may saturate the cpu, so you will need to be smart about it.

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

Thanks for the prompt reply! Would the encoder pin have to be directly connected to this IC timer, or can it be triggered internally? If internally triggered, am I correct to use Compare Pulse as the TRGO? OCxREF can't be used because my encoder (TIM2) is configured as an input timer, not an output.

Yes, the encoder pin would need to be connected to the IC channel. The timers have considerable flexibility, but I don't see any other way of doing this.
If you feel a post has answered your question, please click "Accept as Solution".

If you want to use the internal master-slave connection, then yes, in TIM2 you use the incorrectly named Compare Pulse setting for TIM2_CR2.MMS. However, you won't be able to measure pulse *length*, as in this setting TRGO is taken from after the edge detector and is only one pulse long. You can still infer frequency from signal period, i.e. capture of successive edges of the same direction.

JW