20.5 Application Example

Often at system power-up, a machine or motor must be “homed” to a known position to establish a reference point. In this case, the motor and/or machine is moved in a specific direction at slow speed until a home sensor is detected. The home switch position is not precise. Therefore, the system will use an index pulse to determine the precise point of “home.”

Once the motor is homed, the Position Counter is required to be loaded with a known value.

In this application scenario, the Position Counter could be initialized to Mode 3 (PIMOD[2:0] = 3), and the QEIxIC register could be loaded with the initialization value. Optionally, an interrupt could be generated at the end of this Position Counter (homing) initialization process by setting the PCIIEN bit in the QEIxSTAT register.

int main()
{
/*Remap of QEA, QEB, Index pins goes here*/

/*Clear position counter*/
POS1CNT = 0;

/*Enable QEI interrupt*/
_QEI1IE = 1;

/*Enable QEI interrupt on initializing the position counter on completion of homing process*/
QEI1STATbits.PCIIEN = 1;

/*Initialize QEIxIC register*/
QEI1IC = 0x50;

QEI1CONbits.CCM = 0;      // Position counter in quadrature mode
QEI1CONbits.PIMOD = 3;    // Initialize the position counter on completion of homing process
QEI1CONbits.ON = 1;    // Enable QEI module
}

void __attribute__((interrupt, no_auto_psv)) _QEI1Interrupt(void)
{
     _QEI1IF = 0;

    // Check if the source of interrupt is homing initialization
    if(QEI1STATbits.PCIIRQ) 
    {
         // User application code goes here
    }
}