Shop OBEX P1 Docs P2 Docs Learn Events
Counting for a specified time? — Parallax Forums

Counting for a specified time?

electromanjelectromanj Posts: 270
edited 2010-08-28 17:40 in Propeller 1
Hello all! I was wondering if it is possible to count highs and lows on a certain I/O pin for a specific time in the same method. I would like to count low to high transitions for 10 seconds. Thanks in advance.

pub counter



repeat until (10 seconds pass)
waitpeq(%000000000,|<8,0)
waitpeq(%100000000,|<8,0)
counter1:=(counter1+1)
lcd.tx(158)
lcd.dec(counter1)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-28 14:57
    Well, sort of ...

    You would use one of the cog counters to count low to high transitions and read the PHS register for the counter after a wait of 10 seconds. It will take a few microseconds to read PHS after the 10 second WAITCNT finishes, so there's the possibility of low to high transitions happening (and being counted) during that time, but I suspect that's not a problem.

    You'd use counter mode %01010 to increment on low to high transitions something like this:
    pub count10seconds(pinNumber) | timeDelay
    timeDelay := 10 * clkfreq
    ctra := 0   ' stop any existing counter use
    phsa := 0
    frqa := 0   ' initialize the counter
    ctra := (%01010 << 26) + pinNumber
    waitcnt(timeDelay + cnt)   ' wait for 10 seconds
    return phsa   ' return the count
    
  • potatoheadpotatohead Posts: 10,261
    edited 2010-08-28 14:57
    Well, you can obtain units of time with this expression:

    (CLKFREQ / 10) = clocks per 1/10 second. And 100, 1000, etc... do other units of time.

    So, if you want, say 5.5 seconds, you could do something like:

    time := (CLKFREQ / 10) * 5.5 'total number of clocks in delay

    Once you've got that done, then just add that to the counter, when you want your delay to start:

    delay := cnt + time

    Then do something like:

    repeat until cnt > delay

    put your high / low checks inside this loop, and it will tally them up, while the system counter also counts toward your delay value, exiting the repeat loop after the right amount of time has passed.


    Look at P 219 of the Propeller manual. Time delays and such are detailed there, and are probably what you need.
  • electromanjelectromanj Posts: 270
    edited 2010-08-28 15:12
    Thanks for the quick replys! @Mike where in the method would I send the result to the lcd?
  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-28 15:16
    I wrote the method to take a pin number and return the count after 10 seconds. You'd display the results on an LCD or whatever you want when it returns like:
    pub main | temp
    temp := count10seconds(9) ' use pin 9
    lcd.tx(158)
    lcd.dec(temp)
    
  • electromanjelectromanj Posts: 270
    edited 2010-08-28 15:33
    Hi Mike, I attached the object I have so far now. As you can see I am totally lost.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-28 16:39
    I just wrote "main" as an example of how you might use "count10seconds". I didn't mean for you to just take it unaltered and without understanding what it did. Take some time to understand what I did, then integrate it into what you already have.

    You might want to download the application note on the counters (AN001).
  • electromanjelectromanj Posts: 270
    edited 2010-08-28 17:36
    Thank you Mike and Potatohead for your time and answers.

    Potatohead, that way of doing it is very interesting, and I played around with it a little.

    Mike, thanks for the link I didn't realize those app notes were there. Printing it off now.

    As you can both tell I don't know much about spin, and believe me I tryed many different attempts after browsing the prop manual, the Prop edu lab, and the official guide. Sometimes a guy needs a little push in a new direction. Things are getting a little clearer. Thanks again, much reading to do now.
  • electromanjelectromanj Posts: 270
    edited 2010-08-28 17:40
    Thank you both for your help! I have played with both examples, and as you can tell I'm still not speaking the same "language" as you. I am printing off the app note you suggested Mike. Back to study hall for me!
Sign In or Register to comment.