|
|
View previous topic :: View next topic |
Author |
Message |
SteveK
Joined: 13 Nov 2005 Posts: 4
|
zeroing enums and bit variables at startup |
Posted: Tue Mar 07, 2006 10:09 pm |
|
|
Hi there,
I am using the watchdog on a 18F452 to cause the software to carry on from where it left off, ie without program initialisations. I am careful to initialise my variables only if it is a genuine powerup or brownout restart cause.
I have some enums, some of which have only two states, and are stored by the compiler as bit variables.
On a watchdog restart, the whole byte (at addres 0x02EE) which contains 8 bit variables is cleared, though I never set or clear any of these flags/enums through the watchdog restart path.
I also don't have these varibles initialised at the declaraion time.
Is this known behaviour? Can it be turned off? (Using PCWH 3.241)
Many thanks for your help,
Steve |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 07, 2006 10:33 pm |
|
|
According to the .LST file, the following test program does not clear
the 'mode' variable upon reset. I tested this with PCH vs. 3.241
and 3.245.
Are you using the #zero_ram directive ? If so, then 'mode' will be
cleared upon reset.
Code: | #include <18F452.H>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
enum mode {playing=1, recording=2, stopped=3};
//==============================
void main()
{
char state;
state = playing;
state = recording;
state = stopped;
while(1);
} |
|
|
|
Ttelmah Guest
|
|
Posted: Wed Mar 08, 2006 3:34 am |
|
|
Do you declare the variables as 'static'?.
If so, the compiler will clear them.
This behaviour changed, a long time ago. The manual will tell you that a static variable will be cleared. Remember that if you declare a variable externally to the main code, it will become functionally static anyway, and in this case will not be cleared (this was what was changed - a long time ago, the compiler would treat such 'default static' variables as if they were static, and clear them as well, which was a pain!...).
So, if I declare:
Code: |
enum test_enum {waiting=0,checking=1,testing=2};
test_enum val;
|
The compiler wll declare the variable, and not clear it. However if I declare:
Code: |
enum test_enum {waiting=0,checking=1,testing=2};
static test_enum val;
|
The compiler will now clear it.
Best Wishes |
|
|
SteveK
Joined: 13 Nov 2005 Posts: 4
|
|
Posted: Wed Mar 08, 2006 2:10 pm |
|
|
Hi guys,
Thanks so much for your responses. PCM programmer, no, I was not using the #zero ram directive.
Ttelmah, you have hit the nail on the head. I have declared the enum as a static within a function, so that it would keep its value between calls to that function.
The cure, of course, is simply to declare the enum as a global in main part of the program.
I will try this now - if I don't post again, assume all is well :-).
Many thanks again,
Steve |
|
|
|
|
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
|