PIC16F887 PWM Lesson 13
PIC16F887
PWM
Signals of varying frequency and duty cycle
have a wide range of application in automation.
A typical example is a power control circuit.
Refer to figure below. If a logic zero (0)
indicates the switch-off and a logic one (1)
indicates the switch-on, the electrical power that
load consumers will be directly proportional to
the pulse duration. This ratio is often called Duty
Cycle.
PIC16F887
PWM
The figure above shows the block diagram
of the CCP1 module set in PWM mode. In
order to generate a pulse of arbitrary form
on its output pin, it is necessary to set
pulse period (frequency) and pulse
duration.
PIC16F887
PWM PERIOD
The output pulse period (T) is
determined by the PR2 register of the
timer TMR2. The PWM period can be
calculated using the following equation:
Period=(PR2 +1) * 4Tosc * TMR2 Prescale
If the PWM period (T) is known, then it
is easy to determine the signal
frequency F because these two values
are related by equation F=1/T.
PIC16F887
PWM DUTY CYCLE
The PWM duty cycle is specified by using
in total of 10 bits: eight MSbs of the
CCPR1L register and two additional LSbs
of the CCP1CON register (DC1B1 and
DC1B0). The result is a 10-bit number
contained in the formula:
Pulse Width = (CCPR1L,DC1B1,DC1B0) *
Tosc * TMR2 Prescale Value
PIC16F887
PWM MAIN COMMANDS
setup_timer_2 (mode, period, postscale)Initializes timer 2 command with:
mode:T2_DISABLED : disable timer 2
T2_DIV_BY_1 : prescaler 1
T2_DIV_BY_4 : prescaler 4
T2_DIV_BY_16 : prescaler 16
Period: 0-255
determines when the clock value is reset
Postscale: 0-16
determines how many timer overflows
PIC16F887
PWM MAIN COMMANDS
setup_ccpX (CCP_PWM)
Set CCP to PWM mode with:
X: 1-2
determines which CCP used
PIC16F887
PWM MAIN COMMANDS
set_pwmX_duty (value)
Writes the 10-bit value to the PWM
to set the duty with:
X: 1-2
determines which CCP used
value: 10 bit number
PIC16F887
EXAMPLE
Control 24V DC motor speed by
PWM pulse with period = 0.8 ms
and duty cycle belong to the
voltage value read from variable
resistor connected to AN0.
PIC16F887
ANALYZE
Period=(PR2 +1) * 4Tosc * TMR2 Prescale
with period =0.8ms, fosc =20Mhz
Choose
TMR2 Prescale =16
PR2 = 249
Max duty cycle = (PR2+1)*4=1000
PIC16F887
L298
OPERATING SUPPLY VOLTAGE
UP TO 46 V .
TOTAL DC CURRENT UP TO 4 A .
LOW SATURATION VOLTAGE .
OVERTEMPERATURE
PROTECTION
LOGICAL "0" INPUT VOLTAGE
UP TO 1.5 V (HIGH NOISE
IMMUNITY)