iIn a 4 phase dc motor control application, I have to excite the coil using Mosfet with the pwm of 12.5 Hz with the train of pulses of 12 khz on each phase. So I am in a position to create 4 different pwms each with the above mentioned frequencies. I have planned to use Pic 18f45k80 which has four CCP modules. But these modules use only two timers TIMER 2 and TIMER 4 for PWM. So I could create only 2 different pwm at a time. Is there a way to create 4 pwms using PIC 18f45k80 with using CCP modules? I like to create a pwm waveform like the image that I have added.

  • \$\begingroup\$ If all of the PWMs need to be at 12 kHz, what's the problem? \$\endgroup\$
    – Dave Tweed
    Commented Mar 21, 2014 at 20:24
  • \$\begingroup\$ Each pwm must start nearly at the end of other pwm. how to do it? \$\endgroup\$
    – user37000
    Commented Mar 22, 2014 at 11:11

1 Answer 1


Section 19.4 of the datasheet discusses PWM operation. In reality, the PWM functionality uses only Timer 2. This timer controls the period (frequency) as explained by equation 19-1:

PWM Period = [(PR2) + 1] • 4 • TOSC • (TMR2 Prescale Value)

Thus, so long as the frequency is common to all 4 outputs, you are all set there. Then, you can set each duty cycle independently using the CCPRxL and CCPxCON registers where x is equal to 1, 2, 3, or 4 depending on which duty cycle you want to set. Equation 19-2 explains how to calculate the duty cycle:

PWM Duty Cycle = (CCPR4L:CCP4CON<5:4>) • TOSC • (TMR2 Prescale Value)

Now, when timer 2 equals one of the 10 bit values in CCPRxL and CCPxCON, that particular pin will toggle. Then, when timer 2 matches the value in PR2, all pins will toggle back to the original state.

  • \$\begingroup\$ The pwm for each phase must start nearly at the end of the other. Could you please tell me, Can I achieve the waveform like the above using CCP modules in Pic !8f45k80. \$\endgroup\$
    – user37000
    Commented Mar 22, 2014 at 6:45
  • \$\begingroup\$ Oh, I see now. The picture helps. In that case, I wouldn't use the PWM. I would use the Compare mode of the CCP module (Section 19.3). When the cycle starts, turn on MOSFET 1. Set the CCPRx register so that it will match the timer when the phases need to to switch. Then, rather than having an output pin change, just have it trigger the software interrupt. In the interrupt routine, turn off the current FET, turn on the next FET, and reset the timer. If this is what you are looking for, I will edit my answer. \$\endgroup\$ Commented Mar 22, 2014 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.