Shop OBEX P1 Docs P2 Docs Learn Events
which io pin change state first ? — Parallax Forums

which io pin change state first ?

ArchiverArchiver Posts: 46,084
edited 2000-07-14 05:11 in General Discussion
I have a problem in Bs2 application,

Q1: There are 8 input to BS2, normally these 8 pin is LOW state, but my
application is to determine
which pin occur to "high" state first . Does anyone help me out ?

Baker

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2000-07-13 15:38
    How about:

    status var byte
    hpin var nib
    none:
    status = inl ' read P0-P7 (use inh for P8-P15)
    if status=0 then none ' not yet
    ' ok now, one or more pins are high, give priority to P0
    for hpin=0 to 7
    if (status & (1<<hpin))=1 then found
    next
    found:
    ' hbin now has pin # from 0 to 7 in it
    ' if more than one pin is high, the lowest one "wins"


    Something like that?

    Regards,

    Al Williams
    AWC
    * 8 channels of PWM: http://www.al-williams.com/awce/pak5.htm


    >
    Original Message
    > From: baker_bai@c... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=wca7pf7fedVAaZ7ta9uSJjlcHHlmL8mHgc0l6fUNr00MDvGHxe7gCjRQE5KJylVob-Jkz_DtOO9rvGyPKzf5]baker_bai@c...[/urlOn Behalf Of
    > baker_bai@c...
    > Sent: Thursday, July 13, 2000 9:04 AM
    > To: basicstamps@Egroups.com
    > Subject: [noparse][[/noparse]basicstamps] which io pin change state first ?
    >
    >
    > I have a problem in Bs2 application,
    >
    > Q1: There are 8 input to BS2, normally these 8 pin is LOW state,
    > but my application is to determine
    > which pin occur to "high" state first . Does anyone help me out ?
    >
    > Baker
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2000-07-13 16:04
    baker_bai@c... said:

    > I have a problem in Bs2 application,
    >
    > Q1: There are 8 input to BS2, normally these 8 pin is LOW state, but my
    application is to determine
    > which pin occur to "high" state first . Does anyone help me out ?
    >
    > Baker

    you can read all 8 pins into a byte variable in one command.. then check if
    that byte is zero, if not, then check to find which particular bit is high.
    if the byte is zero, then read it again (until something goes high)

    Jason
  • ArchiverArchiver Posts: 46,084
    edited 2000-07-13 17:20
    Baker asked:
    > Q1: There are 8 input to BS2, normally these 8 pin is LOW
    > state, but my application is to determine which pin occur
    > to "high" state first . Does anyone help me out ?

    Al suggested:
    > status var byte
    > hpin var nib
    > none:
    > status = inl ' read P0-P7 (use inh for P8-P15)
    > if status=0 then none ' not yet
    > ' ok now, one or more pins are high, give priority to P0
    > for hpin=0 to 7
    > if (status & (1<<hpin))=1 then found
    > next
    > found:
    > ' hbin now has pin # from 0 to 7 in it
    > ' if more than one pin is high, the lowest one "wins"


    Or replace the for-next loop between the
    with the single statement,
    hbin = NCD status
    The NCD function returns the number of the highest bit set in status. It is
    a value from 1 to 8, and NCD 0 = 0. So if status=%00100000 going in, then
    hbin=6 going out. The routine might be crunched down further to:

    hpin var nib
    none:
    hbin = NCD inL ' read P0-P7 (use inh for P8-P15)
    if hbin=0 then none ' not yet
    ' ok now, one or more pins are high
    found:
    ' hbin now has pin # from 1 to 8 in it
    ' if more than one pin is high, the highest one "wins"

    It is a stamper habit, looking for ways to save bytes and clock cycles!

    -- Tracy Allen
    Electronically Monitored Ecosystems
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2000-07-13 18:49
    >
    > Or replace the for-next loop between the
    with the single statement,
    > hbin = NCD status
    > The NCD function returns the number of the highest bit set in
    > status. It is
    > a value from 1 to 8, and NCD 0 = 0. So if status=%00100000 going in, then
    > hbin=6 going out. The routine might be crunched down further to:

    Good point Tracy. Of course, with NCD P7 has priority and you have no
    alternative. With the loop you can make either side have the priority. In
    fact, to be fair, you could randomly pick which end you started at so that
    one input was not consistently favored over another. Also, you can't
    determine if more than one is on if you need that information.

    Actually, my code is wrong anyway.

    (status & (1<<hpin)) will only equal 1 for P0. It should read:
    if (status & (1<<hpin))<>0 then found

    Oh well!


    Regards,

    Al Williams
    AWC
    * Floating point math for the Stamp, PIC, SX, or any microcontroller:
    http://www.al-williams.com/awce/pak1.htm
  • ArchiverArchiver Posts: 46,084
    edited 2000-07-14 04:42
    >> hbin = NCD status
    > Of course, with NCD P7 has priority and you have no
    > alternative. With the loop you can make either side have
    > the priority.

    no problem, still no loop necessary:

    hbin = NCD (status rev 8)

    lets the low bit have the highest priority.


    > In fact, to be fair, you could randomly pick which end.
    > you started at so that one input was not consistently
    > favored over another. Also, you can't determine if more
    > than one is on if you need that information.

    We'd better not get too deep into it without more detail!
    The question was simply,

    >>> my application is to determine which pin occur to "high" state first.

    One application we had fun with here in the past was a game show, where 8
    contestants were each to have a button, and a lamp on their stand. The
    Stamp was to determine which contestant pressed his or her button fastest
    in response to a question, and to show first second and third place by the
    rate of flashing of the lamps, and to allow the possibility of ties in each
    place. I think I was able to get that down to four or five lines of code,
    as a state machine with only one loop and no IFs.
    http://www.emesystems.com/BS2misc.htm#Game show

    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2000-07-14 05:11
    > no problem, still no loop necessary:
    >
    > hbin = NCD (status rev 8)
    >

    That's a good one. I had thought of NCD, but I hadn't thought of that one.

    Regards,

    Al Williams
    AWC
Sign In or Register to comment.