Transfer data from array to variable
RDavies
Posts: 3
I have an 8 bit array. I want to get the data from the array into a byte variable.
For example:
muxary(0)=1 LSB
muxary(1)=1
muxary(2)=0
muxary(3)=1
muxary(4)=0
muxary(5)=0
muxary(6)=1
muxary(7)=1 MSB
I would like to take this changing data and move it into a byte variable:
muxvar = 11001011
I'm not quite sure on how to do this without making the code long and overly complex.
Thank you for any help,
Ricky
For example:
muxary(0)=1 LSB
muxary(1)=1
muxary(2)=0
muxary(3)=1
muxary(4)=0
muxary(5)=0
muxary(6)=1
muxary(7)=1 MSB
I would like to take this changing data and move it into a byte variable:
muxvar = 11001011
I'm not quite sure on how to do this without making the code long and overly complex.
Thank you for any help,
Ricky
Comments
my_variable VAR Byte
my_array VAR Bit(8)
idx VAR Nib
main:
my_array(0)=1
my_array(1)=1
my_array(2)=0
my_array(3)=1
my_array(4)=0
my_array(5)=0
my_array(6)=1
my_array(7)=1
FOR idx =7 TO 0
my_variable=my_variable<<1
my_variable=my_variable+my_array(idx)
NEXT
DEBUG DEC my_variable,CR
DEBUG BIN my_variable,C
Jeff T.
muxvar VAR byte
muxvar.BIT0 = 1
.
.
.
muxvar.BIT7 = 1
Then it's automatic... though I guess you have problems addressing the array in other places if you need a variable index.
Possibly muxvar.LOWBIT(x) would let you address the bits with an index variable but you would have to see if this is legal. Take a look at pp. 90-91 of the BS Syntax manual.
Post Edited (Professor Chaos) : 12/12/2006 7:51:26 AM GMT
The same goes for bit arrays (muxvar.BIT0(idx) or muxvar.LOWBIT(idx) *will* work). You can also do similar tricks with Words, Nibs, etc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
·
·· One possible solution that is not very elegant, but very effective and doesn’t require any more variables to implement is listed below.· If you byte array appears on an even BYTE boundary such as the example below does, you can reference it by the register name of the BYTE that each BIT resides in.· In this case B0.· So, you have a BIT array of 8 and reading the underlying register gives you the BYTE value without any conversion.· I hope this helps.· If I think of something better I will post it.· Take care.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
·
·· It’s possible…Your code would work, however it seems he already has an array of 8 bits.· How would you load each bit into the array?· The ability to change that would depend on how the data gets there in the first place.· If we knew that they may be even easier ways to do this.· For example, what if the data is being shifted in?· In that case you could just use a byte variable and call it good.· · Current suggestions are based on the limited information available.· Take care.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
How about aliasing muxarybit to muxary.bit(0)? Wouldn't muxarybit(0) through muxarybit(7) then, through implied array addressing, allow him to manipulate in either space at will?
I'm nowhere near a Stamp machine at the moment, or I'd check it out myself....
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
The beauty of implied addressing should allow you to eliminate the muxary1 through muxary7 definitions and run through your loop on muxary0(idx). Implied addressing relies on the size of the variable and its address. Since muxary0 is bit-sized and starts at the top of muxary, you should be able to count through the bits of muxary using an index off of muxary0.
I think...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...
Also, implied arrays are defined by the size of the FIRST variable used in addressing the array, so if muxary is a Byte, then muxary(1) would be the second Byte following muxary, muxary(2) would be the third byte.
If muxary were a bit, then yes, muxary(1) would be the next Bit, BUT, and this is a big BUT, you need to "reserve" that space somehow, or you will be "overwriting" some other variable that might be declared in that same space.
A side note -- directly named variables take up less program space than indexed variables. Why, I am not sure, but I've verified this more than once. e.g., regardless of the size of the implied array(s):
The question at this point is: RDavis -- is any of this helpful?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
Post Edited (Zoot) : 12/12/2006 5:33:28 PM GMT
·
·· The code you posted above would work if the OP is setting the array elements in that fashion, i.e. muxary.BIT0(1) = 0· I just don’t know how this array is being set, but if the implied array could be used then you could easily refer to the main byte.· Take care.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
The bs2 I'm using will be connected to a 8bit multiplexer, each of the eight inputs will be connected to sensors that will output a "1" when there is a detection. The bs2 will loop through each address (%000-%111) and store the bit present at the input pin in an 8bit array named muxary. I tried to access all 8bits in the array as a byte, my result was just the first bit in the array; maybe my syntax was wrong. So, instead I wanted to move the data in the array into a byte variable so that I could use that to branch my program to the appropriate subroutine.
Thanks,
Ricky
Just to clarify, muxary does allocate the space for the byte, then muxary0 defines a bit by aliasing to muxary.BIT0, so muxary0(0) through muxary0(7) should work...the compiler will convert these references to addresses...
I should have checked this out last night, but no time...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...