Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Timer Applications
Time delay
Pulse wave generation
Pulse width or frequency measurement
Timer as an event counter
Capture: The CCP pin can be set as an input to record the arrival
time of a pulse.
Compare: The CCP pin is set as an output, and at a given count,
it can be driven low, high, or toggled.
Pulse width modulation (PWM): The CCP pin is set as an output
and the duty cycle of a pulse can be varied.
The count for the period and the duty cycle are loaded into CCP
registers.
In this mode, the duty cycle of the output pulse can be varied.
Basic operation
PWM
When Ymin =0
Example
Timer setting
Use Timer2 as the base timer of CCP1 through CCP5 for PWM mode
Enable Timer3 in 16-bit mode with 1:1 prescaler
Set Prescaler to Timer2 to 1:4
Period register value: PR2 = 32MHz /[4x4x10KHz]-1 = 199
Duty Cycle Value: CCPR1L = PR2+1*DutyCycle = 200x40% = 80
We ignor the least two bit that can be used as multiplication factor
00 no effect, , 11 multiply by 4
Another Example
The main trap is that the PWM period is set by a 8-bit value and the duty
cycle is controlled by a 10-bit value.
Example :
lets assume the period value is 99, thus the TMR2 value (8-bit value)
should grow from 0 to (99+1) = 100 (reset value)
and then the duty cycle value should vary from 0 (0% duty cycle) to
400 (100% duty cycle).
In our case
If we use our PIC in OSC=8Mhz and the Timer2 prescaler=4
then the max output frequency when PR2=0.., freq=500000Hz
the min output frequency when PR2=255.., freq=1953.125Hz
PR2 value not exceed the 8bit max value (255)
Duty cycle cant exceed 4*(PR2+1) in 10bit mode
Duty cycle cant exceed DC*PR2 in 8 bit mode
If PR2 value exceed 255 we can change the prescaler factor
Example
Fosc = 48MHz
TMR2 is cleared.
Pin RC2/CCP1 of PORTC is set high.
The PWM duty-cycle byte is latched from
CCPR1L into CCPR1H.