cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder tick call tim ISR not regular

dungeonlords789
Senior III

I use STM32 and TIM in encoder mode. I configure it by Cube, project and screenshorts is attached. Also I enabled TIM global interrupt, so I am wating that TIM2_IRQHandler will calling every TIM2 CNT change. Encoder is rotated and TIM2 CNT looks believable, but indexOfEncoderT2 must be equals TIM2 CNT, isn't it?

Also I rotate encoder for 1 tick. TIM2 CNT is changing for one. But MCU call TIM2_IRQHandler sometimes, sometimes not.

I select Clock polarity Both Edges, so I use HAL_TIM_Encoder_Start_IT(&htim2, TIM_CHANNEL_ALL);

 Then I try to save TIM2 CNT in TIM2_IRQHandler and get interesting table "TIM2 CNT fixed by ISR.png" So ISR is not called at 0,1,4,5,8,9,12,13,16,17 ticks...

3 REPLIES 3

Executing the timer ISR probably takes longer than time difference between consecutive encoder input edges.

Observe them using oscilloscope or LA to see what your time budget is.

Regardless of the fact that Cube/HAL is very inefficient and that the ISR can probably be made faster if written properly, it's unusual to use this kind of interrupt exactly for this reason (it also defeats the purpose of having hardware encoder decoding, at all). Usually, CNT is observed from main loop or from as periodic interrupt.

JW

if I rotate encoder by hand for about 1 tick I have got interesting behavior. TIM2 CNT doesn't change, but ISR is called twice. Can it be because encoder goes from 0 to 1 and back to 0 because it is mechanical?

Observe the signals using oscilloscope or LA. If you did not move back and performed a hslf or quarter cycle (depending on which encoder mode you've chosen in timer), CNT should have changed.

JW