0

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?

Davide Andrea
  • 1,357
  • 2
  • 15
  • 39

1 Answers1

1

Variables can be declared as __persistent:

__persistent int counter;

This should prevent the startup code from initializing it.

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • Thanks. My quick search tells me that support for the persistent attribute depends on the family of micros. I'll see if it works on mine: PIC30F. – Davide Andrea Feb 22 '16 at 23:55
  • 1
    @DavideAndrea : Even if your compiler does not support `__persistent` it is simple enough (and more portable) to modify your linker script and/or start-up code to reserve a section of memory uninitialised and explicitly locate your variable there. It is possible to do this without any special language extensions, though it may be safe to use linker directives rather then literal addresses. So long as the memory in in SRAM and power is not interrupted, in most systems the content will survive a reset. On-chip registers will normally have hardware initialisation so SRAM is the better bet. – Clifford Feb 23 '16 at 11:21