Shop OBEX P1 Docs P2 Docs Learn Events
Simple square wave counter? — Parallax Forums

Simple square wave counter?

ArchiverArchiver Posts: 46,084
edited 2003-04-18 18:22 in General Discussion
Is there a simple way to count incoming pulses from a square wave?
The COUNT function does not do what I want, as it counts edges over a
fixed time period and gives the result. What I am looking to do is
to count pulses from an incoming square wave and do a compare on the
value (ie, count until 40 pulses go by, then turn on an LED.) I am
afraid if I use a pin high to increment a count that I will possibly
get more than one count per pulse. I know that the BUTTON function
will require a low before a new high is counted, but is this the only
way to do this?

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-04-18 17:40
    This could work I guess, it all depends on how fast the pulses are, if your
    doing anything else or just looking for pulses.

    Look:
    if pin1 = 1 then goto counting
    goto Look
    Counting:
    yourcount = yourcount + 1
    if yourcount = 40 then Newcode
    goto Look
    Newcode:
    whatever here


    Original Message
    From: "Kevin R. Walsh" <krwalsh@y...>
    To: <basicstamps@yahoogroups.com>
    Sent: April 18, 2003 9:04 AM
    Subject: [noparse][[/noparse]basicstamps] Simple square wave counter?


    > Is there a simple way to count incoming pulses from a square wave?
    > The COUNT function does not do what I want, as it counts edges over a
    > fixed time period and gives the result. What I am looking to do is
    > to count pulses from an incoming square wave and do a compare on the
    > value (ie, count until 40 pulses go by, then turn on an LED.) I am
    > afraid if I use a pin high to increment a count that I will possibly
    > get more than one count per pulse. I know that the BUTTON function
    > will require a low before a new high is counted, but is this the only
    > way to do this?
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-18 18:06
    You can monitor the input pin directly. What you want to do is look for a
    CHANGE on the input pin. Assume that you are monitoring pin0, and that you
    want to count 3 pulses.

    i var byte
    changedbit var bit

    changedbit = NOT pin0 ' I'm NOT sure if you can use NOT here. If not,
    ' use changedbit = 1-pin0

    i = 0

    loop:
    if pin0 <> changebit then loop
    ' pin0 has changed state
    toggle changedbit
    i = i + 1
    if i > 6 then turnonled
    goto loop

    turnonled:
    ' Turn on the led
    i = 0
    goto loop


    Assume that when the program starts, pin0 is in the 0 state. "changedbit"
    is tracking the state of pin0; we set it to be the opposite of pin0
    initially (in this case, to 1).

    The "if" will be true, and the program will loop until pin0 is a 1 (rising
    edge of the square wave). At this point we toggle "changedbit", so it will
    now be 0. We increment the counter and go back to monitoring pin0. The
    program will now loop until pin0 is 0 (different from "changedbit") - the
    falling edge of the square wave. we increment the counter once more, and
    toggle "changedbit".

    We now have monitored a complete pulse, and the counter "i" is at 2. We
    have counted the number of CHANGES in pin0. Since we want to know when 3
    pulses have occurred, and since we get 2 counts per pulse (rising and
    falling edges), we need to terminate the count at 6, not 3.

    If you want to count complete square waves (i.e. you don't want the count
    to terminate until the rising edge of the 4th square wave (three complete
    on and off cycles), the count would need to be 7, not 6, so that the next
    rising edge is counted.

    Parallax has an appnote 20 "An Accurate Time base" that uses this technique

    Have fun!

    Larry


    At 04:04 PM 4/18/2003 +0000, you wrote:
    >Is there a simple way to count incoming pulses from a square wave?
    >The COUNT function does not do what I want, as it counts edges over a
    >fixed time period and gives the result. What I am looking to do is
    >to count pulses from an incoming square wave and do a compare on the
    >value (ie, count until 40 pulses go by, then turn on an LED.) I am
    >afraid if I use a pin high to increment a count that I will possibly
    >get more than one count per pulse. I know that the BUTTON function
    >will require a low before a new high is counted, but is this the only
    >way to do this?
    >
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject and
    >Body of the message will be ignored.
    >
    >
    >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/

    Larry Bradley
    Orleans (Ottawa), Ontario, CANADA

    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-18 18:14
    >Is there a simple way to count incoming pulses from a square wave?
    >The COUNT function does not do what I want, as it counts edges over a
    >fixed time period and gives the result. What I am looking to do is
    >to count pulses from an incoming square wave and do a compare on the
    >value (ie, count until 40 pulses go by, then turn on an LED.) I am
    >afraid if I use a pin high to increment a count that I will possibly
    >get more than one count per pulse. I know that the BUTTON function
    >will require a low before a new high is counted, but is this the only
    >way to do this?

    How fast? It is possible if it is relatively slow (less than
    somewhat in the 100s of hertz, depending on which Stamp you are
    using). Here is a way using state variables to detect the rising
    edge on p0:

    '{$PBASIC 2.5}
    x0 var bit ' old state of pin
    x1 var bit ' new state of pin
    xx var bit ' change in state of pin
    LED pin 1
    counter var byte
    do
    x1 = in0
    xx = x1 ^ x0 & x1
    counter = counter+xx max 40
    x0=x1 ' update old state variable
    if counter=40 then high LED ' turn it on
    next


    -- best regards
    Thomas Tracy Allen PhD
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-18 18:20
    > '{$PBASIC 2.5}
    > x0 var bit ' old state of pin
    > x1 var bit ' new state of pin
    > xx var bit ' change in state of pin
    > LED pin 1
    > counter var byte
    > do
    > x1 = in0
    > xx = x1 ^ x0 & x1
    > counter = counter+xx max 40
    > x0=x1 ' update old state variable
    > if counter=40 then high LED ' turn it on
    > next

    loop ' not next!!! mixing up pBasics
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-18 18:22
    Yup, the new PBASIC commands are really slick -- used them recently and was
    really impressed.


    Original Message


    > > '{$PBASIC 2.5}

    > > next
    >
    > loop ' not next!!! mixing up pBasics
Sign In or Register to comment.