This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Float and union Storing in EEPROM

Hi, i've tried to store a float variable into eeprom and i couldnt achieve it , when i printf it outputs NNDD ! strange thing , however it works fine with integers

 typedef union {
    unsigned char array[sizeof(float)];
    float gain;
} ahmed;
ahmed temp;

// to save value

           EDATA1=temp.array[0];          //put 78 in edata 0   //GN0L
           EDATA2=temp.array[1];          //put 51 in edata 1   //GN0M
           EDATA3=temp.array[2];          //put 2 in edata 2    //GN0H
       EDATA3=temp.array[3];


// to retrive value

            temp.array[0]=EDATA1;
           temp.array[1]=EDATA2;
           temp.array[2]=EDATA3;
           temp.array[3]=EDATA4;

// to print value

        printf("%1.0f",(float)temp.gain);                    //Output result to UART


.

  • What does it do if you just load the values into the array, and then print the float?

    ie, if you take the EEPROM out of the equation, does it work?

  • Take a closer look at your indices:

    EDATA1=temp.array[0]; //put 78 in edata 0   //GN0L
    EDATA2=temp.array[1]; //put 51 in edata 1   //GN0M
    EDATA3=temp.array[2]; //put 2 in edata 2    //GN0H
    EDATA3=temp.array[3];
    


    A float is four bytes large. You make two assigns to EDATA3. And why have you only commented 3/4 of the lines?

  • Sorry about the typo in the above code , by the way its worked when printing the float , but it doesnt save it into the eeprom , however it does save integers

     void Save_Gain(void)
    {                       //First byte of gain (GN0L).
               EADRH=0x00;    //set page address at 00h  high byte (03ff)
               EADRL=0x00;    //low byte
    
               EDATA1=temp.array[0];          //put 78 in edata 0   //GN0L
               EDATA2=temp.array[1];          //put 51 in edata 1   //GN0M
               EDATA3=temp.array[2];          //put 2 in edata 2    //GN0H
           EDATA4=temp.array[3];
    
               ECON=0x05;     //erase page
               ECON=0x02;     //write EDATA1-4 into pre-erased
               ECON=0x04;     //verify page
           HW_Delay(50);
    }
    void Get_Gain(void)
    {
           EADRH=0x00;        //set page address at 00h  high byte (03ff)
           EADRL=0x00;        //low byte
    
           ECON=0x01;      /*read current 4byte page of FlashEE
                                    //into EDATA1,2,3,4*/
               //HW_Dalay(20);
    //       GN0H=EDATA3;                     //put saved value in gain register H
               //GN0H=255;
               //HW_Dalay(20);
    //       GN0M=EDATA2;                     //put saved value in gain register M
             // GN0M=255;
               //HW_Dalay(20);
       temp.array[0]=EDATA1;                           //put saved value in gain register L
               temp.array[1]=EDATA2;
               temp.array[2]=EDATA3;
               temp.array[3]=EDATA4;
            // GN0L=255;
               //HW_Dalay(20);
    }