You can use the Capture/Compare/PWM module. The duty cycle is proportional to CCPR1L.
i have managed to write this , it's a simple pwm generator
however it's not working , i have tried it on ISIS
i have set the prescaler of timer 2 on 1
oscillator on 4 MHz
pr2 = 99 (so the frequency would be 10Khz)
CCPR1L:CCP1CON<5,4> = 0011001000 (for the duty cycle to be 50%)
____________________________________________________________________
; oscillator : 4 mHZ
#include <p16f877A.inc>
;***declare registeraion
__config 3fC4
ORG 0X00
GOTO MAIN
ORG 0X04
RETFIE
code
MAIN
BCF STATUS,RP0
CLRF PORTA ;clear content of port A
CLRF PORTB ;clear content of port B
CLRF PORTC ;clear content of port C
BSF STATUS,RP0 ;change to bank 1
MOVLW B'11111111'
MOVWF TRISA ;set port A as input
CLRF TRISB ;set port B as output
CLRF TRISC
movlw B'01100011' ; period = 0.0001 seconds
movwf PR2
bcf STATUS,RP0
movlw B'00110010' ; setting the duty cycle time to 50% or 0.00005 seconds
movwf CCPR1L
bcf CCP1CON,5
bcf CCP1CON,4
bsf STATUS,RP0
bcf TRISC,2 ; pin 2 of port c is an output
bcf STATUS,RP0
movlw B'00000100'
movwf T2CON ; prescale =1 , timer2 is on
bsf CCP1CON,3 ;configuring CCP1 module for PWM operation
bsf CCP1CON,2
END