Persistent Variable Setup

Last modified by Microchip on 2024/01/17 22:22

Persistent variables are variables that should not be cleared by the runtime startup code, such as during a reset. If you have a variable that you don't want to have initialized upon a Power On Reset (POR)Watch Dog Timer Reset (WDT), or Master Clear Reset (MCLR), define it as persistent. This is often needed for bootloader entry/exit when the application needs to differentiate between a WDT, MCLR, or other reset conditions. See the following example of how to establish a persistent variable:

/*******************************************************************************
 *   Persistent RAM variables, which are not initialized at power up / reset.
 *
 ******************************************************************************/


#if defined(__XC8__)
   #ifdef __CCI__
       __persistent unsigned char PORStatus;
   #else
       persistent unsigned char PORStatus;
   #endif

#elif defined(__XC16__)
   #ifdef __CCI__
       __persistent int PORStatus;
   #else
       int PORStatus __attribute__((persistent));
   #endif

#elif defined(__XC32__)
   #ifdef __CCI__
       __persistent int PORStatus;
   #else
       int PORStatus __attribute__((persistent));
   #endif

#endif