Setup for PWM Operation

The following steps illustrate how to configure the CCP module for standard PWM operation:

  1. 1.Select the desired output pin with the RxyPPS control to select CCPx as the source. Disable the selected pin output driver by setting the associated TRIS bit. The output will be enabled later at the end of the PWM setup.
  2. 2.Load the timer period register T2PR with the PWM period value.
  3. 3.Configure the CCP module for the PWM mode by loading the CCPxCON register with the appropriate values.
  4. 4.Load the CCPRx register with the PWM duty cycle value and configure the FMT bit to set the proper register alignment.
  5. 5.Configure and start the Timer:
    • Clear the TMR2IF Interrupt Flag bit of the PIRx register. See the Note below.
    • Select the timer clock source to be as FOSC/4. This is required for correct operation of the PWM module.
    • Configure the CKPS bits of the T2CON register with the desired Timer prescale value.
    • Enable the Timer by setting the ON bit of the T2CON register.
  6. 6.Enable the PWM output:
    • Wait until the Timer overflows and the TMR2IF bit of the PIRx 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.