0% found this document useful (0 votes)
51 views16 pages

6. Chapter 6 - QEI Module

The document discusses the Quadrature Encoder Interface (QEI), which is essential for detecting the position and speed of rotors in motor control applications. It explains the structure of encoders, the importance of digital filters, and the configuration of the QEI module, including the position counter and quadrature decoder modes. Additionally, it provides example code for initializing the QEI module and implementing PID control for a DC servo motor.

Uploaded by

thienbinhntb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views16 pages

6. Chapter 6 - QEI Module

The document discusses the Quadrature Encoder Interface (QEI), which is essential for detecting the position and speed of rotors in motor control applications. It explains the structure of encoders, the importance of digital filters, and the configuration of the QEI module, including the position counter and quadrature decoder modes. Additionally, it provides example code for initializing the QEI module and implementing PID control for a DC servo motor.

Uploaded by

thienbinhntb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

MICROCONTROLLERS

CHAPTER 6
QEI
QUADRATURE ENCODER INTERFACE

Assoc.Prof.Dr Tuong
Quan Vo
HCMUT - 2017
QEI
What is QEI?
- A Quadrature Encoder (or incremental encoder, or optical
encoder) is used to detect the position and speed of rotors,
enabling closed loop control in many motor control applications
like switched reluctance and induction motors.

Encoder structure

2
 2011 – Vo Tuong
QEI

Encoder structure

The index occurs only once per revolution and is used to


establish an absolute position. 3
 2011 – Vo Tuong
QEI
- Since these signals are heavily affected by noise  a digital
filter is available on each input. The filtered phase edges are
counted by a dedicated 16 bit up/down counter, also referred to
as the Position Counter (POSCNT).
- To establish a reference point for position and speed
measurements  the counter can be reset either by the index
signal or by a counter period match.

QEI Block Diagram

4
 2011 – Vo Tuong
QEI
INITIALIZING THE QEI MODULE
ENABLE DIGITAL FILTERS
- Enabling the digital filters is desirable to filter any possible
glitches on the incremental encoder signals.
- if and only if three consecutive samples have the same value
the input is considered stable and the value is output from the
filter

QEI Filter

5
 2011 – Vo Tuong
QEI
CALCULATE THE MINIMUM PULSE WIDTH

 Configure the filter to reject any signal lower than 15 MIPS


will be fine for the application.

6
 2011 – Vo Tuong
QEI
QUADRATURE DECODER
Determine the direction of rotation looking at the two incoming
phase signals, and generate the clock that will be used by the
position counter.
• (x2) mode: the decoder only generates a clock impulse at the
rising and falling edges of Phase A signal.
• (x4) mode: the clock pulses are generate at each edge of
phase A and Phase B.
The position counter can be reset either by the index pulse
coming from the encoder or by the matching of the current
position counter value with the number in the Maximum Count
Register

7
 2011 – Vo Tuong
QEI

QEI in x4 mode

8
 2011 – Vo Tuong
QEI
POSITION COUNTER
The position counter can be used either for position or speed
measurement.
To measure motor position, we must know the relationship
between the displacement and the number of phase pulses we
get from the encoder.
 For speed measurement application, the time interval between
two index pulses or count match events gives a measure of the
angular velocity.

9
 2011 – Vo Tuong
QEI
MCU with QEI module

10
 2011 – Vo Tuong
QEI
#include Example code
<p30f4011.h>
#include <pwm.h>
#include <qei.h>
void QEI_Init()
{
ADPCFG = 0xFFFF; // Configure QEI pins as digital inputs
QEICONbits.QEIM = 1; // (bit 10-8 Disable QEI Module) x4 mode
QEICONbits.QEISIDL = 0; // bit 13 Continue operation during sleep
QEICONbits.SWPAB = 0; // bit 7 QEA and QEB not swapped
QEICONbits.PCDOUT = 0; // bit 6 Normal I/O pin operation
QEICONbits.TQGATE= 1; // bit 5 Timer gated time accumulation
disabled QEICONbits.TQCKPS= 0; // bit 4-3 Timer Input Clock
Prescale Select bits

QEICONbits.POSRES = 0; // bit 2 Index pulse does not reset position


counter
QEICONbits.TQCS=0; // bit 1 Timer Clock Source Select bit=Internal clock

} 11
 2011 – Vo Tuong
QEI
DFLTCONbits.CEID = 1; // Count error interrupts disabled
DFLTCONbits.QEOUT = 0; // Digital filters output disabled for QEn
pins DFLTCONbits.QECK = 0; // clock divide for digital filter for QEn

} 12
 2011 – Vo Tuong
QEI
Exercise: DC Servo Motor Control
Control motor that follow the desired profile

13
 2011 – Vo Tuong
QEI
PID Code for motor control
// PID Code for motor control
void pid_cal()
{
EncoderCount = POSCNT - Count;
Count = POSCNT;
mposition +=
EncoderCount ; e =
position - mposition ; Ypid
= ceilf(e*Kp) ;
Intergral =Intergral + e;
Ypid = Ypid + ceilf(Ki*Intergral);
Ypid = Ypid + ceilf(Kd*EncoderCount);//Duty cycle
if Ypid > max
Ypid = max;
if Ypid < min
Ypid = min;
PDC1 = PDC2 = Ypid;
}
14
 2011 – Vo Tuong
QEI
PID Code for motor control
// PID Code for motor control
void pid_cal()
{
EncoderCount = POSCNT - Count;
Count = POSCNT;
mposition = mposition +EncoderCount ;
e = position - mposition ;
Intergral =Intergral + e;
Ypid = ceilf(e*Kp) + ceilf(Ki*Intergral)+ceilf(Kd*EncoderCount);
u= Ypid
if u > maxPID
u=
maxPID; if u< min
u = minPID;
PDC1 = PDC2 = u;
}

15
 2011 – Vo Tuong

You might also like