Finding the Cause of Reset in PIC® Devices Using the MPLAB® XC8 Compiler

Last modified by Microchip on 2023/11/09 09:13

PCON and STATUS Registers in PIC Microcontrollers

The Microchip baseline and mid-range PIC® MCU devices employ a special feature that lets you establish the cause of a device reset once your program has restarted. This feature uses several flag bits in the PCON and STATUS registers, which are written by some instructions and on certain events. These registers can be read by your code to determine the conditions which triggered a device reset.

The two bits in the STATUS register are the PD bit, which is set on power-up and cleared when SLEEP is invoked, and the TO bit, which is cleared if a Watchdog Timer (WDT) time-out occurred and caused a device wake-up. Both of these bits are not directly writable by software but can be written by hardware events.

Since the content of the STATUS register could be lost before your code can access it, the MPLAB® XC8 C Compiler provides a feature that can automatically copy the STATUS register to a location that can be accessed from C code at a later time. This step takes place at the beginning of the runtime startup code generated by the compiler, and so it will be executed shortly after the reset has taken place.

To access the saved content of the STATUS register, your C code should declare and reference the symbol __resetbits (note the two leading underscores). You can also declare and reference the symbols __powerdown and __timeout, which hold copies of the Power-down and Time-out bits, respectively. If required, declare these as follows in your program.

This STATUS register preservation feature is automatically enabled once you access any of the above symbols in your project. Your code must actually read a symbol and do something useful with it; just declaring the symbol alone will not enable this feature. The following is an example of code for a 16F1xxx device that uses the STATUS register copy to look for the copied TO bit being set, the copied PD bit being clear, and the RMCLR bit in the regular PCON register being clear. Such a situation indicates a master clear that occurred during sleep.

Check your device datasheet for full information about your device reset conditions and flags used to detect these.

If you wish to confirm the operation of this feature, open the startup.as or startup.lst file after building your project and search for any of the above symbols (in assembler, there will be three leading underscore characters). You will need to enable the keep suboption to the —RUNTIME option.  Alternatively, select XC8 Linker under Categories, Runtime under Option categories, and check Keep generated startup.as in the MPLAB X IDE Project Properties window to see this file, as it is normally deleted after each build.

project properties window showing the Keep generated startup.as option

You can manually force the STATUS register to be always copied at startup by enabling the resetbits suboption to the —RUNTIME option. Alternatively, select XC8 linker under Categories, Runtime under Option categories, and check Backup reset conditions flags in the MPLAB X IDE Project Properties window.

project properties window showing the backup reset condtion flags option