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

Writing a float to a serial 24LC256 eeprom

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

Writing a float to a serial 24LC256 eeprom
PostPosted: Sun Sep 13, 2009 10:57 am     Reply with quote

Hello,
I have been having a frustrating time trying to write or read a float value from a 24LC256 I2C serial eeprom. Using the CCS driver I have no problems reading and writing int8 data. So I visited the forum to get an idea as to how others were doing this and tried various ways but apparently I am still having issues with the floats. If I store a float and then try to read it I seem to get bogus values. I am sure I am overlooking something very obvious or just doing something very stupid. Embarassed Any advice is appreciated

BTW: I am using the PCD compiler on a PIC24H and using the hardware I2C1 channel
Code:

/***********************************************************
void init_ext_eeprom()  // I don't this is needed in hardware I2C
{
   output_float(EEPROM_SCL);
   output_float(EEPROM_SDA);

}


void write_ext_eeprom(int16 address, int8 data)  //from CCS obviously I cannot call this from iside my routines works ok on bytes
{
   int1 status;
   i2c_start();
   i2c_write(0xa0);         // (0xa0)based on a0,a1,a2 on device set to 0
   i2c_write(address>>8);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
   i2c_start();
   status=i2c_write(0xa0);
   while(status==1)
   {
   i2c_start();
   status=i2c_write(0xa0);
   }
   i2c_stop();
}


int8 read_ext_eeprom(int16 address) {   //from CCS obviously I cannot call this from iside my routines works ok on bytes
   int8 data;
   i2c_start();
   i2c_write(0xa0);
   i2c_write(address>>8);
   i2c_write(address);
   i2c_start();
   i2c_write(0xa1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}






Write_Myfloat_exteeprom(long int address, float data) //My attempt at writing a float

{
int i;
int1 status;
i2c_start();
i2c_write(0xa0);         // (0xa0)based on a0,a1,a2 on device address set to 0
i2c_write(address>>8);
i2c_write(address);
for (i = 0; i < 4; i++)
i2c_write(*((int8 *)(&data) + i));
i2c_stop();
i2c_start();
status=i2c_write(0xa0);
while(status==1)
{
 i2c_start();
 status=i2c_write(0xa0); }
 i2c_stop();

}





float Read_Myfloat_exteeprom(EEPROM_ADDRESS address) //My attempt at reading the float
{
   int8 i;
   float data;
   i2c_start();
   i2c_write(0xa0);
   i2c_write(address>>8);
   i2c_write(address);
   for(i = 0; i < 4; ++i)
   (*((int8 *)(&data) + i)) = i2c_read(0);
   i2c_stop();
   i2c_start();
   i2c_write(0xa1);
   i2c_stop();
   return data;
}


*/
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Sep 13, 2009 11:21 am     Reply with quote

Quote:

Write_Myfloat_exteeprom(long int address, float data) //My attempt at writing a float
{
.
.
.
for (i = 0; i < 4; i++)
i2c_write(*((int8 *)(&data) + i));

i2c_stop();
i2c_start();

// Ack polling
status=i2c_write(0xa0);
while(status==1)
{
i2c_start();
status=i2c_write(0xa0); }
i2c_stop();

}

It fails because you're writing 4 bytes in a for() loop, and the ack-polling
routine must be applied after each byte. Your code doesn't do that.

CCS provides the routines to do this. You don't have to write your
own routines. Just add these routines to your program, and #include
the 24256.c driver. It's much easier that way.
http://www.ccsinfo.com/faq.php?page=write_eeprom_not_byte
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

PostPosted: Sun Sep 13, 2009 4:50 pm     Reply with quote

Once again, thank you PCM programmer!!

I had a feeling it was something stupid with the way I was sending data to the device.
Display posts from previous:   
Post new topic   Reply to topic    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