Shop OBEX P1 Docs P2 Docs Learn Events
PWM monitoring with spin? — Parallax Forums

PWM monitoring with spin?

Brian CarpenterBrian Carpenter Posts: 728
edited 2007-04-08 16:34 in Propeller 1
So i have a· PWM signal coming from a device that would normally drive a servo.· instead, i want to be able to se the PWM with the Propeller and based on its width, have the propeller accomplish different tasks.· Where is the starting point in spin to do this?· what commands would i use.· I realize that i could use the BS2 functions object but i dont need all the other things associated with it.· Any help would be great.· Thanks

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-08 05:51
    You could use the counters to time the pulse. Use counter mode %01000 which will add FRQ to PHS every system clock cycle where the input pin is high. See the application note about the counters (AN-001) for an explanation about the counters.

    In Spin, wait for the pulse to go high, then wait for the pulse to go low. At that point, read the counter (PHSx) and you'll get the width in 12.5ns steps (with an 80MHz clock). Zero the counter and you'll be ready for the next pulse.
  • T ChapT Chap Posts: 4,223
    edited 2007-04-08 05:51
    Here's an idea I posted for something similar recently. There will be some wasted clock pulses due to Spin, so depending on the frequency, you may be able to bias this some and get it close.

    
    PUB INIT
       dira[noparse][[/noparse] 0] := 1    'set your Pulse in pin to pin P0
       dira[noparse][[/noparse] 1] := 0     'set Apin to output P1, may not be used in this example
       CTRA := %00100 << 26 + 1   set Apin to  P1
       frqa := 0      '  no accumulation yet
       PHSA := 0    '  not required, but illustrates that PHSA will start counting at 0 
    
    PUB TestPulseWidth
      Repeat
        Frqa := 0      ' set to no accumulation
        PHSA := 0    '  reset PHSA to 0
        Waitpeq (%1, %1, 0)  'wait for P1 to equal 1, this is your pulse to test
        FRQA := 1    'starts the CTRA counting from 0
        Waitpeq (%0, %1, 0)  'wait for P0 to equal 0, your pulse is finished, set FRQA stop counting next
        Frqa := 0      '  stop the accumulation
        TestResult    'or do a Return PHSA, and let the caller do the TestResult using the returned value
    
    
    

    Post Edited (TChapman) : 4/8/2007 7:10:05 AM GMT
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2007-04-08 06:18
    Tchapman,
     CTRA := %00100 << 26 + 1   set Apin to  P1
    
    

    What does this do differently from the example that Mike gave of using %01000


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • T ChapT Chap Posts: 4,223
    edited 2007-04-08 06:22
    I was sugegsting that the waitpeq is where the counter starts counting. When the pin sees a 1, Freqa changes to 1, so the PHSA starts accumulating in NCO mode, but the mode really makes no difference in this case, it's just counting 1 clock at a time. When the pin goes back low, the Freqa goes back to 0, so no further accumlation in the PHSA, then the PSHA is read to see what it accumulated.

    I am not clear on Mikes idea yet, I am still looking at it to see what that mode is and how it works, I think it is a POS in detector.

    Post Edited (TChapman) : 4/8/2007 6:26:35 AM GMT
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2007-04-08 06:28
    Got to hit the hay for tonight but will look at this again in the morning. Thanks Guys

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • T ChapT Chap Posts: 4,223
    edited 2007-04-08 06:57
    an001 said...
    The remaining modes accumulate when the mode’s logic equation evaluates as true.

    Page 6, Table 6 says that mode %11010 APIN = 1 accumulates PHSA when pin is a 1.

    Page 16 says "Mode %01000 (POS detector) is identical to mode %11010 (LOGIC A)"

    I think Mike is suggesting that the Apin be used as the input to the counter , when High the counter accumulates at the rate of FREQA.

    PUB INIT
       dira[noparse][[/noparse] 0] := 0  
       CTRA := %01000 << 26 + 0  
       frqa := 1     
    
    PUB TestPulseWidth
      Repeat
        PHSA := 0    '  reset PHSA to 0
        Waitpeq (%1, %1, 0)   'wait for high, counter starts also on a 1
        Waitpeq (%0, %1, 0)   'wait for a low, the counter stops on a 0
        TestResult             'count := PHSA 
    
    

    Post Edited (TChapman) : 4/8/2007 9:04:19 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-08 16:34
    You're right. I was suggesting that APIN should be the pulse input, that FRQx be a 1, and that PHSx be initialized to zero sometime between pulses (when APIN is low). At that point, the Spin code waits for the next pulse to finish (by waiting for a high, then waiting for a low). PHSx at that point has the pulse width in units of the system clock and you can reset PHSx to zero again for the next pulse. You have some time to process the count (until just before the next pulse). TChapman's code is right on. From a code style basis, I tend to use Waitpne(0,<pin>,0) to test for a high. If the <pin> expression is complicated, it doesn't have to be repeated twice in the same statement.

    Thanks for pointing out that modes %01000 and %11010 are the same.
Sign In or Register to comment.