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.

org 0x00
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
  • \$\begingroup\$ Change banks using banksel, you are less likely to make mistakes. \$\endgroup\$ Apr 13, 2014 at 12:16
  • 1
    \$\begingroup\$ 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! \$\endgroup\$
    – carveone
    Apr 13, 2014 at 17:21
  • \$\begingroup\$ 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 \$\endgroup\$
    – carveone
    Apr 13, 2014 at 17:23

2 Answers 2

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.

  • \$\begingroup\$ Thanks a lot. That was the error! After changing the CCPR1L value PWM generated :) \$\endgroup\$
    – nrnw
    Apr 13, 2014 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!

  • \$\begingroup\$ 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. \$\endgroup\$
    – nrnw
    Apr 13, 2014 at 15:30
  • \$\begingroup\$ @user2835684 Glad to help :) \$\endgroup\$
    – bitsmack
    Apr 13, 2014 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.