Sie sind auf Seite 1von 20

Capture, Compare, and PWM (CCP) Modules

• CCP modules have the Registers specially designed to perform the


following functions (in conjunction with timers)

• 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.
CCP
• Depending on family member, 0-5 CCP modules

• T3CON is used to choose the timer for compare and capture modes.
• Each CCP module has 3 registers associated with it.
1. CCPxCON
2. CCPRxL
3. CCPRxH
CCP Modules
•Capture, Compare, and PWM registers
•CCPR1H (high) and CCPR1L (low)
–16-bit Capture register
–16-bit Compare register
–Duty-cycle PWM register
•Timer1 or 3 used as clock for Capture and Compare
•Timer2 used as clock for PWM
CCPCON
The CCP Registers
PWM Mode
•CCP module with Timer2
–Output a pulse wave form for a given frequency/duty cycle
•Duty cycle : CCPR1 register
•Period : PR2 register
•When TMR2 is equal to PR2
–TMR2 is cleared
–Pin RC2/CCP1 of PORTC is set high
–PWM duty-cycle byte loaded into CCPR1
PWM
In Pulse-Width Modulation (PWM) mode, the CCPx pin produces up to a
10-bit resolution PWM output.
Steps for programming the PWM mode

1. Set the PWM period by writing to the PR2 register.


2. Set the PWM duty cycle by writing to the CCPR1L for the higher 8 bits.
3.Set the CCP pin as an output.
4.Set the prescale value by using T2CON reg.
5.Clear the TMR2 reg.
6.Configure the CCP1CON reg. for PWM and set DC1B2:DC1B1 bits for
decimal portion of the duty cycle.
7.Start Timer2.
Role of CCPR1H in PWM
1. CCPR1L is loaded into CCPR1H as we start Timer2.
2. Also CCP1 pin goes High to start the TON period.
3. As TMR2 counts up, TMR2 values are compared with both CCPR1L
and PR2
4. When TMR2= CCPR1H( i.e. CCPR1L also), CCP pin is forced low.
5. TON ends.
6. TMR2 keeps counting up until value matches the PR2.
7. When they match, CCP pin goes high.
8. That’s the end of PWM period and beginning of new one.
9. Clears Timer2 for next round.
10. CCPR1L is loaded into CCPR1H, process continues.
PWM Duty Cycle

PWM Duty Cycle =


(CCPRxL:CCPXCON<5:4>) x TOSC x (TMR2 Prescale Value)

•CCPRxL and CCPxCON<5:4> can be written to at any time, but


the duty cycle value is not latched into CCPRxH until after a
match between PR2 and TMR2 occurs (i.e., the period is
complete).

•In PWM mode, CCPRxH is a read-only register.


10 bit PWM
10 bit register for PWM duty cycle:
- Upper 8 bits from CCPR1L
- Lower 2 bits from CCP1CON( bits 5&4)

Lower 2 bits form decimal point portion of the duty cycle.


DC1B2 DC1B1 Decimal points
0 0 0
0 1 0.25
1 0 0.5
1 1 0.75
PWM : T2CON Register
PWM: PIR1 Register(TMR2IF Flag)
Calculations of PR2 and CCPR1L
To find the count in PR2:
fosc= 20 MHz; fpwm= 2.5kHz
tpwm = PR2 x Prescalar x (4 x tosc)
0.4 ms = PR2 x 16 x 4x (1/20) MHz
PR2= 125 (=7D H)
To find the count in CCPR1L:
For 50% duty cycle, ton = 0.2 ms
Calculations of PR2 and CCPR1L

•X = 250 (=FA H)
= 0011 1110 10 …10-bit PWM

Discard two LSB bits if that much precision is not needed.


= 0011 1110 ( =3E H)
Calculate for 75% Duty Cycle

X = 375 (= 177 H)
= 0001 0111 0111
= 5E H
PWM program
/* Calculations
* Fosc = 48MHz
*
* PWM Period = [(PR2) + 1] * 4 * TMR2 Prescale Value / Fosc
* PWM Period = 200us
* TMR2 Prescale = 16
* Hence, PR2 = 149 or 0x95
*
* Duty Cycle = 10% of 200us
* Duty Cycle = 20us
* Duty Cycle = (CCPR1L:CCP1CON<5:4>) * TMR2 Prescale Value / Fosc
* CCP1CON<5:4> = <1:1>
* Hence, CCPR1L = 15 or 0x0F
*/
PWM program
1. Assign C1 and C2 as output port pins
2. Write initial value to port pin C1 as 0 (Low)
3. Configure CCP1CON register for PWM mode, and Duty cycle LSBs as 1:1
4.Configure T2CON register for Prescaler =16 and Timer 2 = OFF
5. Write appropriate value in PR2 register (as per calculation of PWM time
period)
6. Start Timer2
7. Load the appropriate value ( as per desired duty cycle calculation) in
CCPR1L register.

Das könnte Ihnen auch gefallen