Using the quadrature encoder interface on the Tiva C Launchpad

Quadrature encoders are used for measuring motor rotational speed and direction. They can be optical or magnetic just like the one below. This quadrature encoder has two Hall effect sensors which allow a program or quadrature encoder interface measure motor speed and direction.The figures above show the output from the two Hall effect sensors when the motor is running forwards and backwards. Notice how H1 has a falling edge when H2 is high on the left plot and it has a falling edge when H2 is high in the second plot. This allows a quadrature encoder interface to determine the motor direction.To measure motor speed, you need to measure pulse frequency and divide by the number of pulses produced during each cycle. The black disk in the photo above has a number of magnets embedded within it.The outputs from the Hall Effect sensors can be connected to the PhA and PhB inputs of the Tiva C on Port C (PC5,PC6).Code to initialize Quadrature Encoder Interface 1 on the Tiva C is shown below

void initQEI(void)
{

// Quadrature encoder is connected to PC5 and PC6

// These correspond to PhaseA1 and PhaseB1

SYSCTL_RCGCQEI |= (1 << 1); // enable the clock for QEI1

SYSCTL_RCGCGPIO |= (1 << 2); // enable GPIOC

SYSCTL_GPIOHBCTL |= (1 << 2); // enable AHB access to GPIOC

GPIOC_DEN |= (1 << 5) + (1 << 6); // digital mode for bits 5 and 6 of GPIOC

GPIOC_AFSEL |= (1 << 5) + (1 << 6); // alternate function mode for bits 5 and 6

GPIOC_PCTL &= ~((0x0f << 20) + (0x0f << 24)); // zero out pin control value for bits 5 and 6

GPIOC_PCTL |= ((6 << 20) + (6 << 24)); // zero out pin control value for bits 5 and 6

QEI1_CTL = 0x00000020;

QEI1_LOAD = 8000000; // This sets the timing window to 1 second when system clock is 16MHz


QEI1_MAXPOS = 1000;

QEI1_CTL |= 1;

}


The motor velocity can then be read as follows:

int getQEIVelocity()

{

int speed = QEI1_SPEED;

int direction = QEI1_STAT >> 1;

if (direction)

speed = -speed;

return speed;

}

This post was pug together on a mobile phone with a poor Internet connection. It will definitely need editing later 🙂

Leave a comment