Some PIC® devices have Timeout (TO) and Power Down (PD) bits in the STATUS register that can be used to determine the cause of a reset. However, these bits are quickly overwritten by the runtime startup code that is executed before main is executed.
With MPLAB® XC8, the --RUNTIME suboption "resetbits" can be used to preserve these bits, and the entire STATUS register, in variables that can be examined later in your program. This option can be specified in MPLAB X IDE > Project Properties > XC8 Linker category > Runtime drop-down menu > Check / Uncheck the Backup reset condition flags (This field is not available for all the devices).
The state of these bits can be examined after recovering from a Reset condition to determine the cause of the Reset.
The entire STATUS register is saved to an assembly variable __resetbits. This variable can be accessed from C code using the declaration:
The compiler defines the assembly symbols __powerdown and __timeout to represent the bit address of the PD and TO bits within the STATUS register and can be used if required.
For example:
Make sure that the Backup reset condition flags option is selected under MPLAB X Project Properties > XC8 Linker > Runtime (Some devices do not have this option selectable).
For more details, refer to the “Status Register Preservation” section in the Compiler Manual.