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 support@ccsinfo.com

zeroing enums and bit variables at startup

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



Joined: 13 Nov 2005
Posts: 4

View user's profile Send private message Send e-mail

zeroing enums and bit variables at startup
PostPosted: Tue Mar 07, 2006 10:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 07, 2006 10:33 pm     Reply with quote

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







PostPosted: Wed Mar 08, 2006 3:34 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 08, 2006 2:10 pm     Reply with quote

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
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