Hardware Reference
In-Depth Information
Example 8.22
▼
Assume that the E-clock frequency is 24 MHz. Write an instruction sequence to generate a
square wave with a period of 20
μ
s and 60 percent duty cycle using PWM channel 0. Use center-
aligned mode.
Solution:
Select clock A with prescale factor 2 as the clock source to PWM0, then
20
μ
s
5
2/24
μ
s
3
PWMPER0
3
2
⇒
PWMPER0
5
120
Select the PWM0 waveform to start with high level. Then the value to be loaded into the
PWMDTY0 is computed as follows:
60%
5
(PWMDTY0
4
PWMPER0)
3
100%
⇒
PWMDTY0
5
72
We need to choose the following parameters for this waveform:
•
Clock source prescale factor set to 2
•
Clock A selected as the clock input to PWM channel 0
•
Center-aligned mode with PPOL0 equal to 1
•
The value 120 written into the PWPER0 register
•
The value 72 written into the PWDTY0 register
The following instruction sequence will confi gure the PWM0 properly:
movb #0,PWMCLK ; select clock A as the clock source
movb #1,PWMPOL ; set PWM0 output to start with high level
movb #1,PWMPRCLK ; set the PWM0 prescaler to clock A to 2
movb #1,PWMCAE ; select PWM0 center-aligned mode
movb #$0C,PWMCTL ; select 8-bit mode, stop PWM in wait mode
movb #120,PWMPER0 ; set period value
movb #72,PWMDTY0 ; set duty value
bset PWME,PWME0 ; enable PWM channel 0
The following C language statements will confi gure PWM0 properly:
PWMCLK
5
0;
// select clock A as the clock source to PWM0
PWMPOL
5
1;
// PWM0 output start with high level
PWMCTL
5
0x0C;
// select 8-bit PWM0
PWMCAE
5
1;
// PWM0 center aligned
PWMPRCLK
5
1;
// set clock A prescaler to 2
PWPER0
5
120;
PWDTY0
5
72;
PWEN |
5
PWME0; // enable PWM0
▲
Example 8.23
▼
Assume that the E-clock frequency is 24 MHz. Write an instruction sequence to gener-
ate a 50-Hz digital waveform with 80 percent duty cycle using the 16-bit mode from the
PWM1 pin output.
Search WWH ::
Custom Search