# PWM generation PIC16F628A

I have followed the instructions in the data sheet and tried to generate PWM signal using PIC16F628A. However it seems to be not generating any output. My code is shown below. Any help appreciated regarding finding the what is wrong in my code.

Thank you.

PROCESSOR '16F628A'
INCLUDE <P16F628A.INC>
org 0x00
bcf STATUS,RP1
bsf STATUS,RP0 ; go to bank1

movlw 0xff ;255 dec
movwf PR2 ; writing to PR2 register
bcf TRISB,3 ; set RB3 as output

bcf STATUS,RP0 ; go to bank0
bcf CCP1CON,5 ; clearing bits 5 and 4
bcf CCP1CON,4

movlw 0x00
movwf CCPR1L ; set duty cycle

bcf T2CON,1
bcf T2CON,0 ; set pre-scaler to 1
bsf T2CON,2 ; enable Timer2
bsf CCP1CON,3
bsf CCP1CON,2 ; enable pwm mode
return
end

-
Change banks using banksel, you are less likely to make mistakes. –  Leon Heller Apr 13 '14 at 12:16
Agreed. And if you really want to be efficient, create your own banksel macros. I did. And never use them because clarity is better than efficiency! –  carveone Apr 13 '14 at 17:21
The piclist mailing is still useful for this type of thing. I found the PWM section in the datasheet confusing but this link is clearer: piclist.com/techref/microchip/16f877/pwm.htm –  carveone Apr 13 '14 at 17:23
Thank you. It's really understandable :) –  user2835684 Apr 14 '14 at 12:11

movlw 0x00
movwf CCPR1L ; set duty cycle


Are you always just setting the duty cycle to 0? Is the output pin always low? Everything else looks good. Just try changing this number and see if you get any results.

-
Thanks a lot. That was the error! After changing the CCPR1L value PWM generated :) –  user2835684 Apr 13 '14 at 15:25

In your example, you are clearing CCPR1L. This is setting your duty cycle to zero, and so you aren't getting any output.

The opposite can happen, too. CCPR1L, along with two bits in CCP1CON, define the length of the pwm pulse. If this length is longer than the pwm frequency, then the pin never goes low, and it appears that there is no pwm output.

Try increasing CCPR1L. Everything else looks good.

Have fun!

-
Thanks you so much for your valuable advice. It worked! Actually I was struggling with this several days and finally thought about posting it here. Thank you. –  user2835684 Apr 13 '14 at 15:30
@user2835684 Glad to help :) –  bitsmack Apr 13 '14 at 15:47