Shop OBEX P1 Docs P2 Docs Learn Events
Transfer data from array to variable — Parallax Forums

Transfer data from array to variable

RDaviesRDavies Posts: 3
edited 2006-12-13 16:40 in BASIC Stamp
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

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-12-12 03:27
    Hi Ricky, is this along the lines

    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.
  • Professor ChaosProfessor Chaos Posts: 36
    edited 2006-12-12 07:46
    Would this work?

    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
  • ZootZoot Posts: 2,227
    edited 2006-12-12 15:10
    You might want to try using "implied arrays" to access elements of an array by index AND by named variable. In Pbasic, if you define a variable, subsequent memory allocations of the same size can be accessed as array elements even if they are not defined as such...

    idx VAR Nib
    muxvar VAR Byte   'same as muxvar(0)
    muxvar1 VAR Byte  'same as muxvar(1)
    muxvarZ VAR  Byte  'same as muxvar(2) -- the name doesn't matter, just size and position
    muxvarA VAR Byte  'same as muxvar(3)
    muxvar4 VAR  Byte  'same as muxvar(4)
    
    FOR idx = 0 TO 4
       muxvar(idx) = %0001000 'seed with a number
    NEXT
    
    'muxvar1.BIT4 now = 1, etc
    
    



    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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-12-12 16:06
    Ricky,
    ·
    ·· 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.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    muxary    VAR   Bit(8)
    muxary(0) = 1
    muxary(1) = 0
    muxary(2) = 0
    muxary(3) = 1
    muxary(4) = 0
    muxary(5) = 0
    muxary(6) = 0
    muxary(7) = 0
    DEBUG DEC B0
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ZootZoot Posts: 2,227
    edited 2006-12-12 16:29
    Chris -- couldn't he do the same thing with an implied array and not have to use B0 or W0, etc? e.g.

    muxary    VAR   Byte
    muxary.BIT0(0) = 1   'or muxary.LOWBIT(0) = 1, whichever you prefer
    muxary.BIT0(1) = 0
    muxary.BIT0(2) = 0
    muxary.BIT0(3) = 1
    muxary.BIT0(4) = 0
    muxary.BIT0(5) = 0
    muxary.BIT0(6) = 0
    muxary.BIT0(7) = 0
    DEBUG DEC muxary
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-12-12 16:39
    Zoot,
    ·
    ·· 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
  • Tom WalkerTom Walker Posts: 509
    edited 2006-12-12 17:09
    Chris,
    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...
  • ZootZoot Posts: 2,227
    edited 2006-12-12 17:11
    Well, I'm a huge fan of implied arrays, obviously. If it were me, I would set up each bit as a single var and then use the implied array for array functions, and each bit var if I need to set a single bit, e.g.

    idx VAR Nib
    muxary    VAR   Byte
    muxary0  VAR muxary.BIT0
    muxary1  VAR muxary.BIT1
    muxary2   VAR muxary.BIT2
    muxary3   VAR muxary.BIT3
    muxary4  VAR muxary.BIT4
    muxary5  VAR muxary.BIT5
    muxary6  VAR muxary.BIT6
    muxary7 VAR  muxary.BIT7
    
    FOR idx = 0 TO 7
    muxary.BIT0(idx) = 1 'setting bit by index -- straight array
    NEXT
    
    muxary4 = 0  'setting bit manually by varname
    
    SHIFTIN IO, Clk, MSB, [noparse][[/noparse]muxary\8]  'setting bits with shiftin
    
    muxary = %0001000 'setting whole array in one shot
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • Tom WalkerTom Walker Posts: 509
    edited 2006-12-12 17:17
    Zoot,
    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...
  • ZootZoot Posts: 2,227
    edited 2006-12-12 17:29
    I only did that because it offers RDavies the most ways to access a given bit (either by name or by index) since Chris wasn't sure how/when/where he might be setting data.

    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):
    muxary1 = 1 'this takes up fewer bytes in program space than...
    muxary(1) = 1 'which will take up a few more bytes of program space
    
    



    The question at this point is: RDavis -- is any of this helpful? smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    Post Edited (Zoot) : 12/12/2006 5:33:28 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-12-12 18:31
    Zoot,
    ·
    ·· 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
  • RDaviesRDavies Posts: 3
    edited 2006-12-12 21:46
    Thank you all very much for your suggestions!

    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
  • Tom WalkerTom Walker Posts: 509
    edited 2006-12-13 14:08
    Zoot,
    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...
  • ZootZoot Posts: 2,227
    edited 2006-12-13 16:40
    Tom -- yer right. Sorry I must had too many frozen muxarys at the Boe-bot caf
Sign In or Register to comment.