26.9 Setup for PWM Operation Using PWMx Output Pins
Follow the next steps when configuring the module for PWM operation using the PWMx pins:
- Disable the PWMx pin output driver(s) by setting the associated TRIS bit(s).
- Clear the PWMxCON register.
- Load the TxPR register with the PWM period value.
- Load the PWMxDCH register and bits [7:6] of the PWMxDCL register with the PWM duty cycle value.
- Configure and start Timerx:
- Clear the TMRxIF Interrupt Flag bit of the PIRx register.(1)
- 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 CKPS bits of the TxCON register with the Timerx prescale value.
- Enable Timerx by setting the ON bit of the TxCON register.
- Enable the PWM output pin and wait until Timerx overflows; the TMRxIF bit of the PIRx register is set.(2)
- Enable the PWMx pin output driver(s) by clearing the associated TRIS bit(s) and setting the desired pin PPS control bits.
- Configure the PWM module by loading the PWMxCON register with the appropriate values.
Note:
- To send a complete duty cycle and period on the first PWM output, the above steps must be followed in the given order. If it is not critical to start with a complete PWM signal, then move step 8 to replace step 4.
- For operation with other peripherals only, disable PWMx pin outputs.