cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L433RC and quadrature encoders - twice the expected pulses?

Marcelo Barros
Associate III
Posted on June 30, 2018 at 14:35

Hello,

I have configured TIM1 and TIM2 as combined channels (encoder feature), set pins to count on falling edge using TI1 encoder mode (please, see images below), added pull ups to the lines. However, I can see the timers counting on both edges.

After an unexpected result during my real tests (I have a large system with motors and encoders with 1024 pulses per rotation) I did a very small test using those arduino module encoders (KY-040) and I could confirm these behavior.

I tested all edges combinations and modes. I checked the register configuration done by CubeMX against the reference manual and it seems fine. The result is the same, counting twice as expected (for TI1 mode), using both edges to count. I tried also to set 'Prescaler Division Ratio' as 2, but it didn't work as well. By the way, processor errata does not mention anything related to encoder problems.

So, am I doing something wrong here ? Or, is this the expected behavior ?

PS: When using  both TI1 and TI2 encoder mode I had four times the amount of expected pulses.

#encoder #tim #stm32l433 #quadrature-encoder
6 REPLIES 6
T J
Lead
Posted on June 30, 2018 at 14:44

2x  or  4x  ?

there was another thread, did you search for it ?

JW suggested that you must /4

Marcelo Barros
Associate III
Posted on June 30, 2018 at 14:52

Hi

Yes, I searched before, here and using google.

> 2x  or  4x  ?

2x when using TI1 OR TI2.

4x when using both TI1 AND TI2.

Yes, the obvious solution is to divide by two (or 4) but I want to know if it is a feature, a bug or some configuration error from my side as this behavior is not expected.

Marcelo

Posted on June 30, 2018 at 16:06

In encoder mode you don't select the edge on which to count - the counter counts always on *both* edges, either on both edges of one channel, or both edges of both channels.

Read the Encoder interface mode subchapter of timer chapter in reference manual.

JW

Marcelo Barros
Associate III
Posted on June 30, 2018 at 17:00

Hello

Thanks for answering.

I read this section. Some times. Maybe the several references to 'polarity' in this section associated to the Cube setup (clearing stating falling or raising) gave me the wrong impression that I should consider only the configured edge. Add to this the CCER register configuration, where the description is clear about edge polarity and both polarities are not valid for encoder mode, and some doubts may arise.  The statement 'The counter is clocked by each valid transition on TI1FP1 or TI2FP2' is not clear. Valid transition on the configured edge  or both edges ?

But, it is fine. I will divide by two, pretty simple solution.

Could you confirm if 'Prescaler Division Ratio' work in encoder mode ? I set it to 2 but I couldn't see any change in counting.

Marcelo

Posted on June 30, 2018 at 17:56

Marcelo,

Yes, the description may be confusing. If you look at the 'Capture/compare channel (example: channel 1 input stage)' figure, after the input filter, there is an edge detector and then a multiplexor selecting between the edges. However, this is not how the input channel works in encoder mode: there, the edge detector is replaced by a simple invertor, so the TI1FP1 signal (which is subsequently fed to the slave-mode-controller's encoder input) is selected by TIMx_CCER.CC1P to be either a straight copy of TI1F or its inverted value.

The encoder logic in the slave-mode-controller is sensitive to both edges.

The prescaler is a simple downcounter, so it does not count up-down depending on the decoded direction, as the counter does. The result is a very confusing behaviour (when the encoder changes direction), if you set it to other than non-divide.

JW

Posted on June 30, 2018 at 18:03

 Ok, thanks for clarifying. CNT >>1 and it is done !