Why is this code not working(pwm on a 16f628a)


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1

    Default Why is this code not working(pwm on a 16f628a)

    So I am testing out pbp and am trying to get an led to fade on, stay on, then fade off. Here is my current code

    Code:
    STARTMAIN:
    if button=0 then
        For Duty=0 to 255 Step 1
            PWM LED4,Duty,Cycle
        Next
        led4=1
        pause 1000
        For Duty=255 to 0 Step -1
            PWM LED4,Duty,Cycle
        Next
        LED4=0
    
    endif
    LED4=0
    GOTO STARTMAIN
    What happens is that when I press the button, the led fades on, then turns off for a second, then fades off. Why is this happening?

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Why is this code not working(pwm on a 16f628a)

    How is the LED connected to the PIC?

    Robert

    EDIT: And what did you use as CONFIG and TRIS settings?

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Why is this code not working(pwm on a 16f628a)

    Quote Originally Posted by Demon View Post
    How is the LED connected to the PIC?

    Robert

    EDIT: And what did you use as CONFIG and TRIS settings?
    positive end is connected to the pin, negative end is grounded

    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_OFF & _MCLRE_OFF & _BODEN_OFF & _LVP_OFF
    
    Define OSC 4
    
    
    LED4 VAR PortA.1
    button VAR PortA.2
    CMCON = 7 
    VRCON = 0 
    PortA=0
    PortB = 000100 
    T1CON = 000011
    TRISA = 000100
    TRISB = 000000


    and this code works though, which is really confusing me
    Code:
    new:
    if button=0 then
        led4=1
        pause 500
        led4=0
    endif
    
    
    goto new

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Why is this code not working(pwm on a 16f628a)

    Totally wild guess, confirm your programmer is setting the low voltage programming fuse properly:
    http://www.picbasic.co.uk/forum/show...ht=#post113491

    (still looking at datasheet)

    Robert

    EDIT: Darn, I remember reading another thread that was missing one more config to disable analog, can't find it now.

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: Why is this code not working(pwm on a 16f628a)

    It must be something with the PWM, either config or syntax.

    What if you use variables like this?

    http://www.picbasic.co.uk/forum/show...ht=16f628a+pwm

    Robert

  6. #6
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: Why is this code not working(pwm on a 16f628a)

    Try this code insted

    Code:
    STARTMAIN:
    if button=0 then   
     For Duty=0 to 255       
      PWM LED4,Duty,Cycle   
     Next   
     high led4    
     pause 1000 
       
     For Duty=255 to 0 Step -1       
      PWM LED4,Duty,Cycle    
     Next
    endif
    
    low led4
    GOTO STARTMAIN
    If you read the PBP Manual you will see that the PWM command after execution turn the pin input.
    Last edited by gadelhas; - 3rd May 2012 at 05:24.
    Thanks and Regards;
    Gadelhas

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts