Strangw results when using PWM command


Closed Thread
Results 1 to 21 of 21
  1. #1
    malc-c's Avatar
    malc-c Guest

    Default Strangw results when using PWM command

    I've written a simple program to pulse a LED on GPIO.0 of a 12F675, which worked fine. However I didn't save the file, and so I started from scratch again this morning.. but I'm experiencing some strange behaviour.

    This is the main body of the code:

    Code:
    led var GPIO.0
    
    i var byte
    
    up:
    For i = 1 to 254                                                  
    Pwm GPIO.0,i,1
    if i = 254 then goto down
    next i
    
    down:
    for i = 254 to 1 step-1
    pwm GPIO.0,i,1
    if i = 1 then goto main
    next i
    As you can see its a simple FOR/NEXT loop, that, should i reach a set value, goto either up or down section.

    However when programmed, the LED ramps up in brightness but then fails to ramp down. I've tried remming out the up section and all that happens is that the LED flashes fast (as if i is not reducing by the step-1 command)

    Code:
    ;up:
    For i = 255 to 1 step-1                                                 
    Pwm GPIO.0,i,1
    ;if i = 254 then goto down
    next i
    
    ;down:
    ;for i = 254 to 1 step-1
    ;pwm GPIO.0,i,1
    ;if i = 1 then goto main
    ;next i
    Its as if the PWM command or the step-1 statement is not being observed. And what's bugging me is I know it can be done as it worked before (the chip is now in cornwall so I can't read back the HEX !)

    Comments anyone please !

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    This is really strange.. its as though the "IF" in the "IF i = 245" is being ignored.

    I've just tried the following and it simply ramps up the LED to full brightness, over and over again, with out any pause ! (ie its the same as not having the pause statement there)

    Code:
    Main:
    For i = 1 to 254                                                 
    Pwm GPIO.0,i,1
    if i = 254 then pause 500
    if i = 254 then down
    next i

  3. #3
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Now this is getting silly !

    Code:
    Main:
    for i = 1 to 254                                             
    Pwm GPIO.0,i,1
    if i=253 then goto down
    next i
    
    down:
    high gpio.0 
    pause 1000
    low gpio.0
    goto main
    The above test even fails... it simple runs the main loop and doesn't branch off to the down routine !


    EDIT:

    As does the following
    Code:
    Main:
    for i = 1 to 254                                             
    Pwm GPIO.0,i,1
    if i=253 then t=1
    if t=1 then goto down
    next i
    This is doing my head in
    Last edited by malc-c; - 9th July 2006 at 14:08.

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink just simplify ...

    Hi, Malc

    Try this ... and do not forget I/Os and clock defining ...

    DEFINE OSCCAL_1K 1

    ADCON0 = 0
    ANSEL = 0

    ' Define YOUR I/Os Here !!! TRISIO = % ?????1

    led var GPIO.0

    i var byte

    up:
    For i = 1 to 254
    Pwm GPIO.0,i,1
    next i

    for i = 254 to 1 step-1
    pwm GPIO.0,i,1
    next i

    GOTO up

    END

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics
    DEFINE OSCCAL_1K 1

    ADCON0 = 0
    ANSEL = 0

    ' Define YOUR I/Os Here !!! TRISIO = % ?????1
    The define OSCCAL have nothing usefull to do there
    Same for ADCON0 as it's the POR default.. but i agree could be safer

    Add CMCON=7 as it's a 12F675 and it have internal comparator multiplexed with GP.0

    Quote Originally Posted by Malc-C
    Main:
    for i = 1 to 254
    Pwm GPIO.0,i,1
    if i=253 then goto down
    next i
    Bad programming method IMHO, i always prefer to validate the Loop condition before getout and if usefull, set a flag wich say i interrupt the loop... something like
    Code:
    Main:
    for i = 1 to 254                                             
        Pwm GPIO.0,i,1
        if i=253 then 
            i=254
            GetOut=1
            endif
       next i
    
    if Getout then DoSomething
    In your case, the main problem is most the analog stuff
    Steve

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

  6. #6
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Alain, thank for the reply, but that still has the same effect, ie the LED ramps up in brightness, but doesn't ramp down.

    Steve,
    I never said my coding was tidy

    However isn't

    Code:
    Main:
    for i = 1 to 254                                             
        Pwm GPIO.0,i,1
        if i=253 then 
            i=254
            GetOut=1
            endif
       next i
    
    if Getout then DoSomething
    The same thing as

    Code:
    Main:
    for i = 1 to 254                                             
    Pwm GPIO.0,i,1
    if i=253 then t=1
    if t=1 then goto down
    next i
    in a round about way ? where the condition is replaced with t in my case ?

    The thing that is also bugging me if that whilst the loop to increase the brightness works, the one to decrease it doesn't ???

    Let me go away and try Steve's suggestion and see if I get any further.

    Thanks as always for your inputs

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    I tried that and it still just ramps up and doesn't seem to want to jump to the down label

    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    TRISIO.0 = 0   'Set GPIO.0 to output.
    ANSEL.0 = 0    'Set GPIO.0 to digital		
    GPIO.0 = 0
    CMCON = 7 ' PortA Digital inputs
    VRCON = 0        ' Voltage reference disabled
    OPTION_REG.7 =    0
    
    
    led var GPIO.0
    i var byte
    getout var bit
    
    main:
    for i = 1 to 254                                             
        Pwm GPIO.0,i,1
        if i=253 then 
            i=254
            GetOut=1
            endif
       next i
    
    if Getout=1 then goto down
    
    down:
    for i = 254 to 1 step -1                                            
        Pwm GPIO.0,i,1
        if i=2 then 
            i=1
            GetOut=0
            endif
       next i
    
    if Getout=0 then goto main

  8. #8
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    I've also tried the following, which seems to flash the LED rather than ramp down in brighness

    Code:
    for i = 254 to 1 step-1                                             
        Pwm GPIO.0,i,1
        if i=2 then 
            i=1
            GetOut=1
            endif
       next i
    
    if Getout=1 then goto down
    I can't for the life of me work out what's wrong...

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink There's ONLY ONE Dr' No's lamp !!!

    Hi, Malc

    I had just forgotten it was for your famous Dr Who's light ...

    so, it could not work properly with such a soft ...

    There was a thread you've already started about that ... you didn't find anything usable in it ???

    http://www.picbasic.co.uk/forum/show...1754#post21754

    I'll try to make a search ...

    Alain

    Seems the HPWM module of the 16F628a you saw was doing the job ... but may be the "slow PWM" thread from Darrel could help here !!! or use a 12F683 ...
    Last edited by Acetronics2; - 9th July 2006 at 17:13.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Alain,

    Yes this is to do with the "Dr Who" thread in a way. I started a new one as the matters raised in that thread (ie the fact you need to mod the inc files) resolved it.

    What I can't understand was that I had used a 12F675 and the for next loops with the pwm command to successfully produce a pulsating LED for my mates project. I sent this off to him but he found that he needed to drive a bulb as the LED didn't give him the brightness he needed. I informed him that he could just used a FET and how to use a zenenr diode etc to produce 5v from a 12v battery, however he has never done any electronics, and may of fried the PIC. I then loaded up the PBP program but for some reason it stops running the down loop of the code.. anyway.. I've sent off the PCB so hopefully he hasn't fried his original PIC and he can simply drop that in and get filming

    Thanks guys

  11. #11
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    with the existing stuff, try this one
    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    ANSEL.0  = 0 ' Set GPIO.0 to digital		
    CMCON    = 7 ' PortA Digital inputs
    VRCON    = 0 ' Voltage reference disabled
    TRISIO.0 = 0 ' Set GPIO.0 to output.
    GPIO.0   = 0 ' 
    
    LED    var GPIO.0
    I      var byte
    GetOut var bit
    
    main:
        for i = 1 to 254                                             
            Pwm GPIO.0,i,1
            if i=253 then 
                i=254
                GetOut=1
                endif
           next i
    
        if Getout=1 then down
        goto main
    
    down:
        getout=0
        for i = 254 to 1 step -1                                            
            Pwm GPIO.0,i,1
            if i=2 then 
                i=1
                GetOut=1
                endif
           next i
        
        if Getout=1 then main
        goto down
    and to make something usefull with the GetOut flag, use the following. Tested and work at treat. OK i'd modified the hardware assignement to fit to one of my already build board here
    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
    
    ANSEL  = 0      ' Set GPIO.0 to digital		
    CMCON  = 7      ' PortA Digital inputs
    VRCON  = 0      ' Voltage reference disabled
    TRISIO = $0F    ' gpio<3:0> : Input
                    ' gpio<5:4> : Output
    
    GPIO   = 0      ' clear all outputs
    
    LED    var GPIO.5
    PB     var GPIO.0
    I      var byte
    GetOut var bit
    
    Pause 50        ' oscillator settle time...
    OPTION_REG.7 =0 ' enable internal pull-up
    WPU = 1         ' enable pull-up on GPIO.0 only
    
    main:
        for i = 1 to 254                                             
            Pwm led,i,1
            if pb=0 then 
                i=254
                GetOut=1
                endif
           next i
        
        if Getout=1 then 
           while pb=0 : wend 
           pause 50
           GETOUT=0
           goto down
           endif
        goto main
    
    down:
        for i = 254 to 1 step -1                                            
            Pwm led,i,1
            if pb=0 then 
                i=1
                GetOut=1
                endif
           next i
        
        if Getout=1 then 
           while pb=0 : wend 
           pause 50    
           GETOUT=0
           GOTO main
           ENDIF
        goto down
    HTH
    Steve

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

  12. #12
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    Thanks once again for all you help. I had given up and wrapped up the board to post it to my mate, however I took it out of the jiffy bag and gave it one last try...

    I copied and pasted the code in your last post (the first program) and tried that. It compiled fine and loaded into the PIC, however the code does exactly the same as all other attempts. ie it ramps (for want of a better word) up the brightness to full, and then goes off, then repeats the same. It still doesn't ramp down from full brightness to low like it should - I've tried different PICs so it rules out that as being the cause.

    I think I'll call it a day on this.. thanks again for your help

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    That's really strange... it's working here using the first code of my previous message.

    Ramp-up, ramp-down in an endless loop... mmm.

    I suspect a hardware problem but damn it's not a really complicated hardware, i removed all the usual capacitor around, nothing on all other i/o, simple LED+300 ohm resistor... work fine. if i raise down the psu voltage... it's working from 2.5V. so uneless your Vdd rail is below or really noisy causing a RESET... i don't see why it shouldn't work.
    Last edited by mister_e; - 9th July 2006 at 18:47.
    Steve

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

  14. #14
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    Rest assured your code works fine.. I've just breadboarded the PIC and it works, yet in the PCB ot only wants to ramp up ????? - strange !!

    I would of thought that if there was something wrong with the PCB I made (like a short) then the thing would not work full stop ! The board uses a 5.1 zenner to provide the supply to the PIC, and the output simply from GPIO 0 drives the gate of the FET and the LED - why it ramps up and not down I don't know ?

    Thanks mate.. don't waste any more of your time... I've attached the schematic just in case someone might see whats the issue ?
    Attached Images Attached Images  

  15. #15
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    assuming 12Vin and R4 is really 10K: (12-5)/10K = 0.7mA

    No problem when only the PIC run... but when the LED is attach to.. the voltage will really reduce ... 'till the PIC reset.

    your problem is there, you don't provide enough current, Zener would need at least few mA just for her +Led+Pic... Repace R4 to 200Ohm

    @12Vin : (12-5)/200=35mA
    @9Vin : (9-5)/200 = 20mA

    @12 Vin : 35mA*35mA*200Ohm = 1/4 Watt

    Or better if you replace it for a voltage regulator like 78L05 or any else
    Steve

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

  16. #16
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve

    Thanks mate for your further help,

    The reason I opted for using a zenner, was the last time I used a 7805 to run a few LEDs the thing glowed trying to disapate the heat !

    I'll see if changing the resistor does the trick

    Malc

  17. #17
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    7805 is 1Amp rated
    78L05 ~100mA
    Steve

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

  18. #18
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Steve, went the hole hog and dropped in a 68R and it works a treat !!

    Thanks for your help

    Cheers

  19. #19
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    You're welcome... just make sure that your Zener and resistor are rated to handle it ...
    Resistor 68ohm... (12-5)/68=103mA, 103mA*103mA * 68 = 0.72W => 1 watt
    Zener 5 Volt : 5*103mA = .515 Watt => could be 1/2 watt... i calculate the worst case, VMax,No Load
    Last edited by mister_e; - 9th July 2006 at 19:48.
    Steve

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

  20. #20
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink Good old bulbs ...

    Hi, Malc

    I just tried this ... good for bulb lamp- like ... an rather simple.

    DEFINE OSCCAL_1K 1

    ADCON0 = 0
    ANSEL = 0
    CMCON = 7

    TRISIO = %00001000

    led var GPIO.0

    i var byte
    y var byte
    Speed var word ' just if speed > 255 ...

    Speed = 100

    up:

    Pause (Speed*2)

    For i = 0 to 127

    y = SIN i

    Led = 1
    Pauseus (Speed*y)
    Led = 0
    Pauseus (Speed*( 127-y ))

    next i

    GOTO up
    END

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  21. #21
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default better visual results ...

    DEFINE OSCCAL_1K 1

    ADCON0 = 0
    ANSEL = 0
    CMCON = 7
    VRCON = 0


    TRISIO = %00001000

    led var GPIO.0

    i var byte
    y var word

    up:

    Pause 200

    For i = 0 to 127

    y = SIN i

    Led = 1
    Pauseus (y*y)
    Led = 0
    Pauseus ( 16129 -(y*y) )

    next i

    GOTO up
    END
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. Active low input?
    By CosMecc in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 8th August 2010, 20:31
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. turning gpio.0 high after pwm command
    By sirvo in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st February 2007, 18:22
  4. Can I do this???
    By noobie in forum mel PIC BASIC
    Replies: 2
    Last Post: - 10th June 2006, 18:57
  5. Searches
    By bearpawz in forum Forum Requests
    Replies: 11
    Last Post: - 7th November 2005, 18:47

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