1
\$\begingroup\$

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'
addwf CCPR1L,1
return

END

The schematic

\$\endgroup\$
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – PeterJ Apr 20 '14 at 9:32
  • \$\begingroup\$ 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. \$\endgroup\$ – user2835684 Apr 20 '14 at 10:51
  • \$\begingroup\$ Yep it fixed and I've posted the modified part of the code as an answer. Thank you. \$\endgroup\$ – user2835684 Apr 20 '14 at 11:05
1
\$\begingroup\$

Thank you PeterJ. Clearing the WDT inside the loop fixed the issue. Following is the modified part of the code.

l 
clrwdt
btfsc PORTB,0 ;bit test f skip if clear
call inc
goto l
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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