2018-05-17 10:29 PM
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:
I use TIM4:
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:
Any idea why it's not working?
Thank you very much for your help
stm32f103-timer rotary-encoder2018-05-18 01:57 AM
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
2018-05-18 06:22 AM
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 !2018-05-18 10:50 AM
In encoder mode, the result is in the timer's counter (TIMx_CNT). Don't use interrupts.
JW
2023-06-30 04:26 PM
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.
2023-07-01 12:59 AM
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
2023-07-01 06:09 AM
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.
2023-07-04 06:12 AM
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