/*
want to save (and retreve) a float from EEPROM ...
*/
#include "mbed.h"
DigitalOut myled(LED1);
Serial pc (USBTX, USBRX);
float float_1 = 0.00000578; // 77e12; // 124.843;
float float_2 = 0;
float float_3 = 0;
float float_4 = 0;
struct
{
float f1;
float f2;
float f3;
float f4;
} g_settings;
//
struct
{
float f_1;
float f_2;
float f_3;
float f_4;
} g2_settings;
int i, Len;
char My_Byte_Array [128];
int EEPROM_writeAnything(int ee, char * value);
int EEPROM_readAnything (int ee, char * value);
int EEPROM_writeAnythingSize(int ee, int size, char * value);
int EEPROM_readAnythingSize (int ee, int size, char * value);
int main()
{
// fill EEPROM with zeros ....
for (i=0;i<128;i++)
{
My_Byte_Array[i] = 0;
}
g_settings.f1 = 23.006;
g_settings.f2 = 0;
g_settings.f3 = 1234567890;
g_settings.f4 = 22.0/7.0;
//
g2_settings.f_1 = 0;
g2_settings.f_2 = 0;
g2_settings.f_3 = 0;
g2_settings.f_4 = 0;
// print out ..
pc.printf ("\r\nArray Contents\r\n");
for (i=0;i<128;i++)
{
pc.printf ("[%02x] ",My_Byte_Array[i]);
if ((i & 0x0f) == 0x0f) pc.printf ("\r\n");
if ((i & 0x0f) == 0x07) pc.printf (" : ");
}
pc.printf ("\r\n");
// push float into EEPROM
Len = EEPROM_writeAnything(0, (char*)&float_1);
pc.printf ("Length float = %d\r\n", Len);
Len = EEPROM_writeAnythingSize(8, sizeof(g_settings), (char*)&g_settings ) ; // // float_1);
pc.printf ("Length Struct = %d\r\n", Len);
// pop folat from EEPROM
EEPROM_readAnything (4,(char*)&float_2);
// print it all out again ..
pc.printf ("\r\nArray Contents\r\n");
for (i=0;i<128;i++)
{
pc.printf ("[%02x] ",My_Byte_Array[i]);
if ((i & 0x0f) == 0x0f) pc.printf ("\r\n");
if ((i & 0x0f) == 0x07) pc.printf (" : ");
}
pc.printf ("\r\n");
pc.printf ("Write float .. %6.16f\r\n", float_1);
pc.printf ("Read float .. %6.16f\r\n", float_2);
Len = EEPROM_readAnythingSize(8, sizeof(g_settings), (char*)&g2_settings ) ;
pc.printf ("Read of struct eliment 1 .. %6.16f\r\n", g2_settings.f_1);
pc.printf ("Read of struct eliment 2 .. %6.16f\r\n", g2_settings.f_2);
pc.printf ("Read of struct eliment 3 .. %6.16f\r\n", g2_settings.f_3);
pc.printf ("Read of struct eliment 4 .. %6.16f\r\n", g2_settings.f_4);
while(1)
{
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
}
}
/////////////////////////////////////////////////////////////
// write to array
int EEPROM_writeAnything(int ee, char * value)
{
int i;
for (i = 0; i < sizeof(value); i++)
//EEPROM.write(ee++, *p++);
My_Byte_Array [ee++] = *value++;
return i;
}
//////////////////////////////////////////////
int EEPROM_readAnything (int ee, char * value)
{
int i;
for (i = 0; i < sizeof(value); i++)
//*p++ = EEPROM.read(ee++);
*value++ = My_Byte_Array [ee++];
return i;
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// write to array
int EEPROM_writeAnythingSize(int ee, int size, char * value)
{
int i;
//for (i = 0; i < sizeof(value); i++)
for (i = 0; i < size; i++)
My_Byte_Array [ee++] = *value++;
return i;
}
//////////////////////////////////////////////
int EEPROM_readAnythingSize (int ee, int size, char * value)
{
int i;
//for (i = 0; i < sizeof(value); i++)
for (i = 0; i < size; i++)
*value++ = My_Byte_Array [ee++];
return i;
}
I'm using a 25LC256 eeprom to store some floats on my project. These are read once at start-up so speed is not an issue. I'm using the 25LCxxx_SPI library (thanks Hendrik!) and it's working quite nicely. I have a method for stashing floats that seems to be working:
but cannot find a way to pull them back out as floats. Any help would be much appreciated...