View previous topic :: View next topic |
Author |
Message |
object01
Joined: 13 May 2004 Posts: 90 Location: Nashville, TN
|
Odd Memory Usage |
Posted: Thu Jun 03, 2004 8:11 am |
|
|
I'm very new to embedded software, and I'm hoping someone can help me understand why I'm observing this:
I'm working with a PIC16F88, and up until recently I've been getting lots of "Not enough RAM for all variables" errors. As a pure shot-in-the-dark, I commented out a 'char buffer[59]' declaration at global scope, and commented out all references to it. All of a sudden, my code compiles and I have 59% RAM free! Put the buffer back, and I'm back to "not enough RAM." (PCM 3.189) How is this possible? |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu Jun 03, 2004 9:23 am |
|
|
There is not an exact 1:1 relationship between the number of bytes you allocate in your code and the number of bytes the compiler uses. Your array of 59 characters is about 16% of total available RAM. If the functions where you were using the array require accessor variables or scratchpad then the size goes up. Also in some cases the compiler use the RETLW method of accessing RAM arrays and that can take more space too.
If your functions use argument lists there will be scratchpad RAM required for passing arguments.
If the compiler can't resolve the scope of a variable it may not reuse that RAM eventhough it could have.
Another thing that seems to eat up RAM is the way the compiler assigns things to banks. It never quite gets things fit perfectly so you loose a few bytes here and there. You can override the automatic assignment of variables by doing it yourself.
Lots of interactions cause the memory used or available to be off. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 03, 2004 9:53 am |
|
|
Quote: | All of a sudden, my code compiles and I have 59% RAM free!
Put the buffer back, and I'm back to "not enough RAM."
(PCM 3.189) How is this possible? |
It's possible because the default mode for the CCS compiler
is to not use ram above address 0xFF, for the 16-series PICs.
To enable usage of this RAM, you need to add the #device *=16
directive. When you do this, the compiler will use more ROM,
due to the code used for bank switching, but it will get rid of
your problems with "out of RAM" errors.
Example:
Code: | #include <16F88.H>
#device *=16 |
Put the "device" statement on the next line after the main include
statement, exactly as shown above. That's important. |
|
|
prwatCCS
Joined: 10 Dec 2003 Posts: 70 Location: West Sussex, UK
|
|
Posted: Fri Jun 04, 2004 3:46 am |
|
|
I have found it sometime helps to place large arrays at the beginning of the next bank of ram, rather than let the compiler try and fit it (and fail). _________________ Peter Willis
Development Director
Howard Eaton Lighting Ltd UK |
|
|
|