rotary encoder using timer feature

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

ep.hobbyiest
Posts: 94
Joined: Sun Jun 26, 2016 5:22 pm
Has thanked: 4 times
Been thanked: 1 time

rotary encoder using timer feature

Post by ep.hobbyiest »

Hi,
I want to interface rotary encoder with stm32f103xx. Do we have encoder functionality with timer?
I was reading stm32f1xx reference manual which mentioned stm32 has built in encoder reading feature which needs 2 channel from timer which worked together to get this work.

Or is it possible to implement it with ICU?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: rotary encoder using timer feature

Post by Giovanni »

Hi,

There is an encoder driver in the community repository,

Giovanni
ep.hobbyiest
Posts: 94
Joined: Sun Jun 26, 2016 5:22 pm
Has thanked: 4 times
Been thanked: 1 time

Re: rotary encoder using timer feature

Post by ep.hobbyiest »

Thanks Giovanni,

I merge the code and it compiled. I am using TIM4 for that. I have connected PB6 and PB7 are connected to CLK and DT of encoder.
but it is giving only one value as 5489.

Here is my code for ref.

https://github.com/devdprj/rotaryenc
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: rotary encoder using timer feature

Post by Giovanni »

Hi,

You should contact the original author, I never tried it. Please verify if the pins are assigned the correct alternate function.

Giovanni
ep.hobbyiest
Posts: 94
Joined: Sun Jun 26, 2016 5:22 pm
Has thanked: 4 times
Been thanked: 1 time

Re: rotary encoder using timer feature

Post by ep.hobbyiest »

Yes,
I have change the PAD mode to alternate for TIM4 as

Code: Select all

    palSetPadMode(GPIOB, 6, PAL_MODE_INPUT);
    palSetPadMode(GPIOB, 7, PAL_MODE_INPUT);


Is that correct?
And also i am facing one more problem like, USB and encoder are not working together. Is there timer causing issue to usb to work.?
I tried with TIM3 and TIM4 as well.

here is pic of encoder i am using .
s-l500.jpg
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: rotary encoder using timer feature

Post by Giovanni »

Hi,

You need to select an ALTERNATE mode not PAL_MODE_INPUT, see the STM32 datasheet for the correct settings.

Giovanni
ep.hobbyiest
Posts: 94
Joined: Sun Jun 26, 2016 5:22 pm
Has thanked: 4 times
Been thanked: 1 time

Re: rotary encoder using timer feature

Post by ep.hobbyiest »

Yes. while debugging i found that it is crashing. controller goes to _unhandled_exception.
and got this much stack trace.

Code: Select all

3 _unhandled_exception() vectors.c:77 0x08002620   
2 <signal handler called>()  0xfffffffd   
1 <symbol is not available> 0x55555554   


i could track till qei_lld_start function. As soon as it starts configuring registers then it complete stack trace changes from

Code: Select all

4 qei_lld_start() hal_qei_lld.c:156 0x080025a4   
3 qeiStart() hal_qei.c:177 0x080025a4   
2 initEncoder() main.c:99 0x080025a4   
1 main() main.c:117 0x080025a4   

to above mentioned. Not able to debug properly this function.
ep.hobbyiest
Posts: 94
Joined: Sun Jun 26, 2016 5:22 pm
Has thanked: 4 times
Been thanked: 1 time

Re: rotary encoder using timer feature

Post by ep.hobbyiest »

I am trying to figure out at lld level.

i could figure out some register value but not STM32_TIM_SMCR_TS field.

What should be value of STM32_TIM_SMCR_TS field in TIM1->SMCR?

I am trying to get two capture event on single timer which done in qeu driver. but that driver is not working with me.
here is the driver i found on community.
que driver files.7z
(5.17 KiB) Downloaded 444 times
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: rotary encoder using timer feature

Post by Giovanni »

Hi,

Community code is supported by the original authors, cannot help, never used that driver.

Giovanni
wayne
Posts: 2
Joined: Sat Nov 18, 2017 9:03 pm

Re: rotary encoder using timer feature

Post by wayne »

This works for me.

Code: Select all

   
    palSetPadMode(GPIOB, 6, PAL_MODE_INPUT);
    palSetPadMode(GPIOB, 7, PAL_MODE_INPUT);

 // CH1 - PB6    CH2 - PB7
    qeiInit();
    qeiStart(&QEID4, &qeicfg);
    qeiEnable(&QEID4);
Post Reply