cancel
Showing results for 
Search instead for 
Did you mean: 

Timer encoder overflow interrupt

Vineeth CN
Associate II
Posted on June 20, 2017 at 06:31

Hi All ,

   I am interfacing a quadrature encoder to TIM4 in the Encoder Mode using STM Cube with STM32F103. I set up the timer and configure the counter to counter till 1000. The function HAL_TIM_IC_CaptureCallback() is called on every interrupt. I am excepting a call back on when the counter overflow happen ie, when the count reaches to 1000 pulses in this case.

I my application i know for 1mm travel i will be getting suppose 1000 pulses, so i will set 1000 as the pre load count value and if the encoder count reaches to 1000 count i should get an interrupt.

Please help me with a solution.

Thanks and Regards,

Vineeth

1 REPLY 1
Zt Liu
Senior III
Posted on June 20, 2017 at 11:34

I am not sure whether the following method is legitimate, it works for my STM32F091 anyway.

Suppose Tim1 works in Encoder mode, turn on TIM1 update interrupt. (Not CC interrupt)

then you will see something like this in you Interrupt Service Routines.

void TIM1_BRK_UP_TRG_COM_IRQHandler(void)

{

   HAL_TIM_IRQHandler(&htim1);

}

Then Start your encoder and immediately Start Tim base Interrupt.

HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);

HAL_TIM_Base_Start_IT(&htim1);

The UIF flag (overflow event) will trigger the callback

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

   if(htim == &htim1)

   {

      

    /*Encoder Overflow Handler here*/  

   }

}

If you turn on CC interrupt,(not update interrupt)

then your encoder would trigger HAL_TIM_IC_CaptureCallback() every time when the pulses come in.

(according to your EncoderMode setting)

You may also use __HAL_TIM_GET_COUNTER() to  count the pulses each time in your CC interrupt,

then use a conditional to handle the overflow.

Hope it helps for you.