2020-08-04 03:12 PM
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...
2020-08-04 09:34 PM
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
2020-08-05 10:23 AM
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?
2020-08-05 06:06 PM
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