Skip to main content
paul sauv
Associate III
May 18, 2018
Question

2 different rotary encoder on STM32F103

  • May 18, 2018
  • 7 replies
  • 5345 views
Posted on May 18, 2018 at 07:29

Hello everyone,

I want to run 2 rotarty encoders on my STM32F I'm using cubemx.

The first one is a 2000 step rotarty encoder like these:

https://www.aliexpress.com/item/1pcs-E6B2-CWZ1X-2000P-R-encoder-for-Omron-2000-line-rotary-encoder-2M-incremental-encoder/322275014html

I use TIM4:

0690X0000060BGdQAM.png

0690X0000060BGiQAM.png

0690X0000060BGnQAM.png

And everything works perfectly

and here is my code:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

if (htim->Instance==TIM4) //check if the interrupt comes from TIM4

{

if (!(TIM4->CR1 & TIM_CR1_DIR)) MULT1K++;

else MULT1K--;

}

if (htim->Instance==TIM3) //check if the interrupt comes from TIM4

{

if (!(TIM3->CR1 & TIM_CR1_DIR)) pas = pas +5;

else pas = pas -5;

}

}

But on TIM3 I use a different encoder type:

http://www.mpja.com/Rotary-Encoder-with-Pushbutton-Switch/productinfo/30403+SW/

It has only 3 pins

I wired up like this:

except i didn't used PB6 and PB5

and here is my setup:

0690X0000060BGsQAM.png

0690X0000060BGxQAM.png

0690X0000060BD6QAM.png

Any idea why it's not working?

Thank you very much for your help

stm32f103-timer rotary-encoder

    This topic has been closed for replies.

    7 replies

    waclawek.jan
    Super User
    May 18, 2018
    Posted on May 18, 2018 at 10:57

    In debugger, observe the relevant bits of IDR register of GPIOA. Depending on debugger, you may need to refresh manually or switch on autorefresh. Turn slowly the encoder, holding it in various positions between the detents. Do you see both pins changing from 0 to 1? (if not, check connections). Do they change in a different moment (if they change simultaneously, there's a short between them).

    If both these change as expected, check if MODER and AFR are set properly in GPIO, and then check the timer registers.

    JW

    paul sauv
    paul sauvAuthor
    Associate III
    May 18, 2018
    Posted on May 18, 2018 at 13:22

    Hi Jan,

    IDR6 and IDR7 of GPIOA are moving but not on each step. This is a mechanical encoder so i can feel each position

    when i turn slowly from one position to the other, sometime IDR are changing sometimes not. but the value of my variable is still the same

    I tried to wire my optical encoder instead of mechanical encoder and it's not working either

    Edit:

    stupid thing, i just forgot to start tim3 interrupt:

    HAL_TIM_Base_Start_IT(&htim3);

    :(

     sorry for time waste !
    Jan Waclawek
    Visitor II
    May 18, 2018
    Posted on May 18, 2018 at 17:50

    In encoder mode, the result is in the timer's counter (TIMx_CNT). Don't use interrupts.

    JW

    Lina_DABASINSKAITE
    ST Community Manager
    July 4, 2023

    Hello, 

    @Nicholas Yunker_2 , apologies for the experience once again due to the wrong auto spam detection. I am closing this thread and encourage to future continue the discussion here: Solved: Timer Rotarty Encoder Mode not Counting Properly - STMicroelectronics Community

    BR,
    Lina - ST Community manager

    ​In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question.