LISTING 2: Hardware PWM sample program.
duty VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)
Dutycycle specifies the on/off
(high/low) ratio of the signal.
TRISC.5 = 0
CCP1CON = %00001100
T2CON = %00000101
' Set PORTC.5 (CCP1) to output
' Set CCP1 to PWM
' Turn on Timer2, Prescale=4
Frequency is the desired frequency of the PWM signal.
' Use formula to determine PR2 value for a 1KHz signal,
' 4MHz clock, and prescale=4. (4E6/(4*4*1E3))-1=249
PR2 = 249
Now, one drawback is
that you lose a little of the
duty cycle’s 10-bit resolution
because PICBASIC PRO
gives you an eight-bit value
to work with. A value of 255
is 100% duty cycle and a
value of 0 is 0%. Therefore, a
20% duty cycle would equal
51 (0.20 * 255 = 51).
The program in Listing 3
shows the simplified HPWM
version. The DEFINE
statements select the port
and pin of the ECCP pin. The
PIC16F690 hardware PWM
pin is called the CCP1 pin,
and it’s located at PORTC bit
5. The duty cycle is initially
set to 51, but the program
will increase it after a delay
to make the duty cycle
change in a continuous loop.
The frequency is set to 1,000 in the HPWM command,
making the frequency fixed at 1 kHz.
The waveform created by this example was captured
with the Logic Tool (see Figure 9). You can see the pulse
width change. I actually increased the value of the variable
“duty” to create increments of 10%, not just the value 10
shown in the sample program to get this waveform close-up.
The hardware for this article is shown in Figure 10. I
used a few short pieces of wire to connect the PICkit 2 Logic Tool to the demo
board’s expansion header, which has a
Vdd, Vss, and CCP1 pin connection.
' Set PR2 to get 1KHz out
' Use formula to determine CCPR1L:CCP1CON<5:4> value for
' ends of range 20% to 80%. (249+1)*4*0.2=200 (20% value)
' (249+1)*4*0.8=800 (80% value)
duty = 200
' Set duty cycle to 20%
loop:
CCP1CON.4 = duty.0
CCP1CON.5 = duty.1
CCPR1L = DUTY >> 2
' Store duty to registers as
' a 10-bit word
duty = duty + 10
' Increase duty cycle
' Since the total sweep of duty is 600 (800-200) and
' we are adding 10 for each loop, that results in 60
' steps min to max. 1 second divided by 60 = 16.67mS
Pause 17
' Pause 1/60 of second
IF (duty < 800) Then loop ' Do it again unless 80% duty cycle
duty = 200
' Reset to 20% duty cycle
GoTo loop
' Do it forever
reduces them down to a couple of DEFINE statements and
a single command called “HPWM.”
The command line is:
HPWM Channel, Dutycycle, Frequency
Channel specifies which hardware PWM channel to use.
■ FIGURE 9. HPWM sample program signal.
LISTING 3: HPWM example program.
duty VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)
CONCLUSION
I hope this short description
DEFINE
DEFINE
CCP1_REGPORTC
CCP1_BIT5
' HPWM 1 Pin Port
' HPWM 1 Pin Bit
' HPWM Channel,Dutycycle,Frequency
'
' Channel - specifies which hardware PWM channel to use.
'
' Dutycycle - specifies the on/off (high/low) ratio of the signal. It ranges
' from 0 to 255, where 0 is off (low all the time) and 255 is on (high) all the
' time. A value of 127 gives a 50% duty cycle (square wave).
'
' Frequency - is the desired frequency of the PWM signal.
'
' Use formula to determine values to sweep Duty Cycle
' from 20% to 80%. 2 0 = 0%, 255 = 100%.
' 20% * 255 = 51, 80% * 255 = 204
continued ...
■ FIGURE 10.
Dual PICkit 2
programmers.
20
September 2008