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.

Converting floating point number into byte

Status
Not open for further replies.

Maverickmax

Advanced Member level 1
Joined
Dec 6, 2004
Messages
404
Helped
8
Reputation
16
Reaction score
3
Trophy points
1,298
Activity points
3,689
Hi

Since I have managed to use M42C02 via I2C. Now I need to know how to store int (2 bytes), long int values (4 bytes) and floating point number (4 bytes) in EEPROM chip. But I don't have any clue how to do it. Your help would be appreicated

MM
 

Suppose the floating point result is obtained from a sensor of 16-bit resolution, we may use a union to store the raw sensor result. By using a union, we store the floating point (4-bytes) in the union. The advantage is that, we save a lot for the eeprom by storing only the raw data. Consider this:

Code:
typedef union
{
	unsigned int i;  //raw data obtained from the sensor
	float f;
} value;

void main()
{
   value temp_val;

   //store temp_val.i in eeprom for 2 bytes here to store the raw data
   eeprom_store2bytes(address,temp_val.i);
   //read back the value and store it back in temp_val.i
   eeprom_read2bytes(address,&temp_val.i);
}


However, if there is a need to store the floating point result without considering what sensor raw data is nor using union, it is possible to locate the address of the floating point declared and store that 4-bytes representation by:
Code:
unsigned char *eeprom_ptr;
unsigned char eeprom_char, i;
float fVal;
fVal=3.145678;

main(){
eeprom_ptr = (unsigned char*)&fVal;
for(i=0;i<4;i++)
{eeprom_char = *eeprom_ptr++;
write_eeprom(address++,eeprom_char);
}
}
John Leung
 

TechToys said:
Consider this:




Code:
typedef union
{
	unsigned int i;
	float f;
} value;

void main()
{
   value temp_val;

   //store temp_val.i in eeprom for 4 bytes here
   eeprom_store4bytes(address,temp_val.i);
   //read back the value and store it back in temp_val.i
   eeprom_read4bytes(address,&temp_val.i);

  //floating point result here, may be printed to LCD
   temp_val.f=(float)temp_val.i;   
}

John Leung

Can you please kindly give me more information about it because Im unfamaliar with union as I have not used it before

MM
 

A union has the same form as a struct, however the union's members overlap, all occupying the same memory space.
 

Hi

Have revised the code as above, or you may refer to another thread



John Leung
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top