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
-
\$\begingroup\$ Change banks using banksel, you are less likely to make mistakes. \$\endgroup\$Leon Heller– Leon Heller2014年04月13日 12:16:04 +00:00Commented 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– carveone2014年04月13日 17:21:26 +00:00Commented 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– carveone2014年04月13日 17:23:01 +00:00Commented 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– nrnw2014年04月13日 15:25:30 +00:00Commented 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.
pwm
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– nrnw2014年04月13日 15:30:42 +00:00Commented Apr 13, 2014 at 15:30
-
\$\begingroup\$ @user2835684 Glad to help :) \$\endgroup\$bitsmack– bitsmack2014年04月13日 15:47:15 +00:00Commented Apr 13, 2014 at 15:47