Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Microchip C Routine to Calculate Used Program and Data Memory?

Status
Not open for further replies.
Hi,

Best regards to everyone.

Working on a program for an 18F4520 at the moment that displays a start-up / splash screen on a 4 x 20 LCD. Just for novelty it would be nice to show the amount of used or free program and data memory and have this calculated automatically rather than writing it manually into the program. Is this easely doable? Not sure where to start, any hints much appreciated.

Thanks in advance for any help.
 
Last edited:
I doubt this is possible as the compiler doesn't know what/where things are included/placed, the linker takes care of that by which time it is too late to "insert" it in the code.

Mike.
 
It's an interesting idea.

Assuming the PIC can somehow examine the contents of any ROM address, you could loop through the program memory and count how many blank locations there are as the programming sets all blank ROM locations to a particular value.

Then RAM is quite a bit harder... Maybe while it is looping and counting ROM values it could look for any instructions that read or modify a RAM location and then record that RAM location as used. This would require RAM to hold the usage table but seems do-able. The only down side is any indirect addressing of RAM.

Then again if you just want the FEATURE of showing ROM and RAM usage to the user, the best way would be to compile, look at the compiler results, then manually modify 2 constants in the code that will be displayed (or put those 2 values in eeprom?).
 
In assembler you would be able to place a label at the end of your code and then use the address of that label to calculate how much rom is free (Total ROM - label address = free rom). It would automatically be updated at compile time. For ram i don't see an option.

Wouldn't really know how to implement this in C.
 
A brutal solution may be to initialize the heap to 0, and then just count any non-zero bytes. When you free memory, however, you'd have to also zero it.
You could measure the stack via the stack pointer (ESP in Intel x86 assembler). Although, I don't know if PICs use anything like ESP.

For ROM, solutions have been posted above. Searching for a tag seems like the best idea.

All this is possible in C, but you will need to use inline assembler. Again, I don't even know if that's possible with whatever compiler you're using.
 
For flash I think it would work something like this (top of head)

Create a section in your linker command file called LASTBYTE.
EDIT: Setup the linker command file to position this section last.

Declare a constant byte in that section.
#pragma section LASTBYTE
rom const LastByte = 0xAA;
#pragma section CODE

then in the code use @LastByte to get the location of the last byte of flash used.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top