Shop OBEX P1 Docs P2 Docs Learn Events
How would cnt work doing this — Parallax Forums

How would cnt work doing this

krazyideaskrazyideas Posts: 119
edited 2008-10-29 02:54 in Propeller 1
Hello

I want to measure time useing the counter (cnt)·

So what I was going to do was·to get·the cnt value when a pin goes high and then get the cnt value·again when it goes low and subtract the second cnt value by the frist, which will tell me how many clock tiks passed during that time.

My question is will that work?· Because if the 2 cnt values are with in the 32 bit range it would work.
But wouldn't it fail if the first value was at the end of the 32 bit counter and then the counter started at 0 again.·The second cnt value·being say 100 and the the first being 3,999,991,000, which would give me a huge wrong negative number.

So I guess what I'm trying to say is would that be an accurate way to measure time repeatedly, or would it have a possible glitch at the end of the 32 bit counter?? is there some internal program that would eliminate that??

Or what could I do to eliminate that glitch?? like writeing·code that if the counter is equal to or greater then X to just ignor that peoried of time and then start mesureing clock tiks on the next loop around the counter??

Thanks

·

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-10-23 19:45
    Check out the PING))) Object (Included in the Propeller Tool Library). This is how it calculates the time from trigger to received pulse. You·had the right idea! [noparse];)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-10-23 19:46
    What you propose will work, so long as the counter doesn't roll over more than once between readings. For example, if your first reading is $FFFF_FFFF, and your second reading is $0000_0008, then $0000_0008 - $FFFF_FFFF = $0000_0009, the correct value. Just be sure that if the high-order bit of the difference is set, that you don't interpret it as a negative number. This would correspond to an interval longer than 26.8 seconds.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-10-23 20:06
    krazyideas,
    What you might also want to do is check out this application note on the Counters...
    http://www.parallax.com/Portals/0/Downloads/appnt/prop/AN001-PropellerCountersv1.1.zip
    ...If you set the mode to %11010 or "LOGIC A" and set·the·APIN...
    ·
    CTRA := %11010 << 26 | APIN
    FRQA := 1
    ·
    ...Then just prior to your pin going HIGH all you need to do is clear the PHSA register to "reset" it.

    PHSA := 0

    At some time after the·PIN goes low, simply read the PHSA register which will contain the time difference that you are looking for regardless of a counter rollover occurrence from the CNT register.
    ·



    ·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 10/23/2008 8:13:00 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-10-23 20:15
    That could work too…I think the reason I went the way I did is that in SPIN you’re going to get some delay executing the command while the counter is running. But it evens out when you do virtually the same command at both times, so since the offset is the same, the counter delta is more likely to be accurate, I think. I haven’t really tried the other way for timing to compare. =)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • krazyideaskrazyideas Posts: 119
    edited 2008-10-29 00:55
    Hello there.

    Well this·is what I came up with so that if·the second count value (measure2) is 100 and·the frist count value (measure1) is 4 billion, my program will ignor·then negative incorrect number and pick up on the next reading.

    This would work I do belive.· If the theory of this program works I am going to convert it all over·to assembly code so that it will excute faster and be more accurate. Because I need to be as close to perfect·as I possibly can.

    Also I heard someone mention that they could mesure the overhead or time the processor takes to preform all the comands. If so I want to measure the overhead and then advance my reading to be dead·perfect.

    Any advise would be great thanks so much ·



    ·repeat

    ··· waitpeq (%1000000, %00000000000000000000000001000000, 0)········{wait for pin 6 to go high}
    ··· measure1 := cnt
    ··· waitpne (%1000000, %00000000000000000000000001000000, 0)······· {wait for pin 6 to go low}
    ··· measure2 := cnt

    ····· begin++··········· 'counts one every time Reader goes high

    ······· check := (measure2 - measure1)

    ········· if check < 0
    ········· Next

    ··········· degree := check / 10
  • pgbpsupgbpsu Posts: 460
    edited 2008-10-29 02:54
    If you know your events will occur less than 53 seconds apart (if the Prop is running at 80Mhz) than I think Phil's suggestions is the simplest. If you code this in assembly you have an unsigned subtract command. See pg 403 of the manual. You could simply:
    1. wait for pin to go high
    2. grab start cnt value
    3. wait for pin to low
    4. grab stop cnt value
    5. subtract start from stop

    If your events can be more than 53 seconds apart you'll have to get more sophisticated.
    pgb
Sign In or Register to comment.