Topic : Access bit variable declared in another c file.

Forum : 8051

Original Post
Post Information Post
June 14, 2011 - 7:31pm
Guest

Hello,

I'm developing an application composed by multiple c files.

In one file, I declared some bit variables in "bdata" space. The functions in other c files need to access these bit variables. My current codes are like that:

In the Main.c file, I wrote:

void main(void)
{
bdata at 0x20 char SwitchSensors;
sbit Switch0 = SwitchSensor^0;
sbit Switch1 = SwitchSensor^1;

...

}

Then, in Func1.c file, I declare like that:

void func1(void)
{
extern bdata char SwitchSensors;
extern sbit Switch0 = SwitchSensor^0;
extern sbit Switch1 = SwitchSensor^1;

...
}

The Compiler always says the two "extern sbit" declarations are invalid ---- it is quite understandable.

My question is:

In general, how can I access public bit variables declared in another file?

Thanks in advance.

Regards,

WD.

Replies
Post Information Post
+1
0
-1
June 15, 2011 - 9:52am
Raisonance Support Team

Hi,

Thanks for your report.
Your problem is that your variables are declared in the main() function body, hence they are local variables. If you need to access them as external you need to declare them outside of any function. Here is an example:

bdata  at 0x20  char  SwitchSensors;
sbit   Switch0 = SwitchSensor^0;
sbit   Switch1 = SwitchSensor^1;

void main(void)
{
...
}

Using this, the Func1.c file will compile and the whole project will link.
Best Regards,

+1
0
-1
June 15, 2011 - 5:50pm
Guest

Hi Bruno,

I made a mistake when writing my original report. These sbit declarations are not made in main() function.

Actually, in the real codes, these declarations are made in a *.h file, and this file is included in main.c file.

So, these declarations are actually out side any functions.

Sorry about that.

WD.