Search code examples
cembeddedramreal-time-clock

Prevent compiler-generated code from clearing a location at initialization


We implemented a Real Time Clock in a PIC micro, which increments a count of seconds in RAM.

If there's a reset, the C code created by the compiler will clear the RAM, and the count is lost. (That is not a problem if we use assembly instead of C.)

Is there a way to tell the compiler not to clear a particular RAM location?

Is there a RAM area that is not cleared by the C code?

Should we appropriate some unused registers and use them instead of using RAM?


Solution

  • Variables can be declared as __persistent:

    __persistent int counter;
    

    This should prevent the startup code from initializing it.