cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] STM32G070 TIM Encoder mode

YDann.7
Associate III

Dear,

I currently trying to used the timer in encoder mode.

It seems to working fine whatever the mode configured.

I useing the timer interrupt (Capture Compare) to read the number of step (Counter (CNT)).

In mode 1 and mode 2, the CNT is decrement by 2 in clockwise and increment by 2 in counterclokcwise

In mode 3, the CNT is alternatively decrement by 1 and decrement by 3 in clockwise and increment by 1 and decrement by 3 in counterclockwise.

It seems working well regarding the reference manual because the interrupt is configured in rising edge and therefore all steps of the encoder don't generate an interrupt.

Is it possible to have all step of the encoder at each increment/decrement ?

Or must I use a other to read the CNT at fixed interval of time ?

5 REPLIES 5

I'm not sure it's the best idea to read out CNT at the CC interrupt. What happens if there are edges faster than your software can execute the ISR?

Nonetheless, if the edges are far enough from each other, in Mode1 (where counting is upon TI1FP1) you set Capture on both edges (i.e. both TIMx_CCER.CC1P=1 and CC1NP = 1), you should see every single increment/decrement in the ISR.

JW

YDann.7
Associate III

To be more precise, I don't read the value of CNT in the ISR.

I just indicate that a new value is available and in the main loop of my program (outside any ISR), I read the value of CNT.

As mentionned on the reference manual, it is not possible to set the input capture in the both edge when the encoder mode is used and it's for that I have an increment/decrement by 2 at each interrupt.

I also tried to change the polarity after each interrupt but it didn't worked. I had alternatively +1/-1 at each interrupt. I think it's due to the CC input configuration :

  • CC1P = 0 ; CC1NP = 0 => non inverted / riding edge
  • CC1P = 1 ; CC1NP = 0 => inverted / falling edge
  • the other configurations is prohibited in encoder mode.

> As mentionned on the reference manual, it is not possible to set the input capture in the both edge when the encoder mode is used

Ah, indeed, I overlooked it, sorry.

Indeed, in Encoder mode, changing CCxP bits influences the polarity of signal which goes into the encoder logic i.e. effectively changes the "direction of detection". So this is not an option indeed.

Then there's probably no other option but to read out TIMx_CNT periodically.

JW

Perhaps as an alternative, you could try to set EXTI to detect both edges on given pin. You don't need to actually enable the interrupt in NVIC, in main you can simply check/clear the respective EXTI_PR bit.

JW

YDann.7
Associate III

I did it with a timer and reading the CNT value at fix interval time and it working well.

Maybe next time, i will trying using EXTI as you propose.

Thanks for your suggest.