Detecting a Reset Button Press in Swordfish

big red switch1A reset button press or power being applied to a PIC will result in program execution and running the initialization routines in the program.  I would like to execute a branch of my code with configuration options only when the reset button (which is accessible only by removing a panel with four screws) and not during a startup that would occur when power is applied.  The question is if it's possible to differentiate between these events.

A bit of background.  The /MCLR (Master Clear) line is normally pulled high with a 10k resistor.  A reset switch pulls /MCLR to ground, causing a reset.  It's essential that this line not be left floating.  It may be disabled and used as a normal digital line but this can create difficulties when using ICSP.

I used an 18F2520 for this example, but other 18F-series parts are similar although the means of individual bits may be different.

The first stop is the data sheet.  There is an entire section named Reset, so that's a good place to start.  The RCON register has the status of various reset options.  WDT (watch dog timer), software reset, POR (power on reset), BOR (Brown Out Reset) are among the options.


A /MCLR reset is notably missing from this register!  The is apparently no flag to indicate that the reset button was pressed.  Looking further, Table 4-3 shows the status of the RCON register after a reset.

Reset Status

"/MCLR Reset during Full-Power Execution" sounds like the event I'm after.  All of the RCON bits are listed as "u" which means the bits are unchanged during a reset.  So a reset button press can't be detected directly.

So what are the other options to detect a reset?  Looking at the RCON register table, the POR (power-on reset) sounds like it could detect the normal situation of power being reset.  Back to the data sheet to determine the details about a POR:

4.3 Power-on Reset (POR)

A Power-on Reset pulse is generated on-chip
whenever VDD rises above a certain threshold. This
allows the device to start in the initialized state when
VDD is adequate for operation.

To take advantage of the POR circuitry, tie the MCLR
pin through a resistor (1 kΩ to 10 kΩ) to VDD. This will
eliminate external RC components usually needed to
create a Power-on Reset delay. A minimum rise rate for
VDD is specified (parameter D004). For a slow rise
time, see Figure 4-2.

When the device starts normal operation (i.e., exits the
Reset condition), device operating parameters (voltage,
frequency, temperature, etc.) must be met to
ensure operation. If these conditions are not met, the
device must be held in Reset until the operating
conditions are met.

POR events are captured by the POR bit (RCON<1>).
The state of the bit is set to ‘0’ whenever a POR occurs;
it does not change for any other Reset event. POR is
not reset to ‘1’ by any hardware event. To capture
multiple events, the user manually resets the bit to ‘1’
in software following any POR.

So, a power-on reset can be detected.  I can determine if the reset was caused by cycling power and skip the configuration section of code if this is the case.  There are a couple items to note in the above section.  First, /MCLR must be pulled high by a 1kΩ - 10kΩ resistor to detect that power has been off.  If the power supply is slow to stabilize, so additional circuitry will be required.  The resistor is required anyway to prevent /MCLR from floating and causing mystery resets.

The other item to note is in the last paragraph above.  The POR bit is not reset automatically - it must be set to 1 by your code after a POR has occurred.

The RCON register location varies between different 18F-series parts.  Fortunately, its location is defined in the device file, so it's not necessary to know its location.  The code below is a simple example of detecting a reset button (/MCLR) reset.


Detecting a Reset Button Press
Name    : UNTITLED.BAS                                                      **  
Author  : Jon Chandler                                                      **  
Notice  : Copyright (c) 2012 Creative Commons 3.0 sa-by-nc                  **          
          All Rights Reserved                                               **  
Date    : 3/21/2012                                                         **  
Version : 1.0                                                               **  
Notes   :                                                                   **                                                                            
Device = 18f2520
Clock = 20 
Include ("suart.bas")
include ("convert.bas")  
uart.settx(portb.7)    ' matches the PICkit 2 UART tool input pin when connected to the ICSP connector 
if rcon.1 = 1 then    
    uart.write ("Reset button pressed"1310)
end if
if rcon.1 = 0 then 'A Power-On reset has occured
    rcon.1 = 1
end if 
While 1 = 1  
    uart.write (bintostr(rcon)13,10)
    delayms (5000)  



Forum Activity

Member Access