View previous topic :: View next topic |
Author |
Message |
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
Writing a float to a serial 24LC256 eeprom |
Posted: Sun Sep 13, 2009 10:57 am |
|
|
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. 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
|
|
Posted: Sun Sep 13, 2009 11:21 am |
|
|
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
|
|
Posted: Sun Sep 13, 2009 4:50 pm |
|
|
Once again, thank you PCM programmer!!
I had a feeling it was something stupid with the way I was sending data to the device. |
|
|
|