Shop OBEX P1 Docs P2 Docs Learn Events
PWM don`t stop. — Parallax Forums

PWM don`t stop.

dardar Posts: 15
edited 2012-12-21 02:16 in Propeller 1
Hi again!:

I'm trying to work with a pwm signal, but i have a problem, the pwm start and works right, but when i go out the loop and i want to stop the pwm signal, it don't stop.
This is the code i'm using, i don't know where can i have a mistake:

PUB CONTROL_LUMINARIA


LEER_ADC   
debug.str(string("voltaje_panel:"))
debug.str(ftos.FloatToString(voltios_panel))
debug.NewLine
  
repeat while voltios_panel<15.0
    pwm.start(mos_luminaria)                 'Inicializamos el pwm en el mosfet
    pwm.SetPeriod(100000)
    debug.NewLine
    pwm.setduty(potencia1)
    LEER_ADC
                                                             
pwm.stop

Thanks

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-20 11:42
    You didn't mention which PWM object you're using.

    To make sure the PWM stops you could set the PWM to zero before calling stop.
    pwm.setduty(0)
    pwm.stop
    

    Still, It would be good to know which object you're using.

    I had this same problem yesterday, I'm not sure yet if the problem was my code of if for some reason the counters stay active even when a cog is stopped.

    FYI, I was using Kye's "PWM2C_HBDEngine.spin". I'm inclined to suspect the problem was with my code, not Kye's object.
  • T ChapT Chap Posts: 4,223
    edited 2012-12-20 11:43
    Is there a pwm.stop method in the object? You may nee to call the Stop method on exit.
  • dardar Posts: 15
    edited 2012-12-20 11:51
    I'm sorry, the objetct to pwm is:

    pwmAsm.spin

    and the instruction to stop is that.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-20 16:50
    I think the problem may be in the object pwmAsm. The copy I found stores the cog ID differently than normal. I'm not sure if the way it's done in the pwmAsm object will work correctly when the "stop" method is called.

    I'm not sure, but if you changed the following Start method from:
    PUB Start( Pin) : okay
    'start pwm on Pin @ 80 kHz
      longfill(@sDuty, 0, 4)       
      sDuty := 50                   ' default duty
      sPinOut := |< Pin   
      sCtraVal :=  %00100 << 26 + Pin
      sPeriod := 2000
      
      okay := cogon := (cog := cognew(@entry,@sDuty)) > 0    
       
    

    To:
    PUB Start( Pin) : okay
    'start pwm on Pin @ 80 kHz
      longfill(@sDuty, 0, 4)       
      sDuty := 50                   ' default duty
      sPinOut := |< Pin   
      sCtraVal :=  %00100 << 26 + Pin
      sPeriod := 2000
      
      okay := cogon := cog := cognew(@entry,@sDuty) + 1    
       
    

    It would allow the stop method to work correctly.

    I don't understand what the author was doing with " > 0" part in the original code. I'm also not sure if the code above is from the same object you're using.
  • shimniokshimniok Posts: 177
    edited 2012-12-20 18:32
    Just a guess but probably a mistake, thinking that cognew() returns 0 if failure. Like the unix/c-ish kind of idiom. Also sort of resembles pg191 of the prop manual
    Pass := (Cog := cognew(@Update, Pos) + 1) > 0
    

    Sorry for my confusion but doesn't cog get assigned the cog # plus one? So, you'd need to make sure the cogstop call is done correctly (see same page191 example)
    if Cog
    cogstop(Cog~ - 1)
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-20 19:52
    shimniok wrote: »
    Just a guess but probably a mistake, thinking that cognew() returns 0 if failure. Like the unix/c-ish kind of idiom. Also sort of resembles pg191 of the prop manual
    Pass := (Cog := cognew(@Update, Pos) + 1) > 0
    

    Sorry for my confusion but doesn't cog get assigned the cog # plus one? So, you'd need to make sure the cogstop call is done correctly (see same page191 example)
    if Cog
    cogstop(Cog~ - 1)
    

    You're right, I missed the lack of a "-1" in the cogstop call. If the change I suggested is used, then the change shimniok suggested would also be needed.
  • dardar Posts: 15
    edited 2012-12-21 01:10
    Duane Degn wrote: »
    You're right, I missed the lack of a "-1" in the cogstop call. If the change I suggested is used, then the change shimniok suggested would also be needed.

    I tryed to change that, but i couldn't do it works.
    I download other object:
    "PWM_32_V4"

    It works fine.

    Thanks for yours suggentions!




  • MagIO2MagIO2 Posts: 2,243
    edited 2012-12-21 02:16
    I think the problem was that you started the pwm object in a repeat loop, which is not what you should do!
    The bug in pwm object is, that it does not check whether it's already running and stop the other instace before. This way the driver looses information of all but the last started COG and can only stop this last one letting all others run and still generate the PWM.
Sign In or Register to comment.