AnsweredAssumed Answered

STM32F103 - rotary encoder TIM - INT generation

Question asked by zandona.nicola on Apr 7, 2014
Latest reply on Sep 24, 2016 by galloway.paul
Hi.
I successfully configured my STM32F103VC custom HW to use a rotary encoder by following the code suggested in this forum. So first of all: thank you!
When I wrote "successfully" I mean that the counter gets updated for every encoder rotation.


//GPIO & other init code intentionally omitted...
  
  TIM_TimeBaseStructure.TIM_Prescaler = 0;  // No prescaling
  TIM_TimeBaseStructure.TIM_Period = 32;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3;
  TIM_TimeBaseInit(ENCODER_TIM, &TIM_TimeBaseStructure);
  TIM_EncoderInterfaceConfig(ENCODER_TIM, TIM_EncoderMode_TI1,TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);

In my application the encoder is used as input device for the human interface, so I must intercept every action on the knob in order to update the UI.

QUESTION: Is there a way to generate an interrupt for every increment/decrement of the timer by the encoder?

RM0008 - REV14 - section 15.3.2 states:
Center-aligned mode is active when the CMS bits in TIMx_CR1 register are not equal to
'00'. The Output compare interrupt flag of channels configured in output is set when: the
counter counts down (Center aligned mode 1, CMS = "01"), the counter counts up (Center
aligned mode 2, CMS = "10") the counter counts up and down (Center aligned mode 3,
CMS = "11").


So it would seem that the Output Compare feature is doing the job, but it is evidently not working as the INT triggers only on CNT=CCR1 (or CNT>CCR1) matches.
I imagine that I rather need an Input Capture feature for this (note also that the CC1S[1:0] are set to 01 - so Input Capture) by the TIM_EncoderInterfaceConfig call even if apparently that is NOT necessary to drive the timer slave controller).

I tried then to set the IC1 prescaler to /2 and I tried to latch the CNT value: I get almost every time an overcapture flag rather that it INT flag. I suspect that feeding the CNT and latching the capture compare basically with the same signal is not working properly (latencies?).

Do you experts have any idea?
Thank you in advance for your help.

PS: As the code shows I'm not using the prescaler yet so the counter counts twice for each notch as, when setting it != 0 the counter gets crazy - even using the input filter [maybe the latter is not configured properly...]

Outcomes