Shop OBEX P1 Docs P2 Docs Learn Events
How to read 4 bits NOT in convenient INA/INB/INC locations — Parallax Forums

How to read 4 bits NOT in convenient INA/INB/INC locations

xanatosxanatos Posts: 1,120
edited 2011-11-12 17:22 in BASIC Stamp
I have a program that reads a 4 bit nibble from an auxillary chip and during prototyping, I was able to conveniently place it so that all 4 bits lined up with INB on my BS2. Unfortunately the real production unit I am working with must (for reasons too long to explain here) have those bits appear of pins 11, 12, 13 and 14.

I find myself unsure of how to read those bits as a single entity now. My code was a simple "IF INB = 12 THEN...". Now I have IN11, IN12, IN13 and IN14. How can I read them as one "thing"?

Thanks,

Dave

UPDATE: This works - but is this the most simple way of doing it?:

keyIn = (IN14<<3)+(IN13<<2)+(IN12<<1)+IN11

It gets the bits in the right place and works in the program, just wondering if there's a better way, like INS(11..14) or something.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-12 16:55
    You can read in all 16 bits at a time using INS, then mask off and shift the bits you want like:

    myBits = (INS >> 11) & $F

    This gets you bits 14, 13, 12, and 11 as a 4-bit number in myBits. You don't have to assign the value to a temporary variable ... you can use that expression in an IF statement (in parentheses).


    Note: The mask was wrong ... 5 bits when it should have been 4.
  • xanatosxanatos Posts: 1,120
    edited 2011-11-12 17:22
    Thanks Mike,

    I knew if you were on tonight you'd have a more elegant solution than my four-part bitwise shift! Thanks very much!!!

    Dave
Sign In or Register to comment.