# PWM Calculation on PIC16F877

I have an assignment where the PIC is clocked at 16kHz and the output of the the PWM has to be on channel two. The PWM frequency has to be 400Hz and the duty cycle is 10%

Following the manual I derived the following values for the PR2 registers(PWM Period) and then the CCPRIL:CCP1CON<5:4> registers (PWM Duty Cycle) respectfully.

PWM PERIOD:

$$\ \ PR2 = ( \frac{PWM Period}{( T_{OSC} \times 4 \times \ TMR2 \ Prescale \ Value)} ) - 1$$

$$\ \ PR2 = ( \frac{0.0025 }{( 0.0000625 \times 4 \times \ 1)} ) - 1 = 9$$

PWM Duty Cycle in % = 10

PWM Duty Cycle (in time):

$$\ \small CCPRIL:CCP1CON<5:4> = \large ( \frac{ 0.1 \times \ 0.0025 }{( 0.0000625 \times \ 1)} )= 4$$

So PR2 = 0000 1001 and CCPRIL:CCP1CON<5:4> = 00 0000 0100

But my problem is that I used an online calculator and got the following result instead.

  /*
* PWM registers configuration
* Fosc = 16000 Hz
* Fpwm = 400.00 Hz (Requested : 400 Hz)
* Duty Cycle = 10 %
* Resolution is 5 bits
* Prescaler is 1
* Ensure that your PWM pin is configured as digital output
* see more details on http://www.micro-examples.com/
* this source code is provided 'as is',
* use it at your own risks
*/
PR2 = 0b00001001 ;
T2CON = 0b00000100 ;
CCPR1L = 0b00000000 ;
CCP1CON = 0b00111100 ;


I would have expected the LSB in CCPIR1L to be 1 and CCP1CON<5:4> to be 00 Since the value calculated is 4 and 4 in binary is 100 CCP1CON<5:4> contains the two LSBs of the duty cycle value. However it's not according to this calculator and I'm just wondering where I went wrong.

• Maybe you went wrong trusting the online calculator. – Spehro Pefhany Mar 22 '18 at 1:23
• @SpehroPefhany Simulated my values using Proteus, turns out the calculator was wrong. – Simeon R Mar 23 '18 at 16:36