Shop OBEX P1 Docs P2 Docs Learn Events
Pin value to binary number — Parallax Forums

Pin value to binary number

ArchiverArchiver Posts: 46,084
edited 2001-05-07 16:34 in General Discussion
You may use direct bit to bit correspondece like:
x.bit0 = in0
x.bit1 = in1
x.bit2 = in2
or by nibble:
x var nib
x= inA 'low nible of low byte
or by byte
x var byte
x= inL
or by word
x var word
x =inS

ACJacques

rgolub@l... wrote:
>
> I think this is an easy question but I can't figure it out:
>
> Reading pin states:
>
> Value1 = IN1
> Value2 = IN2
> Value3 = IN3
>
> what I want to do is to create a binary value that represents the
> values of the pins, ie. if Value1 = 1, Value2 = 0, Value3 = 1 then
> the result would be (binary) 101.
>
> How does one do that?
>
> Thanks to all....
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-05-04 02:49
    I think this is an easy question but I can't figure it out:

    Reading pin states:

    Value1 = IN1
    Value2 = IN2
    Value3 = IN3

    what I want to do is to create a binary value that represents the
    values of the pins, ie. if Value1 = 1, Value2 = 0, Value3 = 1 then
    the result would be (binary) 101.

    How does one do that?

    Thanks to all....
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-04 03:08
    I guess there is a number of ways to do this
    1 if you can use one var rather than three

    value var nib
    value = ina 'if ina = 1101
    value <<1 >>1 'this just drops the 4th bit

    now
    value.bit0 = 1
    VALUE.BIT1 = 0
    VALUE.BIT2 = 1



    rgolub@l... wrote:
    >
    > I think this is an easy question but I can't figure it out:
    >
    > Reading pin states:
    >
    > Value1 = IN1
    > Value2 = IN2
    > Value3 = IN3
    >
    > what I want to do is to create a binary value that represents the
    > values of the pins, ie. if Value1 = 1, Value2 = 0, Value3 = 1 then
    > the result would be (binary) 101.
    >
    > How does one do that?
    >
    > Thanks to all....
    >
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-04 03:14
    or in your case you can shift right 1 this will make in1 = in0 2 will be
    1 and 3 will be 2

    another way is

    value.bit0 = in1
    value.bit1 = in2
    value.bit2 = in3

    now the nib varable value = X101, x is not used so you may want to shift
    left one then back to make the fourth bit a 0.

    "L .Gaminde" wrote:
    >
    > I guess there is a number of ways to do this
    > 1 if you can use one var rather than three
    >
    > value var nib
    > value = ina 'if ina = 1101
    > value <<1 >>1 'this just drops the 4th bit
    >
    > now
    > value.bit0 = 1
    > VALUE.BIT1 = 0
    > VALUE.BIT2 = 1
    >
    >
    >
    > rgolub@l... wrote:
    > >
    > > I think this is an easy question but I can't figure it out:
    > >
    > > Reading pin states:
    > >
    > > Value1 = IN1
    > > Value2 = IN2
    > > Value3 = IN3
    > >
    > > what I want to do is to create a binary value that represents the
    > > values of the pins, ie. if Value1 = 1, Value2 = 0, Value3 = 1 then
    > > the result would be (binary) 101.
    > >
    > > How does one do that?
    > >
    > > Thanks to all....
    > >
    > >
    > >
    > > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-04 03:19
    --- In basicstamps@y..., "L .Gaminde" <lgaminde@t...> wrote:
    > or in your case you can shift right 1 this will make in1 = in0 2
    will be
    > 1 and 3 will be 2
    >
    > another way is
    >
    > value.bit0 = in1
    > value.bit1 = in2
    > value.bit2 = in3
    >
    >
    That works! Thanks.
  • ArchiverArchiver Posts: 46,084
    edited 2001-05-07 16:34
    rgolub -

    While the answers given are fine, the Stamp provides
    another approach that can make for fewer operations in
    your code, make the code more understandable, and give
    you exactly what you asked.

    Let's say that you use IN1, IN2, and IN3, and tie an
    unused IN0 to ground (so that it remains at logic 0).
    These four input bits are combined automatically as
    INA (see p. 47 of the BASIC Stamp Programming Manual
    2.0c, or page 216 of BASIC Stamp Manual 1.9).

    INA = 8*IN3 + 4*IN2 + 2*IN1 + IN0

    So that instead of:

    IF IN1 = 1 AND IN3 = 1 THEN SubRoutineX

    you could write:

    IF INA = 10 THEN SubroutineX

    Not only does the command call for less logic
    operations, but it makes clear anything you missed.
    There are no odd values for INA in this case, because
    IN0 is tied to ground. But what if IN2 also is high?
    The branch to SubroutineX would not take place,
    because the value of INA would be 14. In a controls
    situation, that's a good thing as the event of all
    three inputs being high might otherwise be unforeseen.


    I have a project currently where I am checking to see
    which of four bits is set to determine which
    operational mode to go to. Consider the code:

    ChooseMode:
    IF IN0 = 1 THEN Mode1Subroutine
    IF IN1 = 1 THEN Mode2Subroutine
    IF IN2 = 1 THEN Mode3Subroutine
    IF IN3 = 1 THEN Mode4Subroutine
    GOTO ChooseMode:

    What if I switch from Mode 2 to Mode 3? If I have a
    non-shorting switch (break-then-make), fine; I see
    logical 0 on all four bits and loop back until the
    switch energizes the third position and I see a
    logical 1 on IN2 so that I select the third mode. But
    if I have a shorting switch (make-then-break), then
    momentarily both IN1 (for Mode 2) and IN2 (for Mode 3)
    are on, but since I get to the IF statement for Mode 2
    first, I go back to Mode 2, when I wanted to go to 3!
    With the named nibble, I can avoid this problem:

    ChooseMode2:
    IF INA = 1 THEN Mode1Subroutine
    IF INA = 2 THEN Mode2Subroutine
    IF INA = 4 THEN Mode3Subroutine
    IF INA = 8 THEN Mode4Subroutine
    GOTO ChooseMode2:

    Now, not only do I not have to worry about shorting
    versus non-shorting switches, but if the switch
    becomes defective and stays on two modes, I get a
    value that is the aggrgate of two bits such as 3, 6,
    or 12, but I safely avoid misrouting control.

    Bob Pence



    --- rgolub@l... wrote:
    > I think this is an easy question but I can't figure
    > it out:
    >
    > Reading pin states:
    >
    > Value1 = IN1
    > Value2 = IN2
    > Value3 = IN3
    >
    > what I want to do is to create a binary value that
    > represents the
    > values of the pins, ie. if Value1 = 1, Value2 = 0,
    > Value3 = 1 then
    > the result would be (binary) 101.
    >
    > How does one do that?
    >
    > Thanks to all....
    >
    >
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >


    __________________________________________________
    Do You Yahoo!?
    Yahoo! Auctions - buy the things you want at great prices
    http://auctions.yahoo.com/
Sign In or Register to comment.