Page 262 - Programming Microcontrollers in C
P. 262
Timer Operations 247
output compare subsystems. When one of the mask bits, say OC1M5,
is set in the OC1M and a compare occurs on OC1, in addition to the
normal operation that usually occurs when OC1 happens, the contents
of the corresponding bit, OC1D5, in the OC1D register is sent to the
output compare pin, which in this case is OC3. Completely
independent of the operation of OC1, OC3 could be set to toggle at
some time. Output Compare 3 could be programmed to be a PWM
output where Output Compare 1 establishes the period of the output,
and Output Compare 3 establishes the on time.
The applications for use of the coupled output compares are
unlimited. An accurate PWM is but one of several. These outputs
could be set up to establish an output sequence to drive a stepper
motor. The control of acceleration or deceleration of the motor is
easily controlled by merely selecting the base time used with OC1.
If you recall, the output compare-based PWM system discussed
for the MC68HC05 had several limitations. For example, that system
required that the system operate from interrupt service routines. The
latency time of interrupt service was so long that the minimum pulse
width was considerably longer than one would expect. Also, the
interrupt service timing dictated the maximum on time for the pulse,
which again was not nearly 100%. Let us look at how we would do a
PWM system with the coupled output compare systems.
#include “hc11e9.h”
/* This program will provide a PWM output to OC3, or
PA5. The period will be the integer value
found in period, and the on time will be the
integer value found in time_on. Keep time_on
less than period. */
#define PERIOD 0X1000
#define TIME_ON 0x0800
WORD period=PERIOD, time_on=TIME_ON;
main()
{
OC1M.OC1M7=ON; /* sent OC1 out to PA7 */