On the Microchip dsPIC33F family, the PWM period PTPER is calculated as:

PTPER = (FCY/FPWM)*(PTMR Prescaler Value) - 1

where PTMR is the input clock to the PWM and FCY = Fosc/2, FCY being the instruction cycle frequency and Fosc the main clock frequency. It is specified that the input clock to PTMR is Fosc/4 (ref. doc: DS70165D-page 178). Does this mean that the period is calculated in instruction cycle clock ticks, or twice the instruction cycle clock ticks? Does this information relate to why multiply PTPER by 2 in the following line of code (taken from Microchip Application Note AN957):

PDC1 = (int)(((long)(PTPER*2)*(long)DutyCycle) >> 15); // why multiply PTPER by 2?
PDC2 = PDC1;                      // PDCx is the duty cycle register for PWMx channel
PDC3 = PDC2;                      // x = 1,2,3

In my understanding, the factor of 2 shouldn't be there. A more complete version of this code was posted yesterday at this forum: Help understanding the code

  • \$\begingroup\$ The clock prescaler beings used is 1:1 \$\endgroup\$ – Adeel Apr 8 '15 at 9:45

I found the answer to this question. In this MCU family, the PWM timer clock is slower than the duty cycle clock. If a 1:1 prescaler is selected for PWM clock, for each timer clock tick there are two duty cycle clock ticks. Therefore, the factor of 2 is multiplied in duty cycle calculation.

Duty cycle clock period = TCY/2; PWM Timer PTMR Clock period = TCY (1:1 prescaler used)

Source: dsPIC33F/PIC24H Family Reference Manual - DS70187E (pp. 14-34 & 14-35)

  • \$\begingroup\$ Self explanatory. \$\endgroup\$ – Adeel Apr 14 '15 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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