Shop OBEX P1 Docs P2 Docs Learn Events
Trying to define a large number of Word variables with SX/B, but having trouble — Parallax Forums

Trying to define a large number of Word variables with SX/B, but having trouble

Joe GrandJoe Grand Posts: 70
edited 2008-03-18 01:31 in General Discussion
Hi guys-

I've been struggling with this problem all weekend. I'm working on a project that requires a number of Word variables (let's say 16 for now). Clearly the SX28 has enough RAM to handle that, but I'm running into the good old "VARIABLE EXCEED AVAILABLE RAM" error message. I understand how to define and work with byte arrays, but all the examples I could find (on this forum, Jon's N&V articles, and The Zen of SX/B Programming, http://jcouture.net/sx/section.asp?sec=arrays) are only working with single bytes.

I've tried to do things like defining a Word with Byte(2) or defining a large byte array (e.g., ByteArray(32)) and then defining my words as:

TmpW1 VAR ByteArray(0) ' this is a Word
TmpW2 VAR ByteArray(2) ' this is a Word

But get the "UNKNOWN COMMAND 'LET"" when I actually try to treat the variable as a Word, which tells me the compiler is thinking it's still working with a Byte.

I've also tried to poke around with using _LSB and _MSB, but that doesn't work either (since that seems to define the actual variable as that name and not just TmpW1):

TmpW1_LSB VAR ByteArray(0)
TmpW1_MSB VAR ByteArray(1)

So, my question is: Is there a way to define a set of Word variables in such a way that I'll be able to use them normally throughout my code and not run into the error messages?

Thanks [noparse]:)[/noparse]

Joe

Comments

  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2008-03-17 04:47
    Hello Joe,

    The SX48 has couple bytes less general RAM than the SX28 since it has two more ports than the SX28. The SX28 treats the unused ports as a little extra RAM.

    As far as I know, word variables can only be defined and reside in general RAM. They don't seem to be supported in arrays. I usually define any word variables early and move most of the byte variables into an array. When doing so you can give it a regular name so you don't always have to use the index of the array. As an example:

    mtrStruct VAR Byte (16)
    encOld VAR mtrStruct(0) ' previous encoder bits
    encNew VAR mtrStruct(1) ' new encoder bits
    m1Dir VAR mtrStruct(2) ' Motor 1 Direction
    m1tDir VAR mtrStruct(3)

    timout1 VAR mtrStruct(5) ' LSB of timeout
    timout2 VAR mtrStruct(6) ' MSB of timeout

    timWrk VAR mtrStruct(7) ' Work variable

    You can access the variables either using the array name and index or by just using the variable name. This only works for bytes. JonnyMac has posted some ISR based Serial code to the list and that has a great example of a byte array used as an array as well as for variables.

    First, I would assign all your word variables first and move any bytes into an array. If it is still tight then take a good hard look to see if you REALLY need all the work variables you are using. If you have to have them then you'll just have to leave a word variable available as a work variable and store some of your words in byte array. When you need to work with them you can pull it out and put it in the work variable like:

    TmpW1_LSB = ByteArray(0)
    TmpW2_MSB = ByteArray(1)

    When you are done just store it back:

    ByteArray(0) = TmpW1_LSB
    ByteArray(1) = TmpW1_MSB

    As you write larger programs there are a few quirks like this but once you are aware of them it isn't too bad to code around them.

    Good luck,

    Robert
  • Joe GrandJoe Grand Posts: 70
    edited 2008-03-17 14:18
    Hi Robert-

    Thanks for the reply. I've already moved my Byte variables into an array (I only have a few). I'm doing some heavy math routines, which is why I have so many Words. I'll try to condense the number of Words I actually need, but does anyone know any other tricks I could use instead of having to assign the values to a Word and back? Maybe something with the BANK command?

    Joe

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---

    Joe Grand
    Grand Idea Studio, Inc.
    Designer of the Parallax Emic Text-to-Speech, RFID, and GPS modules
  • BeanBean Posts: 8,129
    edited 2008-03-18 00:47
    Joe,
    If you could post the code I could make some suggestions. If don't want the code to be public you can email it to me.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • Joe GrandJoe Grand Posts: 70
    edited 2008-03-18 01:31
    E-mail sent. It's still wayyyyyy too rough and too embarrassing to post publicly yet (though, eventually, the source will be completely open to the public) [noparse]:)[/noparse]

    Take care,

    Joe

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---

    Joe Grand
    Grand Idea Studio, Inc.
    Designer of the Parallax Emic Text-to-Speech, RFID, and GPS modules
Sign In or Register to comment.