This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129ENCPDT: Integer to float representation

Part Number: TM4C129ENCPDT

Hi, 

I am using TM4C129ENCPDT controller and the IDE is CCS Version: 7.4.0.00015, recently facing an issue with reading a 32 bit unsigned integer to a float variable. have tried type casting too, but unfortunately it is not working. 

for example, consider the value 30.00 as in integer representation.

SP_Temperature = 0x41f00000; // 32 bit unsigned integer >> 30.00

f_SP_Temperature = (float) SP_Temperature;   // f_SP_Temperature >> float, SP_Temperature >> unsigned 32 bit integer

The value of "f_SP_Temperature" is taking as decimal equivalent like "1.10624768e+09"

Please find attached screenshot of the same, any help is highly appreciated.

  • Hi Kurian,
    Can you please elaborate the problem? 0x41F00000 is equal to 110624768 in decimal. What do you expect the f_SP_Temperature to be?
  • I think you are trying to do something like this:

    float main(void)
    {
    	uint32_t SP_Temperature;
    	float f_SP_Temperature;
    
    	SP_Temperature = 30; // 32 bit unsigned integer >> 30.00
    	f_SP_Temperature = (float) SP_Temperature;   // f_SP_Temperature >> float, SP_Temperature >> unsigned 32 bit integer
    	return f_SP_Temperature;
    }
    

    Then if you look at  the variables, you can see the hex representation of a floating point 30.0 is 0x41F00000.

  • Thanks Bob. The poster just needs to right click on the expression window and under the Number Format, select the hex.
  • On the other hand, if you get a return integer value from a function that reads a peripheral, but you need to interpret that value as a float and avoid the conversion, you can write the code like this. You basically cast the address of the integer as a float pointer. While the C code looks messy, the compiler understands and generates code that simply copies the value directly from one location to the next.

    float main(void)
    {
    	uint32_t SP_Temperature;
    	float f_SP_Temperature;
    
    	SP_Temperature = 0x41F00000; // 32 bit unsigned integer >> 30.00
    	f_SP_Temperature = *(float *) &SP_Temperature;   // f_SP_Temperature >> float, SP_Temperature >> unsigned 32 bit integer
    	return f_SP_Temperature;
    }
    

  • Sorry, I just could not resist one more comment. Perhaps a more elegant method would be the use of a union. Basically telling the compiler I want to reference a single location two different ways. It would look like this:

    float main(void)
    {
    	union
    	{
    		float 	  f;
    		uint32_t  i;
    	}SP_Temperature;
    
    	SP_Temperature.i = 0x41F00000; // 32 bit unsigned integer >> 30.00
    	return SP_Temperature.f;
    }
    

  • Thanks a lot dear  Bob and Charles.

    "f_SP_Temperature = *(float *) &SP_Temperature;" resolves the issue. looks like telling compiler an address location and introducing as type float. It was wonderful when exploring something new.... thank you again. 

  • Can something like this be done with ADC / uDMA transfers? Skip integer to float conversion?

  • Savo Pejovic said:

    Can something like this be done with ADC / uDMA transfers? Skip integer to float conversion?

    No, those are different representations, not a different view of the same representations.

    Robert