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

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;
 

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
 

Thanks a lot, this is the perfect way out!!!
I forgot about unions...

off: going to read more about unions.
 

debug has been successfull, thanks again
 

Or about this:

int integer;
char byteHi, byteLo;

byteHi = integer / 256;
byteLo = integer % 256

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

Eskimo, this code needs additionals variables in memory to be created, I havent got them at all
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…