Topic : Why does my static variable not initialize?

Forum : 8051

Original Post
Post Information Post
May 19, 2008 - 5:13pm
Guest

Hi,

I need some static variables in my function but declaring variables like:

static unsigned char myVar=15;

when evaluated it appears to be 0.

What am I missing?

Thx

Henk

Replies
Post Information Post
+1
0
-1
May 20, 2008 - 11:28am
Guest

static variables are supposed to be initialized by the startup routine. Are you using the default startup or your own?

+1
0
-1
May 20, 2008 - 1:34pm
Guest

Hi,

>Are you using the default startup or your own?

I allready included the file 'startup.a51' in my project if that's what you mean.

If I use the Option:
Option > Project > RC51 > Code generation > Initialize static variables to 0
does this mean that initialized static variables are set to zero?

Henk

+1
0
-1
May 20, 2008 - 7:03pm
Guest

At a first step, I suggest that you select the default startup:
Options > Project > LX51 > Startup > Default Startup

It should work (or it means that there is a bug in the compiler).
Then (if it works), you should analyze why your startup does initialize the static/global variables.
In the original startup, it is done in the area marked by:
IF INIDATA <> 0
but there are several parameters that have to be set.

+1
0
-1
May 21, 2008 - 10:09pm
Guest

Hi,

>Options > Project > LX51 > Startup > Default Startup

was allready set this way.

If I want a (global) xdata variable to be accessible throughout different modules do I have to declare it like:

static xdata unsigned char myVal; //in module A

extern xdata unsigned char myVal; //in module B

or

xdata unsigned char myVal; //in module A

extern xdata unsigned char myVal; //in module B

???

I searched Internet and read all Raisonance pdf manuals but I didn't succeed in finding the answer.

Thx,

Henk

+1
0
-1
May 22, 2008 - 9:16am
Guest

That's a general 'C' question. If you specify 'static' for a global variable in module A, it will NOT be visible in other modules (it would be considered as a 'private' variable in a C++ class).
Note that for a local variable, 'static' has a different meaning since it changes the allocation type (for a local variable, 'static' means 'non-automatic'.

+1
0
-1
July 2, 2008 - 1:55am
Guest

rshvw wrote:
I searched Internet and read all Raisonance pdf manuals but I didn't succeed in finding the answer.

Look here: http://c-faq.com/

As it's a standard 'C' question - nothing specifically to do with Raisonance or the 8051 - it should also be covered in any decent 'C' textbook.