Shop OBEX P1 Docs P2 Docs Learn Events
detecting pwm — Parallax Forums

detecting pwm

ThricThric Posts: 109
edited 2012-08-26 10:59 in Propeller 1
Is there a way to detect whether a pwm signal is present on a pin? Currently I can use timers to find the duty of a PWM signal but if there is no signal present the program will hang indefinitely for an event that will never come. Is there a way to do this without implementing a watchdog?

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-08-24 20:21
    Sure. Set up a counter to count when there are transitions on the pin (low-to-high is mode %01010). Check its phase register periodically for activity.

    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-08-24 20:31
    You don't have to use a "waitpxx" command to check for pin states. Some quadrature encoder programs just keep checking pin states and compares the new pin state with the previous state to see if it has changed.
  • ThricThric Posts: 109
    edited 2012-08-25 20:47
    So I did some much needed reading in the manual as well as around the forums and i came up with this code
    PUB Pulse_detect(Pin) 
    
      DIRA[pin]~
      ctra := 0
      ctra[30..26] := %01010        ' set up counter for positive edge detection
      ctra[5..0] := Pin
      frqa := 1
      waitcnt(10_000+cnt)
      if phsa > 0
        ctra :=0
        return 1
      else
        ctra :=0
        return 0  
                     
    

    Problem is it doesn't seem to be working as I would expect it too (of course). See any blatantly obvious mistakes?
  • kuronekokuroneko Posts: 3,623
    edited 2012-08-25 20:56
    Thric wrote: »
    Problem is it doesn't seem to be working as I would expect it too (of course). See any blatantly obvious mistakes?
    Can you describe the problem you're (still) having? The only thing I can see which would have an impact is that you don't reset phsa. Try something like
    PUB Pulse_detect(Pin) 
    
      DIRA[pin]~
      ctra[30..26] := %01010        ' set up counter for positive edge detection
      ctra[5..0] := Pin
      frqa := 1
      waitcnt(10_000+cnt)
      ctra := 0
      if phsa[COLOR="#FF0000"]~[/COLOR]
        return 1
    
    [COLOR="silver"]' implied return 0 otherwise[/COLOR]
    
    Or reset phsa before waiting. Your choice.

    Following my own advice here, since frqa stays not equal zero you should clear phsa once the counter is upset:
    PUB Pulse_detect(Pin) 
    
      DIRA[pin]~
      ctra[30..26] := %01010        ' set up counter for positive edge detection
      ctra[5..0] := Pin
      frqa := 1
      phsa := 0
      waitcnt(10_000+cnt)
      ctra := 0
      if phsa
        return 1
    
    [COLOR="silver"]' implied return 0 otherwise[/COLOR]
    
  • ThricThric Posts: 109
    edited 2012-08-25 21:08
    Alright I found my problem... simple of course. I just needed to wait longer, I just chose an arbitrary value but I should calculate it out to find the smallest number. Thanks for the help

    Edit: maybe i jumped the gun a bit for no problems, works one minute and not the next. I'll check latter.
  • ThricThric Posts: 109
    edited 2012-08-25 21:35
    Alright so I figured out the problem (finally). The counter is working but the only problem is that the pins I'm testing are all floating, but when pulled to ground the program performs as expected. There must be some software method of pulling a pin to ground right?
  • kuronekokuroneko Posts: 3,623
    edited 2012-08-25 21:44
    Thric wrote: »
    There must be some software method of pulling a pin to ground right?
    You could define it as an output but that would work against whatever external h/w you're trying to monitor. What's wrong with ordinary pull-ups?
  • Mark_TMark_T Posts: 1,981
    edited 2012-08-26 04:57
    kuroneko wrote: »
    You could define it as an output but that would work against whatever external h/w you're trying to monitor. What's wrong with ordinary pull-ups?

    Adds to the BOM...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-08-26 10:59
    Mark_T wrote:
    Adds to the BOM...

    Here ya go: 20,000 pull-up resistors for $37.05. That's less than two-tenths of a cent apiece.

    -Phil
Sign In or Register to comment.