cancel
Showing results for 
Search instead for 
Did you mean: 

Tracking an optical encoder overflow and underflow

Hrishikesh
Senior

I'm using an STM32F407 MCU with a 600 PPR incremental optical encoder. The encoder is coupled to a stepper motor that drives a 5mm pitch ballscrew. I'm trying to keep track of the encoder count to determine the absolute position of the spindle attached to the ballscrew. Also, using the encoder in quadrature mode so the actual count per 1 revolution of the encoder is 2400 counts. I wrote the following program only to test the logic (and not store the actual count) but this does not seem to work. Any idea what I'm missing?

I'm keeping track of when the encoder overflows and becomes zero. If its rotating CW, then the previous count is 2399 and in CCW it should be 1 right? Also I'd really appreciate it if anyone can recommend a better way to track overflow.

void encoderCount()
{
    currCount = __HAL_TIM_GET_COUNTER(&htim1); //get encoder count
 
    if (!__HAL_TIM_IS_TIM_COUNTING_DOWN(&htim1)) //Is the encoder timer counting up? (CW)
    {
        if (currCount == 0)
        {
            prevCount = 2399;
            indexCount++;
        }
    }
    else //then it must be counting down (CCW)
    {
        if (currCount == 0)
        {
            prevCount = 1;
            indexCount--;
        }
    }
}

10 REPLIES 10

> You'd check the count with sufficient frequency

> that you could catch over/under limit zones.

This is what I meant, but it's true that that's not what I've written above.

I meant, the "sufficient frequency" can be achieved by taking readings periodically e.g. in an another timer's interrupt, *in addition to* the readings taken deliberately in the program to determine the current position.

JW