# Practical issue with PIC PWM

I have written the following code to generate PWM and to change the duty cycle when a button is pressed. In simulations it works fine. However when actually implemented the circuit, the PWM starts when the button is pressed. But after an about 2 secs it will stop. PWM is not generated continuously. Is there any reasons why this is happening? (eg: PIC is defect, error in assembly code)

*I'm using internal clock of the PIC

 PROCESSOR PIC16F628A
#INCLUDE <P16F628A.INC>
#INCLUDE <BANKSEL.INC>   ;a macro is used to easily change the banks

org 0x00

;set the PWM period
Bank1
movlw d'249'    ; PR2 is 249 when Fosc=4MHz, TMR2 prescale=4
movwf PR2

;set the PWM duty cycle- initially set to zero
Bank0
movlw d'0'  ; set bits 9 - 2
movwf CCPR1L
bcf CCP1CON,CCP1X  ; set bit 5
bcf CCP1CON,CCP1Y  ; set bit 4

;Make the CCP1 pin an output
Bank1
bcf TRISB,3

bsf TRISB,0;make RB0 as input

;Set the TMR2 prescale value
Bank0
movlw b'00000101'   ; TMR2 = on, prescale = 1:4
movwf T2CON

; enable pwm mode
bsf CCP1CON,3
bsf CCP1CON,2

l
btfsc PORTB,0 ;bit test f skip if clear
call inc
goto l

inc
movlw d'50'
return

END


• I don't suppose while programming the PIC you have the watchdog timer enabled? If you're not sure how to check it you could put a clrwdt in your loop to see if it makes a difference. – PeterJ Apr 20 '14 at 9:32
• Yes I've enabled the watchdog timer in fuses. I've modified the code as you mentioned. Why clearing WDT make it work? Now it runs continuously. Thank you. But still I've to keep touching the ground to make it run. – user2835684 Apr 20 '14 at 10:51
• Yep it fixed and I've posted the modified part of the code as an answer. Thank you. – user2835684 Apr 20 '14 at 11:05

l