Shop OBEX P1 Docs P2 Docs Learn Events
Reading single bit out of a byte (or word or long) — Parallax Forums

Reading single bit out of a byte (or word or long)

LitefireLitefire Posts: 108
edited 2007-03-25 21:33 in Propeller 1
hey, say i have four sensors whose pin states i want to store into a single variable (sensor).

sensor := sensor1
sensor :+ sensor2 <<1
sensor :+ sensor3 <<1
sensor :+ sensor4 <<1

now, how do i read single bits out of that sensor variable? is there a way besides simple bit shifting?

~~Brian

Comments

  • cgraceycgracey Posts: 14,133
    edited 2007-03-25 02:47
    You could put those bits into the VSCL register and then be able to manipulate like this:

    'Output sensor1 bits 0..3 onto P0
    VSCL := sensor1
    outa[noparse][[/noparse]0] := VSCL[noparse][[/noparse]0]
    outa[noparse][[/noparse]0] := VSCL
    outa[noparse][[/noparse]0] := VSCL
    outa[noparse][[/noparse]0] := VSCL

    'Input P0 into sensor1[noparse][[/noparse]0..3]
    VSCL[noparse][[/noparse]0] := P0
    VSCL := P0
    VSCL := P0
    VSCL := P0
    sensor1 := VSCL

    That code doesn't do anything practical, but it shows how you can (ab)use VSCL for any bitwise purpose. You can specify ranges of bits to: VSCL[noparse][[/noparse]3..0] or the reverse VSCL[noparse][[/noparse]0..3].

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • LitefireLitefire Posts: 108
    edited 2007-03-25 04:36
    i think you might have dropped off the [noparse][[/noparse] index] part of VSCL, but i get the idea. thanks!

    i assume this would be faster than bitwise shifts... could i set VSCL as an alias to sensor? so that when sensor changes VSCL changes along with it?

    ~~Brian
  • cgraceycgracey Posts: 14,133
    edited 2007-03-25 04:40
    Woops! You're right that I forgot the index notation. You can't use this as an alias, though, only a temporary workspace.
    Litefire said...
    i think you might have dropped off the [noparse][[/noparse] index] part of VSCL, but i get the idea. thanks!

    i assume this would be faster than bitwise shifts... could i set VSCL as an alias to sensor? so that when sensor changes VSCL changes along with it?

    ~~Brian
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • LitefireLitefire Posts: 108
    edited 2007-03-25 21:33
    thanks chip.

    ~~Brian
Sign In or Register to comment.