Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help need regarding PWM(PIC16f877a)

Not open for further replies.


Newbie level 4
Mar 5, 2006
Reaction score
Trophy points
Activity points
pwm pic16f877a

Hi all,
I am a complete newbie in pic...and i need help desperately in interpreting the data sheet. What do they mean by (CCPR1L:CCP1CON <5:4>) in the equation PWM Duty Cycle = (CCPR1L:CCP1CON <5:4>).TOSC.(TMR2 Prescale Value). Is it possible to give me an example on how to use the equation? and for the PWM Period given in the data sheet the unit is in seconds?

When the servo motor data sheet says that it need a pulse duration from 0.9mS to 2.1mS with 1.5mS as center. What does it mean?

pic pr2 calculation timer

The colon ":" between CCPR1L and CCP1CON <5:4> is used as a separator for an easy interpreting of data format.
<5:4> means the bit 5 and bit 4 of the CCP1CON register (which has a size of 8 bits)
Thus expression CCPR1L:CCP1CON <5:4> means 8 bits (MostSignificantBits) of CCPR1L concatenated with 2 bits (LeastSignificantBits) of CCP1CON for building a 10 bit register.
It's exactly the PWM 10-bit resolution.
A PWM output has a time base (period) and a time that the output stays high (duty cycle).
For your servo motor the period is 3ms.
The pulse duration is the time during the output (CCP1 pin) stays high (duty cycle).
So for a pulse duration of 0,9ms, the output stays high 0,9ms then low 2,1ms again high 0,9ms then low 2,1 ms and so on...
For a pulse duration of 2,1ms, the output stays high 2,1ms, then low for 0,9ms, again high for 2,1ms and so on...
For a pulse duration of 1,5ms (as center), the output stays high 1,5ms then low 1,5ms then high again for 1,5ms and so on....(this is what is called 50% duty cycle)
It's obvious that for higher pulse period you get higher motion speed.

The PWM period given in the data sheet PWM Period = [(PR2) + 1] • 4 • TOSC • (TMR2 Prescale Value) is based on TOSC measure unit.
Because PR2 and TMR2 values has no unit measure, are registers loaded with values according to your needs.
So if your PIC16F877A is driven in HS Osc mode by 20 MHz XTAL, then the oscilator period TOSC is 50ns and PWM Period is measured in ns too.

For setup PWM of your application you have to choose a proper value for PR2 (initialized to FFh upon reset) and T2CON (bits 0 and 1 - prescaler) registers according to 3ms period and your XTAL value.
Then set CCPR1L register according to your duty cycle (0,9ms - 2,1ms), the XTAL value and previous setings of TMR2 prescaler (T2CON <1:0>).
You can forget about bits 5 and 4 of CCP1CON (leave them both of zero) - are the least significant bits of 10-bit concatenated register value because it won't affect your results.
But you must write to 1 bits <3:2> of CCP1CON to enable PWM mode. Thus you must write 00001100 to CCP1CON register
Next make CCP1 pin an output by clearing the bit 2 of TRISC register.
Enable Timer2 by writing to bit 2 of T2CON register.
That's all.

Every time when you wish to change the speed of the servo motor just write to CCPR1L register the new value.

If I've spent some time writing this post I'm expecting to have the patience and write yourself the code. Even if you're a newbie you have to give it a try one day.


    Points: 2
    Helpful Answer Positive Rating

If u r trying to control servos using PWM module, i think u cant use the module, unless u supply ur PIC with 1Mhz crystal. If u supply it with 20Mhz crystal, u cant produce a PWM using PWM module which is less than 50Hz.

tosc pic16f877

silvio said:
Every time when you wish to change the speed of the servo motor just write to CCPR1L register the new value.

We have 10 bit PWM duty cycle value,but we just change CCPR1L register.What about other two bits?

pwm 50hz bei 20mhz

Hey coshkun!

If you want to have 10 bit PWM resolution you should update the two bits in CCP1CON 5:4 as well.
Otherwise just put zeros in them - you'll have 8-bit resolution PWM. (they are the LSBs!)



wakaka said:
If u supply it with 20Mhz crystal, u cant produce a PWM using PWM module which is less than 50Hz.

Which is less than 1220HZ, my man.

For XTAL of 4096KHz, timer prescaler of 16, and PR2 value of 0xBF you got a period of 3ms, 2xling's request.
It's a PIC but don't teasing at 1MHZ.

optical encoders with pic16f877a

thanks all, i find that the posts are really useful!

pic16f877 pr2 register

how about the programming part??? how to adjust the period time?? i mean to make it faster and to make it slower by the input..

1). input- variable resistors which is analog signal
2). buttons/switches- ????

Added after 1 minutes:

and why inteerupt and compare is being used to control the speed for LED as indicator???

pwm pic16f877a

thanks to all.. the posts are really helpful. but right now i have a doubt regarding application of load to the motor. how can i compensate for the loss of rpm?

pwm pic16f877 8 bit input

Use optical encoders

pwm 10 bits pic16f

i have a doubt regarding the working of ADC.Can i move the value of ADRESL register to a port to watch the digital approximated value.. practically i aint getting the result.

Not open for further replies.

Part and Inventory Search

Welcome to