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

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

Reply Children
No Data
Related