Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp 2P with dealing with Binary Code — Parallax Forums

Basic Stamp 2P with dealing with Binary Code

DukeDuke Posts: 21
edited 2006-04-19 06:05 in BASIC Stamp
Hi,

I'm working with a simple circuit of reading 4 rapid switches·on port 4 5 6 7 (Port INB)
The·pattern·of switch·read·can be·16 possible binary patterns

The program is like this

'
PortData·VAR Nib······················ 'Declare var type

MAIN:

· OUTB =~INB
··PortData = OUTB·····················'Set value "PortRead" equal to Pin 4-7

· DEBUG IBIN·PortData ,CR·········· 'Display Binary Value of Pin 4-7

GOTO MAIN
'
Result on DEBUG Screen shows

%0001
%0001
%0011
%0011
%0110
%0110
%1110
%1000
%1000
%0000
.. As it continues

I want to extract only 2 Higher bit or 2 Lower bits Like..

Low··························· High······················X = ignore
%00XX··-->00············· %XX01· -->01
%00XX··-->00··············%XX01· -->01
%00XX··-->00··············%XX11· -->11
%00XX··-->00············· %XX11· -->11
%01XX··-->01··············%XX10· -->10
%01XX··-->01············· %XX10· -->10
%11XX··-->11············· %XX10· -->10
%10XX··-->10············· %XX00· -->00
%10XX··-->10············· %XX00· -->00
%00XX··-->00············· %XX00· -->00

Are there·some math function or some short programs that can do this extraction.
Aslo it would be useful if you can advise me how·to read only 2 bits data at one time
I will use it instead of "INB" Command.

Thank you.

Duke..

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-04-19 05:59
    To convert the high·(left-most)·bits of a nibble to a two-bit value:

    · twoBits = theNib >> 2

    To simply keep the lower two bits:

    · twobits = theNib & %0011

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • DukeDuke Posts: 21
    edited 2006-04-19 06:05
    Thank you very much

    That is exactly what I want!, My program is working now.

    Duke..
Sign In or Register to comment.