Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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
share|improve this question
    
Change banks using banksel, you are less likely to make mistakes. –  Leon Heller Apr 13 at 12:16
1  
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 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 at 17:23
    
Thank you. It's really understandable :) –  user2835684 Apr 14 at 12:11
add comment

2 Answers 2

up vote 3 down vote accepted
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.

share|improve this answer
    
Thanks a lot. That was the error! After changing the CCPR1L value PWM generated :) –  user2835684 Apr 13 at 15:25
add comment

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!

share|improve this answer
    
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 at 15:30
    
@user2835684 Glad to help :) –  bitsmack Apr 13 at 15:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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