Finding the Cause of Reset

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. Alternitavely, 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.

keep_startup_option.png

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.

backup_reset_condition_flags_option.png
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.