Beruflich Dokumente
Kultur Dokumente
• Capture: The CCP pin can be set as an input to record the arrival
time of a pulse.
• 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
•X = 250 (=FA H)
= 0011 1110 10 …10-bit PWM
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.