cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder problems STM32H743ZI2

danielallstar
Associate II

Hello,

I have a problem using the LL_TIM_ENCODERMODE_X4_TI12 on a nucleo_H743ZI2 development board. I directly read the counter register (TIM2->CNT for example ) to obtain the encoder value. I do however notice that even though the encoder is not rotating the count value in the register fluctuates as for example:

Sensor value: 1
Sensor value: 0
Sensor value: 0
Sensor value: 1
Sensor value: 1
Sensor value: 1
Sensor value: 0
Sensor value: 0
Sensor value: 65535
Sensor value: 1
Sensor value: 0
Sensor value: 65535  

This does also happen at the moment that no encoder is connected to the pins and nothing should happen at all. 
Besides that I also notice that counts are missed sometimes when I rotate the encoder and therefore the timer decoder is not as accurate as expected. 

I use the same settings as suggested in the Cube IDE:

TIM_InitStruct.Prescaler = 0;

TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;

TIM_InitStruct.Autoreload = 65535;

TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;

LL_TIM_Init(TIM2, &TIM_InitStruct);

LL_TIM_DisableARRPreload(TIM2);

LL_TIM_SetEncoderMode(TIM2, LL_TIM_ENCODERMODE_X4_TI12);

LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);

LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);

LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);

LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);

LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);

LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);

LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);

LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);

LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);

LL_TIM_DisableMasterSlaveMode(TIM2);

Help would be very much appreciated to solve this problem!

5 REPLIES 5

What encoder are you using? How is it connected?

With the common manually turned encoders, you need to have pull-ups connected or switched on on the inputs.

JW

danielallstar
Associate II

The encoder is an AZ-Delivery KY-040 rotary encoder . The encoder is directly connected to the STM32 development board using jumper wires. The power and ground are also supplied from the STM32 board. This problem was also faced with non-manually turned encoders.

 

It appears that there are pullups on that board. Do you have the "+" terminal connected to VDD?

> This does also happen at the moment that no encoder is connected to the pins

For that case, you should switch on the internal pullups.

JW

danielallstar
Associate II

I connected the + terminal to the 5V pin of the STM32, but using VDD results in the same behavior.

Then the only remaining thing to do is to observe the input pins using oscilloscope.

JW