cancel
Showing results for 
Search instead for 
Did you mean: 

2 different rotary encoder on STM32F103

paul sauv
Associate III
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 discussion has been locked for participation. If you have a question, please start a new topic in order to ask your question
7 REPLIES 7
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

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 !
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

Does the Encoder work properly with Interrupts at all?  I have been attempting to take the CNT register value and put it in a variable called counter within the HAL_TIM_IC_CaptureCallback function and while it works sometimes, most of the time the value in my variable does not match what's in CNT.  I've been able to pull it out of the CaptureCallback and it works OK in the Main while(1) loop checking once per loop...but I'd rather use interrupts if possible.

Please don't hijack others threads, especially a historical one. Start your own, stating the STM32 and hardware you are using, and the problem you came across. You can link to this thread if you think it's relevant.

JW

my apologies, I was not trying to hijack a post.  I tried posting two previous posts of my exact problem and they kept getting marked as SPAM and not posted.  I have finally managed to get a post up that did not get marked as spam.

Lina_DABASINSKAITE
Community manager
Community manager

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 'Accept as Solution' on the reply which solved your issue or answered your question.