Close

QEI

A project log for dsPIC: from Blink to QEI

a simple project with dsPIC33

jakub-pnkJakub P̵̗̋n̵̻̾k̶͙͘ 02/20/2021 at 13:460 Comments

Quadrature encoder interface (QEI) does exactly what is in the name - provides an interface for quadrature encoders. The used dsPIC has two such interfaces.

My encoder is Faulhaber IE2-16 and is mounted to a small DC motor. This encoder has a resolution of 16 pulses per revolution and requires a supply voltage of 4-18V. I will power the encoder with 5V, and because the logic voltage of my dsPIC is 3.3V, I have to use a logic voltage converter for the two outputs.

Since MCC does not support QEI, I will have to set all the registers myself, including setting up the Peripheral Pin Select (PPS). PPS basically allows you to select which pin will serve what purpose.

First, I connected the encoder and the logic voltage converter. 

Now I need to set up the pins. Both are inputs, also need to be set to digital instead of default analog. PPS is not terribly difficult to set - simply setting the QEIA1R and QEIB1R registers to the pin number does the job.

void initQEI()
{
    _ANSELD10 = 0;          // set pin to digital (by default analog)
    _ANSELD11 = 0;          // set pin to digital (by default analog)
    _TRISD10 = 1;           // set pin to input
    _TRISD11 = 1;           // set pin to input
    __builtin_write_RPCON(0x0000);      // Unlock
    _QEIA1R = 75;           // assign pin to QEI
    _QEIB1R = 74;           // assign pin to QEI
    __builtin_write_RPCON(0x0800);      // Lock
    
    QEI1CONbits.QEIEN = 1;  //Enable Module
}

Now it's simply the matter of reading the POS1CNT register and displaying it on the display. 

And after a few hours of debugging, only to discover bad contact on the breadboard, it works!

Discussions