This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Reading a 32-bit floating point value from register

Hello,

I am using SEGGER Embedded Studio together with a nRF52832 and I am trying to read an EEprom register containing a 32-bit floating point value in four bytes (register address 0x10, 0x11, 0x12, 0x13).

I have seen examples of someone using a union function for this, but I can't figure out how to do it.

I can read each byte separately and combine them into a 32-bit integer value (uint32_t), but i don't think this helps me.

Any suggestions are very much appreciated.

Thanks.

Parents
  • There are a few things to consider with this.

    Firstly whether the data in EEPROM register is a floating point value that is in the same format as the compiler you are using expects.

    If you wrote the EEPROM using the nRF52 it will be in the correct format, but if the value was written by a different processor, potentially running code generated by a different compiler etc the way the number is stored could be completely different.

    Assuming its the same format, then yes you can use a union to do this, e.g.

    union
    {
    float floatVal;
    uint8_t bytes[4];
    } floatConverter;
    
    
    	floatConverter.bytes[0]=0x10;
    	floatConverter.bytes[1]=0x11;
    	floatConverter.bytes[2]=0x12;
    	floatConverter.bytes[3]=0x13;
    
    float eepromFloat = floatConverter.floatVal;
    

    You could also just cast the byte array as a pointer to float and get its contents.

    Note. Byte order may be not 0,1,2,3 it could be 3,2,1,0 or even 2,3,0,1 etc

    It really depends on what stored the float in the EEPROM in the first place

Reply
  • There are a few things to consider with this.

    Firstly whether the data in EEPROM register is a floating point value that is in the same format as the compiler you are using expects.

    If you wrote the EEPROM using the nRF52 it will be in the correct format, but if the value was written by a different processor, potentially running code generated by a different compiler etc the way the number is stored could be completely different.

    Assuming its the same format, then yes you can use a union to do this, e.g.

    union
    {
    float floatVal;
    uint8_t bytes[4];
    } floatConverter;
    
    
    	floatConverter.bytes[0]=0x10;
    	floatConverter.bytes[1]=0x11;
    	floatConverter.bytes[2]=0x12;
    	floatConverter.bytes[3]=0x13;
    
    float eepromFloat = floatConverter.floatVal;
    

    You could also just cast the byte array as a pointer to float and get its contents.

    Note. Byte order may be not 0,1,2,3 it could be 3,2,1,0 or even 2,3,0,1 etc

    It really depends on what stored the float in the EEPROM in the first place

Children
  • Hi Roger, Thank you very much for your reply. I have added the code and it compiles and runs without any errors. The next issue I see is that I am not able to print a float value using RTT viewer (JLinkRTTViewer). It works fine when printing integer values. This is the code:

    char float_str[80]; sprintf(float_str, "The 32-bit float value is %f\n", eepromFloat);
    SEGGER_RTT_WriteString(0, float_str); I have tried with '%d', '%0.1f' etc. but it makes no difference.

    Do you know if it is possible to print float values using RTT, so that it is possible to debug the code?

    Thanks.

  • Just one more question to make sure that I understand the union code. I guess it is the 8-bit value stored in each register that you are referring to in the above example and not the address of the register. Is that correctly understood? So in my case it would loook something like this:

    uint8_t floatArray[4]; floatArray[0] = registervalue_0; // 8 bit value read from register address 0x10 floatArray[1] = registervalue_1; // 8 bit value read from register address 0x11 floatArray[2] = registervalue_2; // 8 bit value read from register address 0x12 floatArray[3] = registervalue_3; // 8 bit value read from register address 0x13

    Where registerValue_0 - registervalue_3 is the value of the 8 bits stored in each of the four registers. I have used the address to retrieve the value from the register.

    So the union code would look like this:

    floatConverter.bytes[0]=floatArray[0]; floatConverter.bytes[1]=floatArray[1]; floatConverter.bytes[2]=floatArray[2]; floatConverter.bytes[3]=floatArray[3];

    Or simply just assigning the register values directly.

    Or am I getting this wrong?

  • I presume you have code that is reading the data into floatArray[], so you could read directly into floatConverter.bytes

    Alternatively, you could achieve the same results by casting pointers

  • Thanks for your help, it is appreciated. I am not quite sure how to cast pointers, since my experience with pointers is limited. I will be able to see if the union code works once I am able to print float values using RTT. I have posted a separate question on this topic.

Related