CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Weird result with read and write float to EEPROM

 
Post new topic   This topic is locked: you cannot edit posts or make replies.    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
janater123



Joined: 10 Dec 2010
Posts: 1

View user's profile Send private message

Weird result with read and write float to EEPROM
PostPosted: Fri Dec 10, 2010 6:45 am     Reply with quote

+++++++++++++++++++++
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);       

}
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group