English
Language : 

PIC18LF24K Datasheet, PDF (273/594 Pages) –
PIC18(L)F26/45/46K40
21.5.2 SETUP FOR PWM OPERATION
The following steps should be taken when configuring
the CCP module for standard PWM operation:
1. Use the desired output pin RxyPPS control to
select CCPx as the source and disable the
CCPx pin output driver by setting the associated
TRIS bit.
2. Load the PR2 register with the PWM period
value.
3. Configure the CCP module for the PWM mode
by loading the CCPxCON register with the
appropriate values.
4. Load the CCPRxL register, and the CCPRxH
register with the PWM duty cycle value and
configure the FMT bit of the CCPxCON register
to set the proper register alignment.
5. Configure and start Timer2:
• Clear the TMR2IF interrupt flag bit of the
PIR4 register. See Note below.
• Select the timer clock source to be as
FOSC/4 using the TxCLKCON register. This
is required for correct operation of the
PWM module.
• Configure the T2CKPS bits of the T2CON
register with the Timer prescale value.
• Enable the Timer by setting the ON bit of
the T2CON register.
6. Enable PWM output pin:
• Wait until the Timer overflows and the
TMR2IF bit of the PIR4 register is set. See
Note below.
• Enable the CCPx pin output driver by
clearing the associated TRIS bit.
Note:
In order to send a complete duty cycle and
period on the first PWM output, the above
steps must be included in the setup
sequence. If it is not critical to start with a
complete PWM signal on the first output,
then step 6 may be ignored.
21.5.3 TIMER2 TIMER RESOURCE
The PWM standard mode makes use of the 8-bit
Timer2 timer resources to specify the PWM period.
21.5.4 PWM PERIOD
The PWM period is specified by the PR2 register of
Timer2. The PWM period can be calculated using the
formula of Equation 21-1.
EQUATION 21-1: PWM PERIOD
PWM Period = PR2 + 1  4  TOSC 
(TMR2 Prescale Value)
Note 1: TOSC = 1/FOSC
When TMR2 is equal to PR2, the following three events
occur on the next increment cycle:
• TMR2 is cleared
• The CCPx pin is set. (Exception: If the PWM duty
cycle = 0%, the pin will not be set.)
• The PWM duty cycle is transferred from the
CCPRxL/H register pair into a 10-bit buffer.
Note:
The Timer postscaler (see Section
20.3 “Timer2 Interrupt”) is not used in
the determination of the PWM frequency.
 2016 Microchip Technology Inc.
Preliminary
DS40001816C-page 273