PDA

View Full Version : 2 bit access



Nightrider7731
05-31-2005, 10:04 AM
Is there·any symbol that can be used to reference 2 bits of a byte?· I'm controlling 4 pairs of bits with the requirement that only one bit in each pair can be on (1).· I can label each bit and programmatically affect each pair one bit at a time, but I'd love to be able to reference and set them to %01 and %10 in a single call.· All I can find are symbols for 4 (Nib) and·8 (low/highbyte)·bit values.

Thanks!

Chris Savage
05-31-2005, 10:18 AM
Hello,

·· There are 2 ways you could go about this.· The first would be to use a NIB variable and mask the bits you aren't using.· The other way would be to reference the individual bits.· For example:



bitVal = index.BIT0

OR



index.BIT1 = IN1
index.BIT2 = IN2

Something like that.· But basically there is BIT access, not just NIB and BYTE (And WORD).

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)


Post Edited (Chris Savage (Parallax)) : 5/31/2005 3:25:46 AM GMT

Nightrider7731
05-31-2005, 12:19 PM
Currently I've aliased each bit in·the byte...

TARGETCONTROL···· VAR Byte····· 'control pointer for all fire/popup code
Player1TargetUp···· VAR TARGETCONTROL.BIT0
Player1TargetDown·· VAR TARGETCONTROL.BIT1
Player2TargetUp···· VAR TARGETCONTROL.BIT2
Player2TargetDown·· VAR TARGETCONTROL.BIT3
Player3TargetUp···· VAR TARGETCONTROL.BIT4
Player3TargetDown·· VAR TARGETCONTROL.BIT5
Player4TargetUp···· VAR TARGETCONTROL.BIT6
Player4TargetDown·· VAR TARGETCONTROL.BIT7

... and I control each 2 bit value pair bit by bit.
Player1TargetUp = 1
Player1TargetDown = 0

···· and

Player1TargetUp = 0
Player1TargetDown = 1

There is a separate controller for each motion on each target, so simply turning a single bit value·on and off doesn't work in this case.· The byte is being passed to '595 to do the work.· Actually several 595s are chained together, but that doesn't matter.· I was looking for a cheap (code wise) way to alias 2 bit blocks like this...

TARGETCONTROL···· VAR Byte····· 'control pointer for all fire/popup code
Player1Target···· VAR TARGETCONTROL.2BIT0··········· ' 2BIT0 being a fictional symbol of course :)
Player2Target···· VAR TARGETCONTROL.2BIT1
Player3Target···· VAR TARGETCONTROL.2BIT2
Player4Target···· VAR TARGETCONTROL.2BIT3
TargetUp···· = %10
TargetDown = %01

... and then manipulate TARGETCONTROL by ...

Player1Target = TargetUp
Player1Target = TargetDown
Player1Target = TargetDown
Player1Target = TargetUp

... and then shiftin TARGETCONTROL.· I guess I'll just stick to the Bit manipulation.

KenM
05-31-2005, 01:10 PM
If I understand what you are trying to do, Chris's suggestion of masking bits·will allow·you· to process each player as follows.

Or perhaps you could explain fundamentally what you want to do?




' {$STAMP BS2}
' {$PBASIC 2.5}

TARGETCONTROL VAR Byte 'define byte TARGETCONTROL
TARGETCONTROLm VAR Byte 'create a copy of TARGETCONTROL
Playerup1 VAR Bit
Playerdwn1 VAR Bit
Playerup2 VAR Bit
Playerdwn2 VAR Bit

'once you have the value of target control execute code below to act on
'the status of Player1, Player2, Player3 etc.

TARGETCONTROL = TARGETCONTROL & %00000011 'mask bits 0 & 1 (player1 up & down)
SELECT TARGETCONTROL
CASE %01
'do your code based on Player1up = 1
'and Player1dwn = 0
CASE %10
'do your code based on Player1up = 0
'and Player1dwn = 1
ENDselect
TARGETCONTROL = TARGETCONTROLm 'reload TARGETCONTROL with original value

TARGETCONTROL = TARGETCONTROL & %00001100 'mask bits 2 & 3 (player2 up & down)
SELECT TARGETCONTROL
CASE %01
'do your code based on Player2up = 1
'and Player2dwn = 0
CASE %10
'do your code based on Player2up = 0
'and Player2dwn = 1
ENDselect

Post Edited (KenM) : 5/31/2005 6:13:22 AM GMT

Don Buczynski
05-31-2005, 09:27 PM
Two bit reference·symbols;·(how about·TWIT),·would be a useful addition·to a future version of PBasic.·I've needed·that functionality to generate four color states·using·two I/O bits and a red/green LED; red, yellow, green and off.··The multiple bit command method is·workable, but·not the best use of the·Basic Stamp·code space.···

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don Buczynski

http://www.buczynski.com

Chris Savage
05-31-2005, 10:59 PM
Nightrider7731 said...(Trimmed)
I was looking for a cheap (code wise) way to alias 2 bit blocks like this...
TARGETCONTROL···· VAR Byte····· 'control pointer for all fire/popup code
Player1Target···· VAR TARGETCONTROL.2BIT0··········· ' 2BIT0 being a fictional symbol of course :)
Player2Target···· VAR TARGETCONTROL.2BIT1
Player3Target···· VAR TARGETCONTROL.2BIT2
Player4Target···· VAR TARGETCONTROL.2BIT3
TargetUp···· = %10
TargetDown = %01
... and then manipulate TARGETCONTROL by ...
Player1Target = TargetUp
Player1Target = TargetDown
Player1Target = TargetDown
Player1Target = TargetUp
... and then shiftin TARGETCONTROL.· I guess I'll just stick to the Bit manipulation.
Hello,
··
·· The immediate problem I see with what you're wanting to do, is you're trying to assign a 2-bit value (TargetUp) to a 1-bit variable (Player1Target).



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Tracy Allen
05-31-2005, 11:33 PM
Use math to map a nib to a byte, each bit in the nib becomes two bits 01 or 10 in the byte...
ps = %1001 ' 4 bits, one bit for each playertarget, set bits as desired for target up, down.
' then mathematically create the following
stateOfPlay = (1 << ps.bit0) + (4 << ps.bit1) + (16 << ps.bit2) + (64 << ps.bit3)
' that creates the byte, %10010110 to go with nib %1001
' to send to the controller

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

Nightrider7731
05-31-2005, 11:51 PM
Chris Savage (Parallax) said...

Nightrider7731 said...(Trimmed)
I was looking for a cheap (code wise) way to alias 2 bit blocks like this...
TARGETCONTROL···· VAR Byte····· 'control pointer for all fire/popup code
Player1Target···· VAR TARGETCONTROL.2BIT0··········· ' 2BIT0 being a fictional symbol of course :)
Player2Target···· VAR TARGETCONTROL.2BIT1
Player3Target···· VAR TARGETCONTROL.2BIT2
Player4Target···· VAR TARGETCONTROL.2BIT3
TargetUp···· = %10
TargetDown = %01
... and then manipulate TARGETCONTROL by ...
Player1Target = TargetUp
Player1Target = TargetDown
Player1Target = TargetDown
Player1Target = TargetUp
... and then shiftin TARGETCONTROL.· I guess I'll just stick to the Bit manipulation.
Hello,
··
·· The immediate problem I see with what you're wanting to do, is you're trying to assign a 2-bit value (TargetUp) to a 1-bit variable (Player1Target).



I was creating a ficticious 2 bit symbol (see comment on Player1target) that would solve my problem of assigning a 2 bit value to it.

Jon Williams
06-01-2005, 12:05 AM
Remember that Nibs and Words are not native microcontroller data types, so the BASIC Stamp has a lot of underlying code to deal with them. Adding TWITS would be no different, and what you might find is that you consume more code space than expected because of the underlying code required to deal with a non-native two-bit variable type.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Paul Baker
06-01-2005, 01:09 AM
Take a look at this (http://forums.parallax.com/showthread.php?p=537751) post of someone asking the same question, there are fragments of SX/B code you may find helpful that should translate to the Stamp easily.

nick bernard
06-01-2005, 04:38 AM
silly jon twits are for kids...

i've always been a fan of shifting, perhaps this method suits you

'unverified
playerdata VAR Word

Main:
GOSUB target_routine
FOR idx = 0 TO 6
playerdata >> 2
GOSUB target_routine
next

'... several lines later
target_routine:
SELECT playerdata // 2
CASE 0
'do that thing
CASE 1
'do that thing
CASE 2
'do that thing
CASE 3
'do that thing
ENDSELECT

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Byte walks into a bar and orders a pint. Bartender asks him "What's wrong?"
Byte says "Parity error." Bartender nods and says "Yeah, I thought you looked a bit off."