Order this document by:
AN1215/D
MOTOROLA INC, 1996
SEMICONDUCTOR
APPLICATION NOTE
MOTOROLA
PID Routines for MC68HC11K4 and
MC68HC11N4 Microcontrollers
By James W. Gray
INTRODUCTION
PID (proportional, integral, derivative) compensation is one of the most common forms of closed-loop con-
trol. Control of closed-loop systems that require compensation is a growing area of application for embed-
ded microprocessors. In these systems, analog signals must be converted into discrete digital samples
before compensation or filtering can take place. Loop performance dictates the sampling rate, and calcula-
tions must be complete before the next sample time begins. These loop-related constraints and the Nyquist
frequency requirement place an upper bound on digital control of closed systems with feedback error. If the
controlled system has a resonance or other behavior with a time constant shorter than the sample and cal-
culation time, chaos is the most likely outcome. Despite these limitations, increases in microprocessor clock
rates and the addition of on-chip control-oriented hardware are expanding the number of medium perfor-
mance control applications handled by 8-bit machines. While an expensive DSP-class processor is the cor-
rect choice for the most demanding applications, several members of the M68HC11 family have the speed
and resources to control multiple PWM channels.
This note provides two working examples of PID control-loop software. The first example, written primarily
in C, shows a PID algorithm in a straightforward way using floating-point math. Key features of the C envi-
ronment are covered for readers who are more used to assembly language. The second example imple-
ments a PID algorithm in assembly language. It uses the MC68HC11N4 on-chip math coprocessor to speed
up arithmetic operations.
Both examples are complete and ready to run on a Motorola M68HC11EVS evaluation board. External in-
terfacing is identical for both examples — an 8-bit analog to digital converter is used for input, and an 8-bit
PWM waveform is output. Because the code in both examples carries more than 16 bits of precision, and
because both processors support 16-bit PWM, only minor changes are needed to increase precision. Power
amplifiers, sensors, and other interface circuitry must be supplied in order to experiment with real-world sys-
tems — a simple RC circuit is used for software checkout.
C and assembly language source code and loadable object code can be obtained from Motorola Freeware
Data Systems. Modem access: (512) 891-3733. Internet access: freeware.aus sps.mot.com. Web access:
http:///www.freeware.aus.sps.mot.com.
THE MICROCONTROLLERS
The MC68HC11K4 and MC68HC11N4 are 16-MHz devices with nonmultiplexed external address and data
buses. Each has 24 Kbytes of on-chip ROM or EPROM. Both devices have multiple PWM channels with
programmable period, duty cycle, polarity, and clock source. In both, two 8-bit channels can be concatenat-
ed to generate a 16-bit PWM output. The MC68HC11N4 also has two additional 12-bit PWM channels and
two digital to analog converter channels with 8-bit resolution.