cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder overflow or underflow?

Raül G
Associate II
Posted on February 17, 2018 at 16:56

Hello,

I configured the TIMER1 to use like an encoder counter. My question is: How I can detect inside the in the interrupt function if the interrupt was caused by an overflow or underflow in the counter?

void

HAL_TIM_PeriodElapsedCallback(

TIM_HandleTypeDef

*htim)

{

   if

(htim == &htim1)

   {

    

HAL_GPIO_TogglePin(Led_Groc_GPIO_Port, Led_Groc_Pin);

   }

}

Thanks!!!

5 REPLIES 5
Posted on February 18, 2018 at 10:54

Normally you don't want to do that as with most encoders missing an interrupt thus losing the count is inevitable.

Instead, make sure you read out the timer faster than half a revolution at maximum rate, and perform the underflow/overflow detection in software. Using a 32-bit timer (TIM2, TIM5) helps in this immensely.

JW

matic
Associate III
Posted on February 18, 2018 at 16:18

Timer (CNT register) in encoder mode is counting independently on its own. When interrupt is triggered, check if CNT value is between 0 and 32768 (in case of 16-bit timer) or if the CNT value is between 32768 and 65535. Normally it will be just a few counts above the zero (overflow, counter went from 65534-65535-0-1-2...) or few counts below the max. value (underflow, counter went from 2-1-0-65535-65534...).

In such fashion, you can create a 32-bit encoder counter with additional 16-bit variable, which is incremented/decremented by 1 always when interrupt occurs due to overflow/underflow. Your final 32-bit counter would than be current value of 16-bit variable shifted to the left by 16 ORing a TIMx->CNT register.

u32_Value = (u16_OverflowCnt << 16) 

| TIMx->CNT;

Posted on February 18, 2018 at 16:46

When interrupt is triggered,

This is exactly what I warn against. Don't do this, it's easy to miss an interrupt when the encoder reverses.

JW

Raül G
Associate II
Posted on February 18, 2018 at 21:59

Thanks to everybody for the comments!

I need to control 2 motors. Each motor have a speed of 10.000rpm with an encoder of 500pulses/turn, it means that I can get up to 83.333pulses/second.

The uC that I'm using is the STM32F410 (Nucleo board) that have 5 timers, and only TIM1 (16bits) and TIM5 (32bits) have the possibility work like 'encoder counter'.

Of course if I choose counters with 32bits it can solve my problem. Where I can find in ST the chip selector where I can filter the microcontrollers that fulfills my requirements?

Thanks!

RG 

Posted on February 19, 2018 at 13:59

Have a look at AN4013.

Only Advanced and General-purpose timers (i.e. those which are up-down counters) have the encoder feature. There are at most 2 32-bit timers, so you need to cope with the 16-bit ones anyway - as I've said, it's just a question of sampling them often enough.

In one application we are using 3 cheap 'F0 to handle several encoders each (but they have other tasks, too). The mutual communication is then not quite trivial, so while it may be a way to go, certainly not an easy one.

It is also not quite impossible to avoid using timers by sampling the signals and decoding them in software, however, the overall computing power will be impacted. Benchmarking and proof-of-concept work would be needed to find out the limitations of this approach.

JW