Page 314 - Programming Microcontrollers in C
P. 314

A Pulse Width Modulation Program      299

            A Pulse Width Modulation Program

                              While the MC68HC16 has a built-in pulse width modulation
                          (PWM) system, it is sometimes desired to achieve more flexible reso­
                          lution than can be obtained with the built-in system. Therefore, it is
                          not unreasonable that one would want to use the general-purpose
                          timer to create a PWM. Prior to writing the code for a PWM in this
                          manner, we should look at the basic time period of the processor.
                          Unless otherwise directed, all of the timers in the GPT are driven by
                          the system clock. This signal is passed through a prescaler controlled
                          by the bits CPR2 through CPR0 in the register TMSK2. The default
                          prescaler value will cause a clock rate to the GPT of the system clock
                          divided by four.
                              The question now is the clock rate. Recall that the clock control
                          is a portion of the SIM. Within the SIM there is a register named
                          SYNCR. The clock frequency is controlled by the three bit fields
                          named W, X, and Y in this register. When operating at a low crystal
                          frequency—between 25 and 50 kHz—the formula for the system
                          frequency is given by

                                       (
                              F= F  4 y +1  )(2 2w+x  ) 
                               s    r              
                          where y has a value between 0 and 63, and both w and x can have
                          values of 0 or 1. With a frequency f of 32767 and the W, X, and Y
                          default values of 0, 0, and 63, respectively, the device will come out
                          of reset with a system frequency of 8.388 MHz. To be able to get the
                          finest resolution for our timing functions, let us plan to operate the
                          system clock frequency at its maximum value by changing the value
                          of X from its default value of 0 to 1. This change will cause the
                          system frequency to be 16.776704 MHz. The frequency of the input
                          into the GPT is one-fourth this value or 4.194176 MHz. The time
                          period for this frequency is 238 nanoseconds.
                              The code for a PWM on the MC68HC11 was shown in Chapter 5. A
                          program that implements a PWM on the MC68HC16 is shown below.

                   /* This program provides a pwm output to OC3. The period
                       will be the integer value found in pwm_period, and the
                       on time will be the integer value found in pwm_count.
                       Keep pwm_count less than pwm_period. */
   309   310   311   312   313   314   315   316   317   318   319