4 bytes to a float

25 May 2013

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:

float test=1.2348;
eeprom.write(16,sizeof(test),(char*)&test);    //16 being the addr

but cannot find a way to pull them back out as floats. Any help would be much appreciated...

25 May 2013

What I think should work is something like:

float value;
char *readchar = eeprom.read(16, sizeof(value));
value = *(float*)readchar;
free (readchar);

First read the data again, the read function returns a pointer to a char array. Then in a reverse way of the function you had, we change the pointer type from char to float, and we read the value of the float it points too. Finally we have to free readchar since it was explicitely allocated by the eeprom read function, and we don't need it to keep occupying memory. (For this not a big deal, but if you read in a loop you otherwise would be creating a memory leak).

25 May 2013

Works a treat! Thanks Erik. I got close on this one. On to the next task...

25 May 2013

Another solution is to make a struct with all your data and write/read it all once:

struct 
{
  float f1;
  float f2;
} g_settings;

eeprom.write(16, sizeof(g_settings), (char*)&g_settings);
...
char *readbuf = eeprom.read(16, sizeof(g_settings));
memcpy(&g_settings, readbuf, sizeof(g_settings));
free (readbuf);

I would also modify the library so that it could read into a passed buffer; this way you can get rid of the unnecessary malloc/memcpy/free calls.

26 May 2013

I like your idea Igor. I found it easy to load my float variables but again having difficulty pulling them back out. My code:

    //g_settings.alphaF=91.369;                  // can set start-up values 
    //g_settings.alphaR=91.369;
    //g_settings.betaF=-0.8446;
    //g_settings.betaR=-0.7996;
    

    eeprom.write(16, sizeof(g_settings), (char*)&g_settings);             // writes all 4 bytes


    char *readbuf = eeprom.read(16, sizeof(g_settings));
    memcpy(&g_settings, readbuf, sizeof(g_settings));
    free (readbuf);
    
    pc.printf("alphaF = %f \n",g_settings.alphaF);                             // always outputs 0.0

what am I doing wrong (and tnx) ?

26 May 2013

Well, the code looks correct to me, so I'm puzzled where is the problem... maybe writing did not actually succeed for some reason?

26 May 2013

my mistake here. didnt comment out the eeprom.write when I went back to pull values from the eeprom. works perfectly. tnx again..

gb

08 Jul 2013

I have been messing with this too,

and have come up with:

/*

    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;
}

there are 2 blocks, (of two)

write/read float, write/read structure/array

Cheers

Ceri