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.

IAR AVR C, Low/High byte of INT variable

Status
Not open for further replies.

Diamant

Newbie level 6
Joined
Oct 23, 2003
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
113
Need access to bytes of integer (16 bit).

I can do this in case if I know the integer address:
char *variable1 = (char*) 0xXXX; (address of integer)
char *variable2 = (char*) 0xXXX+1; (address+1 of integer)

is there any other way?
char *variable1=(char*) IntVariable; -could not be compiled

Moving integer left/right to receive Low/High byte is not god idea for me.

...Need help of advanced programmer...
 

artem

Advanced Member level 4
Joined
May 22, 2003
Messages
1,350
Helped
126
Reputation
252
Reaction score
32
Trophy points
1,328
Location
Turkey
Activity points
13,450
The better is to do it via union

typedef struct chara
{
char char1;
char char2;
} chars ;

typedef union tall
{
int16 intn ;
chars charn;
} mynumber;

mynumber tnum1.intn = 65535;
char first_byte = tnum1.charn.char1;
char second_byte = tnum1.charn.char2;
 

C-Man

Advanced Member level 4
Joined
Jul 19, 2001
Messages
1,059
Helped
90
Reputation
180
Reaction score
17
Trophy points
1,318
Activity points
10,189
What about this:

union
{
int integer;
unsigned char bytes[2];
}variable;

variable.integer to access it as an integer
variable.byte[0] and variable.byte[1] to access it bytewise

hope this is useful for you

Sorry for my post repeating artems answer, I could not see it while I typed this :)
 

Diamant

Newbie level 6
Joined
Oct 23, 2003
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
113
Thanks a lot, this is the perfect way out!!!
I forgot about unions...

off: going to read more about unions.
 

Diamant

Newbie level 6
Joined
Oct 23, 2003
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
113
debug has been successfull, thanks again
 

Eskimo

Member level 1
Joined
Apr 9, 2002
Messages
34
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
183
Or about this:

int integer;
char byteHi, byteLo;

byteHi = integer / 256;
byteLo = integer % 256

----------------------
or
byteHi = integer >> 8;
byteLo = integer;
 

Diamant

Newbie level 6
Joined
Oct 23, 2003
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
113
Eskimo, this code needs additionals variables in memory to be created, I havent got them at all
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top