freeing up memory?
Paul
Posts: 263
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.
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
Then 'READ' them and use them to initialize with.
Then ignore them the rest of the time.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
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 Williams
Applications Engineer, Parallax
Dallas Office
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
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?
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
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.
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
·· 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
·
It takes a second to understand what's happening, but it does work.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
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
And let me suggest that time spent experimenting is not wasted time.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
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
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
"So I name locations in Scratchpad for each of the variables"
I should have been more clear about that.
Something like this:
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:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
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:
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!