QEI hardware can be found onboard all variants of the
PIC18Fxx31 microcontrollers. The PIC-based QEI hardware
decodes motion sensor information provided by the optical
encoder outputs. The QEI can also be configured to sense
rotational velocity. In that we’re not spinning our optical
encoder with a motor shaft, we won’t cover the QEI
velocity configuration in this discussion. We’ll base our
project here on the 28-pin PIC18F2431. If your optical
encoder application requires a bit more I/O, you can use
the PIC18F2431 optical encoder logic and code we’re about
to discuss with the 40-pin PIC18F4431.
The PIC18F2431’s QEI consists of three inputs. In
addition to the QEA and QEB quadrature inputs, an index
signal input (INDX) is also provided. The index input is used
with encoders that emit an index pulse to mark an absolute
position of rotation. As you can see in Figure 1, our optical
encoder does not provide an index signal.
Registers and buffers dedicated to the QEI keep track
of the optical encoder’s direction of rotation and quantity
of optical encoder sequences. The UP/DOWN bit in the
QEICON (Quadrature Encoder Interface Control) register is
used to monitor the optical encoder’s direction of rotation.
A 16-bit up/down counter keeps track of the rotational
position. We’ll use the POSCNT register pair to generate a
numeric entry according to the value of the POSCNT
register. The maximum value of the POSCNT counter is
controlled by the value contained within the MAXCNT
register.
For instance, if we only want to count from 0 through
9, we load the MAXCNT register with 9. We then configure
the QEI to reset on a match of the POSCNT and MAXCNT
values. Thus, as we spin the optical encoder shaft the
POSCNT will increment or decrement between the values of
0 and 9. If the value is to roll off of 0 during a decrement
operation, the POSCNT counter is loaded with the MAXCNT
value. Otherwise, the QEI will sense the optical encoder
outputs and count from 0 to 9, then rollover to 0.
The PIC C compiler of choice for this project is HI-TECH
PICC- 18 PRO. Let’s use the HI-TECH PICC- 18 PRO tool to
configure the PIC18F2431’s QEI:
//***********************************************
//* CONFIGURE QEI
//***********************************************
QEICON = 0b10011000;
MAXCNTH= 0;
MAXCNTL
POSCNTH
POSCNTL
= 9;
= 0;
= 0;
Working the QEICON register bits from left to right,
setting the most significant bit of the QEICON register
disables the QEI’s velocity mode. We really don’t care how
fast we’re turning the optical encoder shaft as our intent is
data entry not motor control. The cool thing is that the QEI
hardware implementation is very fast and we would really
have to try hard to outrun the POSCNT counter rotating the
optical encoder shaft by hand.
Adding an LCD
We can twist the optical encoder’s shaft until the cows
come home and never be the wiser of the optical encoder’s
operational output. So, let’s add a visual output device to
our optical encoder input design. The LCD/optical
encoder/PIC18F2431 circuitry is graphically displayed as
Schematic 1. The QEI is pinned out on the PIC18F2431’s
PORTA. The optical encoder’s pushbutton is logically part of
the QEI. So, I selected RA0 for the optical encoder’s SPST
switch input. That leaves RA1 for any analog-to-digital
converter (ADC) work you might want to perform. I’ve also
intentionally left the RA2 pin open as you may need the
INDX functionality in your application.
Normally, the most significant nibble of the LCD data
I/O is connected to the most significant nibble of an eight-bit PIC I/O port when four-bit LCD mode is employed. The
SERVO 07.2010 45