+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2012
    Posts
    10

    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
    1,798

    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
    Join Date
    Apr 2012
    Posts
    10

    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
    1,798

    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
    1,798

    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
    211

    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 04:24.
    Thanks and Regards;
    Gadelhas

    http://www.gadelhas.cambs.net/

  7. #7
    Join Date
    May 2008
    Location
    Italy
    Posts
    792

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

    Did you declare the variable CYCLE? Did you give a value to such a variable?

    If not then PWM command will not work.

    Cheers

    Al.
    All progress began with an idea

  8. #8
    Join Date
    Apr 2012
    Posts
    10

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

    Quote Originally Posted by gadelhas View Post
    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.
    thanks, so high and low still work when the pin is a input, interesting.

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,896

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

    not interesting, it is explained in the manual...


    Hi, we are pleased to see that you are using our forum.

    We'd like you to become a member of our community. Membership is FREE, please REGISTER and view our forums without these annoying reminders!

    Once registered you may post on the forums, download from the file areas and use the WIKI without interruption.




    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Members who have read this thread : 0

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