CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Counter for number of RESET

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
esa



Joined: 31 Jan 2008
Posts: 19

View user's profile Send private message

Counter for number of RESET
PostPosted: Sat Feb 09, 2008 4:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 09, 2008 8:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 5:05 am     Reply with quote

dear ckielstra,
That's exactly what I need ( and maybe more ). Very Happy
So, thanks a lot, for this informations. Wink
I will use "restart_cause".
Regards
Eric
Case closed
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 9:42 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 10, 2008 11:40 pm     Reply with quote

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)

View user's profile Send private message MSN Messenger

PostPosted: Mon Feb 11, 2008 2:08 am     Reply with quote

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).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group