Continue to Site

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.

integer to 8-bit conversion

Status
Not open for further replies.

latecomer

Member level 4
Joined
Jun 6, 2001
Messages
77
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,288
Location
USA
Activity points
642
Hello all,
I need to send an integer as 8 bit string to a flash memory. the integer will be 24 bit long . Anyone has any suggestion on how I can send the 24 bit integer as three 8 bit bytes in C code ?
Thanks in advance.
 

Assume the 24bit integer is stored in a 32bit variable called aa.

unsigned long aa;
unsigned char x,y,z;

x = (unsigned char)(aa / 0x10000); // the MSB 8 bits
send_to_flash(x);
y = (unsigned char)((aa % 0x10000) / 0x100); // the middle 8 bits
send_to_flash(y);
z = (unsigned char)(aa % 0x100); // the LSB 8 bits
send_to_flash(z);

Ther are other methods also. You can use the shift operator or you can use a union. The above method is not the most efficient. But it is easy to comprehend. The most efficient method is to use a union.
 

unsigned long aa;
unsigned char *tosend=(unsigned char*)&aa;
send_to_flash(tosend[0]);
send_to_flash(tosend[1]);
send_to_flash(tosend[2]);
Note: you must consider the endian problem and the order in which you send to flash. There is another probability:
send_to_flash(tosend[2]);
send_to_flash(tosend[1]);
send_to_flash(tosend[0]);
 

Keep in mind that 8051 and 8086 store in different endian formats..

The union method actually does the same thing. So you have to keep the endian problem in mind while using the union as well.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top