cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 encoder mode sample rate

lk.dgironi
Associate III

Hello,

I'm working on a STM32 project which use the timer in encoder mode.

TIM2 of my STM32F103C8 is set in Encoder Mode, I've enabled the TIM2 global interrupt.

I'm evaluating the counter at each interrupt.

Prescaler is 0, Counter Period is 65535.

Main clock is 72Mhz.

I've read the datasheet but it's not clear to me if the Counter Period in this mode is referred to the sampling rate of the encoder, or to the max Interrupt sampling rate.

I mean, If my motor encoder has a sampling output rate of 1KHz, do I need to set a Counter Period of 72Mhz/1KHz, or if i set higher than this the interrupt will "lose" some count, cause interrupt counter is 16bit (65536 count) but encoder output would be 72000000/1000 = 72000 count?

Thank you!

10 REPLIES 10

Yeah! Now is getting clear.

> Yes. But those signals are not directly coming from pins...

So TI1FP1 or TI2FP2 are sampled at APB clock rate... means 72Mhz in my case. Right?

> That's not entirely true. ARR...

> That depends on what did you set in TIMx_DIER...

Maybe I get it now. My interrupt is server by the NVIC, the interrupt is enabled on DIER register. I've follow the path, and come to the conclusion that my interrupt is enabled masking DIER with 0x00000004, so it's CC2IE bit, it means Capture/Compare 2 interrupt enable

__HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
#define TIM_IT_CC2 TIM_DIER_CC2IE
#define TIM_DIER_CC2IE_Msk (0x1UL << TIM_DIER_CC2IE_Pos) /*!< 0x00000004 */
#define TIM_DIER_CC2IE TIM_DIER_CC2IE_Msk

Capture/Compare 2 interrupt enable means that each time the counter change, that interrupt is triggered, interrupt is then served according to his priority and IRQ by the NVIC, which finally call my HAL_TIM_IC_CaptureCallback function.

You suggest to have another interrupt triggered by a new timer (ex. TIM1), which handle the CNT grabbing for TIM2. This interrupt should be triggered before the TIM2 encoder counter overflow, and that overflow threshold depends on the ARR register of this timer.

Am I right? I mean, is this what is happening?

Again thanks for your help.