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.

Problem with float in F28027

Hello.

I'm using a TM320F28027 controlstick.
I have a problem with a float calculation on this device.

I have a variable float32 AVG_PWMControl_buf

I add to this buffer the result of ADC conversion in adc_isr:
AVG_PWMControl_buf += AdcResult.ADCRESULT4;

After first result is added to buffer I get the results in watch window as followed:
AdcResult.ADCRESULT4 = 3482
AVG_PWMControl_buf = 4.87932125e-42 (DEN) - strange result, especially "e-42"
Measured_Times = 1

After the second measurement:
AdcResult.ADCRESULT4 = 3536
AVG_PWMControl_buf = 9.83431262e-42 (DEN) - strange value
Measured_Times = 2

After 11 interrupts I get results as followed:
AdcResult.ADCRESULT4 = 3517
AVG_PWMControl_buf = 5.41685934e-41 (DEN)
Measured_Times = 11

And I want calculate the variable unsigned int AVG_PWMControl as:
AVG_PWMControl = AVG_PWMControl_buf / Measured_Times;

And I got the result AVG_PWMControl = 63093, which is incorrect.

What is the reason of wrong calculation of float in my project?

P.S.:

1. Code Composer Studio Version: 6.2.0.00050
2. Compiler TI v15.12.3.LTS

I've also detected, that if I decrease an amplitude of analog signal for ADC to value lower than 3000 - calculation goes correctly.

E.g. for AdcResult.ADCRESULT4 = ~2942
After 11 measurements I got:
AVG_PWMControl_buf = 4.54216884e-41 (DEN) - the value is still strange
Measured_Times = 11
AVG_PWMControl = 2946

I suspect, that there is some problem with overflow.
But how an overflow can happen with a variable of float type?

UPDATE:

If I look into memory, I can see, that variable is located only in two bytes, but type float32 is declarated.

  • I suspect this is a display artifact with CCS. When you load up the variable AVG_PWMControl_buf to the watch window you can set the display options to "single precision float"

    You can also try to explicitly cast the ADC value to float before accumulating it. Also did you set the initial variable value to 0.0F?
  • Hi, Vishal.

    I believe, that this is not just a display artifact of CCS. Because the value of AVG_PWMControl is depended of AVG_PWMControl_buf,

    and it is calculated with an error.

    But anyway, thank you for recommendations. I've tried to check them.

    1. I don't have an option "single precision float" in watch window:

    Even while adding a variable to watch window:

    2. Declaration of variable as float32 AVG_PWMControl_buf = 0.0F; also didn't give any changes.

  • Hi VP,

    Just one suggestion, as ADCRESULTx values are integers then why float as the resultant? Use 'Uint32'. One thing I've observed and is very consistent is: float32 doesn't play well with integers - I've previously got similar erroneous values.

    Regards,
    Gautam
  • Hmm, the reason i thought it was a CCS artifact is that the numbers show, for example n.nnnn..e-41 are not valid single precison floats. The normal range of single precision float is +-n.nnnne-38 to n.nnnne+38 (i forget the exact range but the numbers in your post are clearly not representable in IEEE 754). What you can do is open up the "Memory Browser" and navigate to the address of the variable (0x881C from the image in your post) - the memory browser allows you to display the contents of the memory in single precision float - and step through each accumulation to see if the variable has the right value.

    Also maybe you can post the disassembly for that particular accumulation. I don't think it is incorrect as it works for values < 3000 but its worth checking out.

  • Hello, guys!

    I found the reason of my problem.

    To be honest, the mistake was made too silly.

    The reason of such behaviour was in variable declaration.

    float32 AVG_PWMControl_buf was declarated in a file main.c first.

    Then I had to declare it in other file, where calculations were performed - adc.c. This variable was declarated as

    extern AVG_PWMControl_buf in file adc.c.

    And by default, without specifying the type, int type was assigned to variable AVG_PWMControl_buf.

    After changing declaration to extern float32 AVG_PWMControl_buf in adc.c programm works correctly.