22.4.2 Setup for PWM Operation

Take these steps 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 T2PR 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 CCPRx register with the PWM duty cycle value and configure the FMT bit to set the proper register alignment.
  5. Configure and start Timer2:
    • Clear the TMR2IF interrupt flag bit of the PIR4 register. See the Note below.
    • Select the timer clock source to be 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 T2ON bit
  6. Enable the PWM output pin:
    • Wait until the timer overflows and the TMR2IF bit of the PIR4 register is set. See the Note below.
    • Enable the CCPx pin output driver by clearing the associated TRIS bit
      Important: 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.