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.