Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to declare the byte variable at header file?

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi,

a.c
Code:
union Data
{
	unsigned char nbit;
};
bdata struct Bit
{
	union Data byte;
}Ex;  

sbit xbit0 = Ex.byte.nbit^0;
sbit xbit1 = Ex.byte.nbit^1;
sbit xbit2 = Ex.byte.nbit^2;
sbit xbit3 = Ex.byte.nbit^3;
sbit xbit4 = Ex.byte.nbit^4;
sbit xbit5 = Ex.byte.nbit^5;
sbit xbit6 = Ex.byte.nbit^6;
sbit xbit7 = Ex.byte.nbit^7;

a.h
Code:
extern bit xbit0;
extern bit xbit1;
extern bit xbit2;
extern bit xbit3;
extern bit xbit4;
extern bit xbit5;
extern bit xbit6;
extern bit xbit7;

Does anyone know how to declare the "nbit" valiable as global variable? In the a.c file we can easily assign the valiable to
Code:
Ex.byte.nbit = 1;
but i don't know how to declace the Ex.byte.nbit as global variable and use it in other .c file.

Thank you.
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,863
Bit Addressing

Declare it as extern in a header file and include the header file in the c files that want to use it.
 

    Help

    Points: 2
    Helpful Answer Positive Rating

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Re: Bit Addressing

Hi btbass,

Thank for your reply. Yes, what you have spoken is rite but how to declare the byte variable at header file? Please have a look in the attactment.

Thank you.
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,863
Re: Bit Addressing

It seems that you might be better off using bitfields.
Code:
/* stick this in a header file */

struct mybitfield
{
unsigned bit_0:1;
unsigned bit_1:1;
unsigned bit_2:1;
unsigned bit_3:1;
unsigned three_bits:3;
unsigned bit_7:1;
};

typedef union
{
struct mybitfield bits;
unsigned char thebits;
}BIT_UNION;

extern volatile BIT_UNION Bit_union;

/* declare instance in a c file */

volatile BIT_UNION Bit_union;

/* use like so */

Bit_union.thebits = 0;

Bit_union.bits.bit_0 = 1;
Bit_union.bits.bit_1 = 1;
Bit_union.bits.three_bits = 7;

/* etc */

/* Or passed as a pointer */

Bit_union->thebits = 0;

Bit_union->bits.bit_0 = 1;

If you just want to set and clear the bits, you dont need a union.

Code:
/* stick this in a header file */

typedef struct
{
unsigned bit_0:1;
unsigned bit_1:1;
unsigned bit_2:1;
unsigned two_bits:2;
unsigned three_bits:3;
}MYBITFIELD;

extern volatile MYBITFIELD My_bits;

/* declare instance in a c file */

volatile  MYBITFIELD My_bits;

My_bits.bit_0 = 1;
My_bits.two_bits = 0x03;
My_bits.three_bits = 0b010;

/* etc */
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top