Shop OBEX P1 Docs P2 Docs Learn Events
Creating large array — Parallax Forums

Creating large array

RusRus Posts: 3
edited 2008-07-24 21:40 in BASIC Stamp
Hello. Need help to create 256 bites long array.
I have BS2px. I get variable size too large when I try to declare array (array_name· VAR Bit (256)). I guess I am out of RAM space.
Is there any work around? I need serially shift 256 values (0 or 1), and I do not want to create several small arrays.
·
Thank you

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-24 19:05
    The Stamps have only 26 bytes of variable space. There is a separate RAM area called "scratchpad RAM" which is 128 bytes. The last few locations have special uses, but most of that is available using the GET / PUT statements to access it a byte or a word at a time. It's not an array, but it's almost usable that way.
  • ZootZoot Posts: 2,227
    edited 2008-07-24 20:01
    If indeed you are talking about 256 *bits*, you can emulate a pseudo bit array using 32 bytes in SPRAM (scratch pad ram):

    myBitArray CON 64  ' starting SPRAM address for 32 byte array, can be anywhere pretty much
    
    index VAR Byte ' general counter
    
    param VAR Byte ' for passing value to subroutine(s)
    temp VAR param ' work var for subroutine(s)
    bitVal VAR Bit
    
    ' note that at reset/powerup, all vals should be 0, this writes 1s to each bit in the pseudo array
    
    Main:
       FOR index = 0 TO 255 ' loop through all 256 bits
         param = index ' which bit to get/set?
         bitVal = 1        ' set bit to 1
         GOSUB PUT_BIT
       NEXT
    
       FOR index = 0 TO 255
         param = index
         GOSUB GET_BIT
         ' now value of BIT #index is in bitVal
        DEBUG "Bit # ", DEC3 index, " = ", BIN1 bitVal, CLREOL, CR
       NEXT
    
    END
    
    
    ' some subroutines:
    
    ' set param to bit # to read (0 to 255) before calling routine
    ' returns bit value as 0 or 1 in bitVal
    GET_BIT:
      GET param >> 3 + myBitArray, temp ' get correct byte for this BIT (every 8 bits is a byte from the "stack")
      bitVal = temp.BIT0(param & $07) ' save correct bit for this byte into bitVal and return the value
      RETURN
    
    ' set param to bit # to set before calling routine
    ' set bitVal to value to set before calling routine
    PUT_BIT:
      GET param >> 3 + myBitArray, temp ' get correct byte for this BIT (every 8 bits is a byte from the "stack")
      temp.BIT0(param & $07) = bitVal  ' set correct bit for this byte
      PUT param >> 3 + myBitArray, temp ' save it
      RETURN
     
    
    

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

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 7/24/2008 9:33:11 PM GMT
  • RusRus Posts: 3
    edited 2008-07-24 21:12
    Thanks. I still need some help. I did not see Set_Bit routin.
    Also , if I need insert data to index 35-40, what is the procedure?

    Thank you
  • ZootZoot Posts: 2,227
    edited 2008-07-24 21:40
    Sorry, that was a typo. Should be "PUT_BIT". I fixed it above.
    said...
    Also , if I need insert data to index 35-40, what is the procedure?

    Just set the index value, the desired bitval and it's done.

    Each byte holds 8 bits. So when the param (the index) is shifted right 3 times, that divides the index by 8, resulting in byte 0-31. Then the routine gets the byte at that location in SPRAM. Then the lowest 3 bits of the param (the index bit desired) are used to set/get the bit WITHIN that byte that corresponds to the desired index.

    e.g.

    param = 35  ' working with bit 35 out of 0-255 bits
    bitVal = 1     ' set that bit to 1
    GOSUB PUT_BIT
    
    



    35 >> 3 (i.e. 35/8) = 4 --> working with byte index 4 (5th byte from bytes 0-31)
    35 & $07 (i.e. 35//8) = 3 --> working with bit index 3 (4th bit of byte)

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

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
Sign In or Register to comment.