cancel
Showing results for 
Search instead for 
Did you mean: 

How to reverse the counting direction using a Timer in Encoder Mode.

GBert.2
Senior

I'm using an STM32 to control a DC motor with a dual hall encoder feedback.

To monitor motor position I want to use a General Purpose Timer (16bits) in Encoder Mode.

The configuration is going well but I notice that the logic is reversed:

  • When the motor is going forward, the TIM Counter goes down.
  • When the motor is going backward, the TIM Counter goes up.

I could fix this issue by flipping the wires, but I would prefer just changing a HAL configuration. Using CubeMx I tried to change the count direction settings without success (the behaviour stayed the same).

Is there a way to flip the timer counter logic?

Your help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

Change polarity of one of the channels, using TIMx_CCER.CCxP.

JW

View solution in original post

4 REPLIES 4

Change polarity of one of the channels, using TIMx_CCER.CCxP.

JW

GBert.2
Senior

Hi @Community member​ 

Thanks for your answer.

It worked perfectly.

I'm not sure I understand why it worked though. I don't see how switching channel 1 to Falling edges would reverse counting.

If you have any resources to learn about it, let me know.

Regards,

Gabriel

> I don't see how switching channel 1 to Falling edges would reverse counting.

It inverts the whole signal, thus rising edges become falling and falling edges become rising.0693W00000Kb3COQAZ.png 

JW

In changing the phase of one of the timer inputs for the quadrature encoder to reverse it's direction of counting, how does this affect the index signal which in my case, is set to high when both phases are low, and is only a quarter of a phase wide?  Might it not miss it totally, or do I have to redefine it in the encoder definition?