AN1215/D
MOTOROLA
3
The form which can be executed directly on the microprocessor is:
x (t)
=
KP e (t)
+
KI (Gdt
T
2
This term is added to the current output and put into the PWM control register at the beginning of the next
calculation cycle. Substituting the microcode labels for constants and variables into EQ. 4 and using C lan-
guage operator notation gives:
NEWDTY = KP
(ERRX) + KI
((ERRX - ERRM3X) + 3
PERDT
(CMNDX - (ADRCX + ADRCXM1) / 2) + (KD / (6
(ERRM1X - ERRM2X)) + OLDDTY
PERDT))
The function of the proportional term is clear, but the derivative and integral terms may need a brief expla-
nation. When a system with only proportional control is off the specified setpoint, the controller will increase
the control voltage until the error signal is zero, and the system thus returns to the setpoint with more applied
voltage than is required for maintaining equilibrium. This causes overshoot and, as the process continues,
under-damped ringing. The derivative term contributes proportionally to the error rate of change, but with
the opposite sign of the proportional term. If the proper constants are chosen, critical damping can be
achieved. The role of the integral term is to eliminate steady state error. A system that has a steady state
error when tracking a ramping input function can use an integral term to integrate the error over time and
compensate for it.
C LANGUAGE IMPLEMENTATION
This version of the PID control routine illustrates use of high-level language for control applications. High-
level language offers many conveniences not available in assembly language. Here are a few instances:
Memory-mapped registers can be defined in a single file, which can be included in any function.
Since C is a strongly-typed language, the compiler can identify data-type mismatches, such as writing
a 16-bit integer to an 8-bit port.
Most C compilers for the M68HC11 family allow direct vectoring to interrupt service routines. Interrupt
functions are usually defined for a special memory segment with a base address and a function name:
interrupt [INTVEC_START + 26] void RTI_interrupt(void);
When an interrupt service routine is written it can be declared as an interrupt function instead of a nor-
mal subroutine:
i
nterrupt void RTI_interrupt(void)
The function is compiled with a terminating RTI instruction, eliminating the need for a patch between
hardware interrupts and C subroutines. The void statements indicate that no arguments are being
passed to or from the interrupt routine.
One of the most attractive features of contemporary C compilers is the ability to add floating point math
with an “include math.h” statement. Even when the final application can't afford the time or code space
for floating point calculations, use of floating point math during debugging provides an excellent means
of testing new algorithms.
Now to examine the control routine itself (refer to Appendix A for a complete C listing). After necessary files
are included and floating point variables are declared, a prototype is given to define an assembly language
function that is used later.
G(Kt )
+
G (k
1)T]))
(
+
KD
6T
((e (kT)
e (k
3)
+
3 e(k
1)
e (k
2)))