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

  • 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.

Reply
  • 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.

Children
No Data
Related