Shop OBEX P1 Docs P2 Docs Learn Events
freeing up memory? — Parallax Forums

freeing up memory?

PaulPaul Posts: 263
edited 2005-01-25 18:46 in BASIC Stamp
Greetings,
Anyway to reuse variable space? I have a DS1822 that needs 8 bytes to initialize but then the 8 bytes are never used after that. I would like to find a way to recover these bytes. I did a search on 'memory' and ' variables' but nothing came close. Can I use an implicit array and just fake it instead of the explicit array "romData VAR Byte(8)"?
Thanks, Paul.

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2004-12-16 20:52
    You could put those 8 bytes into EEPROM with the 'DATA' statement.
    Then 'READ' them and use them to initialize with.
    Then ignore them the rest of the time.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-16 20:55
    Or you could just send constants if they will always be the same.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • PaulPaul Posts: 263
    edited 2004-12-16 21:04
    hmmm.. the eight bytes are coming 'out' of the DS1822 temperature sensor and need to be read out with OW.

    OWIN OWpin,%0010,[noparse][[/noparse]STR romData\8] ' read serial number & CRC

    I think it's a Device Code ($22) followed by a checksum. After the Device code is checked to make sure the sensor is plugged in, the check sum is ignored.

    Thanks
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-16 22:25
    Do you have more than one 1-Wire device connected? If not, you can ignore reading the serial number bytes, and access the DS1822 with the SkipROM ($CC) command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • TristanTristan Posts: 3
    edited 2004-12-17 01:50
    Hi Paul,

    If you are reading more than one DS1822 there are a few things to try. I find that usually 3 bytes of serial number are 0s - so you can use constants for those and the family code is always $22 so again you can use a constant. If you wish to free up more variables, you can load the serial numbers as DATA and use READ to retrieve. You can then reuse the same variables over and over while reading in different serial numbers.

    Tristan
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-12-17 14:15
    No one has yet provided this answer:

    YES, you can map variables more than one way -- but perhaps your options are limited.

    RomData VAR BYTE(8)

    MyByte1 VAR RomData

    This works, but I think then MyByte1 is an alias for an 8-byte array. Jon, any help here?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-17 14:46
    The easiest way to reuse the elements of an array is to define the array as individual bytes.· Instead of:

    romData·····VAR··· Byte(8)

    you can do this:

    romData·····VAR··· Byte
    romData1··· VAR··· Byte
    romData2··· VAR··· Byte
    romData3··· VAR··· Byte
    romData4··· VAR··· Byte
    romData5··· VAR··· Byte
    romData6··· VAR··· Byte
    romData7··· VAR··· Byte

    You can still treat it like an array, because PBASIC treats the entire Stamp RAM as an array.· If you do this:

    romData(2) = 17

    then the variable in the list called romData2 will be changed; this happens because it is offset by two bytes from the base variable romData.· Note that when defining arrays this way, they must be defined in order, and in a contiguous block,·as I have done above.

    Now that we have a bunch of individual bytes, they are easy to alias.· Like this:

    tempC······ VAR···· romData7

    We do the byte-by-byte array definition because this is not allowed:

    tempC······ VAR·····romData(7)··· ' Error

    What this does is give the compiler a second name (tempC) for the RAM location that is also defined for romData7.· Again, you now have two names for the same byte in RAM.· An assignment to either (name) will affect the location.· You must keep this in mind during the course of your program.· Generally, aliasing is best used for transient values that don't need to be maintained for more than the course of a given routine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 12/17/2004 2:49:43 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-12-17 15:17
    Excellent Jon! That's exactly what I had in mind. And thanks for pointing out that once you do:

    romData VAR BYTE
    romData1 VAR BYTE

    Then romData(1) accesses the 'romData1' variable. Neat.

    This still has the limitation that you can only re-use Byte array variables as bytes -- but that may not be a problem, if you have at least 8 byte variables you want to use.

    So:
    · romData VAR BYTE
    · MyByte· VAR BYTE········' AKA romData(1)
    · MyOtherByte VAR BYTE· 'aka romData(2)
    · MyTempByte VAR BYTE· ' aka romData(3)

    Very nice.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-17 15:55
    You can use the "array" as anything smaller:

    flags··· VAR·· romData6
    tempHi···VAR· ·flags.BIT1

    'tempHi' is actually BIT1 of romData6.

    You can also do this:

    idx1··· ·VAR·· romData7.LOWNIB
    idx2··· ·VAR·· romData7.HIGHNIB

    And yes, you can have the romData "array" without defining all of the elements manually (as I did), but I've found this often leads to confusing code listings and programming errors.· Be careful.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 12/17/2004 3:59:59 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-12-17 17:13
    Jon,

    ·· Just wanted to point out that, even as an experienced MCU programmer, I still learn things by reading all your replies.· I guess I hadn't realized that the memory could be accessed that way.· I'm sure at one time I read it, but since I never used it, I guess I forgot it.· Very good information.· This is why I read ALL the messages in ALL the forums (Except Javelin & Translation).· You never know what things you might pick up.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-17 20:41
    One of the best tricks with this "secret" is pushing all of a program's variables into the Scrachpad RAM (does not work on stock BS2) before RUNning another program.· This is the ONLY time that I can justify the use of internal variable names:

    Push_All_Vars:
      PUT 0, B0
      FOR B0 = 1 TO 25
        PUT BO, BO(B0)
      NEXT
      RETURN
     
    Pop_All_Vars:
      FOR B0 = 1 TO 25
        GET B0, B0(B0)
      NEXT
      GET 0, B0
      RETURN
    


    It takes a second to understand what's happening, but it does work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • kelvin jameskelvin james Posts: 531
    edited 2004-12-21 07:05
    Jon, i am wondering about the code to use the scratchpad as variable space. Doesn't that just reload the variables into the fixed ram of the next program slot? Thus not really freeing up any of the variable space? I am a little confused on this. I was originally toying with the idea of how to take advantage of the 128 bytes of scratch, that i am currently not using in my program, to be able to add some extra variables. And was trying to find a way that i wouldn't have to add an extra 200 lines of code to do it, like put/get, put/ get and so on. I haven't tried the code yet, and thought i would ask first, as i don't want to waste a ton of time for nothing.
    I have tried hashing this over in my mind, but there doesn't seem to be a lot of info on the scratchpad about how it works. Thankyou . kelvin
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-21 13:54
    Usually when one jumps from slot to slot the variable definitions are maintained and the other slot acts like more program space.· But there will be times when you want the other slot to have an independent set of variable definitions.· By "pushing" your current variables onto the SPRAM stack, you can do this.· When you jump to the next slot you can completely redefine the variable RAM for that slot (it can be preserved too, if needed, when going back to the other spot -- it just needs an offset added for its location in SPRAM).

    And let me suggest that time spent experimenting is not wasted time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-21 17:21
    Scratchpad is very useful, and I am sure Jon will agree that using it as a stack to move between banks is only one of its uses.

    I do a lot of interbank programming, and only very rarely have to Stack data in that manner. Where might you want to use the stack facility to have completely different variables in two banks? 1) You want to use RAM to capture a serial string coming in from an external device, like a GPS or an electronic caliper or flue gas analyzer... 2) a particular sensor or analysis requires a lot of intermediate variables for purposes of computation.

    It depends a lot on the kind of programs you are writing. My programs tend to have lots of process variables that the program needs to access in sets of 5 or 6 words at a time. Then it finishes with those and moves on to the next set. Those are measurements from sensors, previous values, accumulations, numbers of samples, conversion factors, etc. I can't possibly fit all of those variables in the 13 words of Stamp ram, and using aliases would be very confusing to my way of thinking. So I name locations in Scratchpad for each of the variables, and in RAM I have generic variable names, along with a few important variables that run the underlying program machinery. When the time for each process comes along, the program reads the named variables from scratchpad into the generic RAM variables and does the crunching. The names in scrachpad are descriptive, and the scope limited, so the assignments do not get too confusing.

    Scratchpad is also useful for buffering data that has to go somewhere else. For example, a program running in one slot can put a bunch of data into the scratchpad, and then RUN to another bank, where that data can be acted on or moved or displayed etc etc.

    I agree totally with Jon that the time spent experimenting is not wasted. The Stamp has a segmented memory structure, and you have to figure out how to make that work for you in the kind of program you want to write.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • kelvin jameskelvin james Posts: 531
    edited 2004-12-21 19:13
    Thankyou Jon and Tracy for that great info. This will prove to be a "life saver" for me, as the variable space was beginning to create a major problem. Being still a little green on the stamp memory architecture and operation, i was having trouble trying to work through this, but now i understand the concept. I am working on a fairly complex motor control operation, am using all 8 banks, have 4 input/output devices, have to retrieve and store data and change some variables to modified variables using mathematical calculations, while still maintaining the original data. I am trying to stay away from writing to memory, as of the limits involved.It may sound like a bit much, but i know the bs2p can do it .
    And guess i should of used another term than "wasted time", as i have learned many a valuble lesson by working through problems on my own. On one situation, it took me 2 months to correct a bug, that i found out was a one line code change. I learned from that to try and read all possible info available, but sometimes like this, somethings are just hard to grasp at first. Thanks again. kelvin
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-21 19:41
    Bruce asked me in an email what I meant by
    "So I name locations in Scratchpad for each of the variables"

    I should have been more clear about that.

    Something like this:

    windspeed  CON 10   ' SPram word address for wind speed
    windmax  CON 12
    windavg    CON 14   ' average
    
     'Then in the program within a local scope:
    GET windspeed, word wx
    GET windmax, word wy
    GET windavg, word wx
       ' process it here
    PUT windspeed, word wx
    PUT windmax, word wy
    PUT windavg, word wx
    



    It is the addresses in SPram that have assigned names, as constants. Or I can pass the address in SPram as a pointer to a generic subroutine:

    wpe = windavg
    gosub updateaverage   ' generic routine used for all averaging
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • PaulPaul Posts: 263
    edited 2005-01-25 18:46
    As always, I'm impressed with the variations in stamp programming. If one thing doesn't quite work you can always come at the problem from a different angle. I realized the only thing I needed was the Device Code from the DS1822. This tells me if the DS1822 is installed. If not, skip all the temperature calulations.
    So now I use " OWIN dta,%0010,[noparse][[/noparse]STR Device\1,SKIP 7] " This saves over 25% of the variable space.

    I also am using the multibank programming and have found a "Ram Table" suits my style better. I always begin listing the locations in the header so I will always have them no matter what bank I am programming in. For instance:
    '-----[noparse][[/noparse] RAM Table ]------------------------------------------------------------
    '  accessed by PUT and GET
    '  7, Board      '"H"
    '  8, Revision   'start with 10 for .10
    '  9, Task       '(0 = initialize, 1 = Main)
    ' 10, alarmbits  'alarms (new alarms will flash)
    ' 11, oldbits    'save alarms
    ' 12, Loads      'loads 1-8
    ' 13, timeout   '60 second HOT timer
    ' 14, global
    ' 15, tempC     'temperature Celsius
    ' 20-21 Word, degF 'temperature Fahrenheit tenths
    
    



    If I need to check to see if loads are on I do a 'GET 12, loads'. Some banks may GET some variables and ignore others. Jon may recognize the "Task" variable.
    Thanks all for your help!
Sign In or Register to comment.