Hardware Reference
In-Depth Information
Solution: The ratio of 24 MHz and 50 Hz is 480,000. One of the possible breakdowns of this
number is 16 times 30,000. Therefore, one of the possible settings is as follows:
Select clock A as the clock source.
Set the prescaler to 16.
Select left-aligned mode.
Select polarity of the waveform to be 1.
Load the value of 30,000 into the PWMPER0:PWMPER1.
Load the value of 24,000 into the PWMDTY0:PWMDTY1.
The following instruction sequence will achieve the desired setting:
movb #0,PWMCLK
; select clock A as the clock source
movb #2,PWMPOL
; set PWM0:PWM1 output to start with high level
movb #4,PWMPRCLK
; set prescaler to 16
movb #$1C,PWMCTL
; concatenate PWM0:PWM1, stop PWM in wait mode
movb #0,PWMCAE
; select left align mode
movw #30000,PWMPER0
; set period to 30000
movw #24000,PWMDTY0
; set duty to 24000
bset
PWME,PWME1
; enable PWM0:PWM1
The PWM function can be used in many applications that require the average value of
output voltages. The lamp dimmers and DC motor speed control are two examples of the PWM
applications.
Example 8.24
Using PWM in dimming the light . Suppose we are using the PWM0 of the HCS12 to control the
brightness of a light bulb. The circuit connection is shown in Figure 8.51. Write a program so that
the light is turned down to 10 percent brightness gradually in 5 s. The E-clock frequency is 24 MHz.
Solution: We dim the light in the following manner: Use the PWM0 output to control the
brightness of the light bulb. Set the duty cycle to 100 percent at the beginning and then dim the
brightness by 10 percent in the first second, and then 20 percent per second in the following 4 s.
Use 100 as the initial duty and period values.
5 V
HCS12
R 1
Light
bulb
R 2
PWM0
Figure 8.51 Using PWM0 function to dim the light
 
Search WWH ::




Custom Search