View previous topic :: View next topic |
Author |
Message |
esa
Joined: 31 Jan 2008 Posts: 19
|
Counter for number of RESET |
Posted: Sat Feb 09, 2008 4:45 am |
|
|
Dear users,
I use a external WatchDog device ( MAX6301) to reset the PIC in case of hangUp. So, it's a real reset on the PIN MCLR.
How can I count the number of reset ?
I already do that by setting a PIN (on a PCF8574) as output in my main code.
When the reset occur with the WD ( so without power off ).At the start of my program, I check this PIN.
I increment a counter if PIN =1 and store it in EEPROM.
When a reset occur by power off, the PIN is low, so I don't increment the counter ( it's a normal power off ).
But, now I can't use a PCF8574.
Is it a way by doing this directly with the PIC18F4620 ?
Using a variable, or a PIN ?
I think, it's not possible.
Best Regards
Eric |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Feb 09, 2008 8:26 am |
|
|
Have you tried using the function Restart_cause()? Use this as one of the first functions in your program and it should give you the reason for restart. On a PIC18F4620 it can give you the following values: Code: | // Constants returned from RESTART_CAUSE() are:
#define WDT_TIMEOUT 7
#define MCLR_FROM_SLEEP 11
#define MCLR_FROM_RUN 15
#define NORMAL_POWER_UP 12
#define BROWNOUT_RESTART 14
#define WDT_FROM_SLEEP 3
#define RESET_INSTRUCTION 0 | You should get NORMAL_POWER_UP after a power down or MCLR_FROM_RUN when the reset pin was triggered.
Another solution would be to place a checksum in a global variable. After a reset the contents of RAM are not touched so the checksum should still be there. In case of power down the variable will contain random data. With an 8 bit checksum there is a 1/256 chance the random value equals your checksum, giving a wrong detection. Use a 16 or 32 bit checksum for even better detection rates.
Code: | #define MAGIC_BOOT_VALUE 0x12345678
int32 WarmStartDetect;
void main()
{
if (WarmStartDetect == MAGIC_BOOT_VALUE)
{
// Reset detected
}
else
{
// Normal power up
WarmStartDetect = MAGIC_BOOT_VALUE; // Set value for next reboot
} |
|
|
|
esa
Joined: 31 Jan 2008 Posts: 19
|
|
Posted: Sun Feb 10, 2008 5:05 am |
|
|
dear ckielstra,
That's exactly what I need ( and maybe more ).
So, thanks a lot, for this informations.
I will use "restart_cause".
Regards
Eric
Case closed |
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Sun Feb 10, 2008 9:42 pm |
|
|
You can do this by looking in the RCON register. At the beginning of your program, check whether the ~POR bit is clear, that indicated it was a power-on reset. (If it is clear, you have to set it in software). I did this for a project at work with the 18LF4520, and the 4620 is equivalent for this. Check the datasheet. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 10, 2008 11:40 pm |
|
|
That's what restart_cause() does. It reads the RCON register, and it
also sets the bits = 1, so it's ready to detect the cause of the next reset.
Here is the .LST file for the 18F4620 with vs. 4.066:
Code: |
........ value = restart_cause();
00018: MOVF RCON,W
0001A: ANDLW 0F
0001C: BTFSS RCON.RI
0001E: MOVLW 00
00020: BSF RCON.BOR
00022: BSF RCON.POR
00024: BSF RCON.RI
00026: BSF STATUS.OV
00028: BSF STATUS.N
0002A: MOVWF value |
|
|
|
D-Kens
Joined: 09 May 2005 Posts: 35 Location: Toulouse (France)
|
|
Posted: Mon Feb 11, 2008 2:08 am |
|
|
Remember to use "restart_cause()" at the very beginning of your main, even before all the setup commands, cause some of them are likely to change the value (so I read). |
|
|
|