janater123
Joined: 10 Dec 2010 Posts: 1
|
Weird result with read and write float to EEPROM |
Posted: Fri Dec 10, 2010 6:45 am |
|
|
+++++++++++++++++++++
Locked.
Reason: Atmega328p, AVR GCC are off-topic for CCS
-- Forum Moderator
+++++++++++++++++++++
I've really bashed my head around trying to figure out why the following code doesn't work. I am trying to write float values to the EEPROM of Atmega328p, using AVR GCC.
The output is "?" to the screen when I expect "14.4". Can someone verify that this does/doesn't work for them, or if the solution is obvious?
By the way, I included a write to the EEPROM with an integer value just to show that writing to the EEPROM is working correctly. The value "55" is printed to the screen.
code:
Code: |
int main(void)
{
init();
float set_value_float;
float read_value_float;
int set_value_int;
int read_value_int;
printf("starting up");
set_value_float = 14.4;
set_value_int = 55;
write_to_EEPROM( EEP_INT_LOCATION, set_value_int );
write_float_eep( EEP_FLOAT_LOCATION, set_value_float );
read_value_int = read_from_EEPROM( EEP_INT_LOCATION );
read_value_float = read_float_eep( EEP_FLOAT_LOCATION );
printf( "\r\nRead float value: %.1f\r\nRead int value: %u",read_value_float, read_value_int );
while(1){}
}
//EEPROM
void write_float_eep(unsigned int address, float data) {
write_to_EEPROM(address,*((char*)&data));
write_to_EEPROM(address+1,*((char*)&data+1));
write_to_EEPROM(address+2,*((char*)&data+2));
write_to_EEPROM(address+3,*((char*)&data+3));
}
float read_float_eep(unsigned int address) {
float temp;
*((char*)&temp) = read_from_EEPROM(address);
*((char*)&temp+1) = read_from_EEPROM(address+1);
*((char*)&temp+2) = read_from_EEPROM(address+2);
*((char*)&temp+3) = read_from_EEPROM(address+3);
return (temp);
}
|
|
|