Page 217 - Programming Microcontrollers in C
P. 217
202 Chapter 4 Small 8-Bit Systems
Thus, we have created a simple digital-to-analog converter. Many
control applications need analog control signals for parts of the sys
tem. PWM systems on microcontrollers provide an excellent means
for providing these analog voltages, so long as the limitations we
will discuss below are acceptable to the total system design.
The built-in PWMs in the M68HC05Bx family of devices pro
vide you with one of two pulse periods: the programmable timer
clock frequency divided by 256 or 4096. These frequencies are fixed,
and the interval is divided into 256 parts. Therefore, the analog fre
quency ranges—and the accuracy of the analog reproduction—is
limited to one part in 256 over the range of the output.
When using the output compare system to create a PWM, it is
possible to achieve a finer analog resolution at slower frequencies,
or perhaps a faster signal with poorer accuracy. A PWM created from
an output compare system is somewhat more flexible than the usual
built-in PWM. With the PWM systems built around the output com
pare system, the computer is responding to frequent interrupts. When
an interrupt occurs, the system status is pushed onto the stack and the
address of the interrupt service routine is placed into the program
counter. It takes approximately 10 clock cycles to respond to an in
terrupt after the completion of the executing instruction. Return from
an interrupt requires 9 clock cycles. Therefore, it is not possible to
accomplish the full goal of a PWM with an output compare system.
The goal in this case is to achieve full on at the one extreme input
level and full off at the other. The times required to process an inter
rupt will limit the performance at one extreme, the other or perhaps
both when a PWM is implemented with an output compare system.
A first example is shown in the following listing. This example
shows the interrupt service routine along with the necessary defines
only. It is assumed that the output compare interrupt bit along with the
interrupt bit are properly set to allow the output compare 2 to interrupt
the processor. Also shown in this example is code to control a second
output compare—1—to provide the system with a fixed time base.
In operation, the applications portion of the program must place
a pair of complementary numbers, pwm_number and off_count,
in place. The sum of these two numbers must equal a constant that
establishes the period for the PWM signal. For the M68HC05B4, the