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.

Compiler/LAUNCHXL-F28069M: Problem with conversion from Uint16 to float

Part Number: LAUNCHXL-F28069M

Tool/software: TI C/C++ Compiler

**************************************************************************************

Uint16 conv[6];
phase_1_gain=3.3f;
phase_2_gain=3.3f;
phase_3_gain=3.3f;


while(1)
{
AdcConversion_run(conv);
phase1_current=(((float)(conv[0]-conv[3]))/4096.0f)*phase_1_gain; 
phase3_current=(((float)(conv[1]-conv[4]))/4096.0f)*phase_3_gain; 
phase2_current=(((float)(conv[2]-conv[5]))/4096.0f)*phase_2_gain; 
}

************************************************************************************************+

The code above with conv={1500,1500,1500,0,0,0} debugging (in Variables or Expressions) gave phase1_current=0.0, phase2_current=0.0 and phase3_current=0.0

Seems that (float)(conv[0]-conv[3]) is equal to 0.0 and must be (float)(1500-0)/4096.0f*3.3f

Please, consult included libraries. Any missing or error in debug?

 

 

  • Dear All

         Do you need a sample project to verify?

    Let me know and I will provide a link in a private message

  • I have a clue. Perhaps, an Uint16 can not be subtracted from Uint16 because can not take a negative value (?????)
  • The result in Uint16 can not be negative and the computed result can be negative
  • We need a test case we can build.  It does not have to run.  I am confident inspecting the compiler generated assembly code will be good enough to work out what happened.  For the function which contains this code ...

    user4957458 said:
    while(1)
    {
    AdcConversion_run(conv);
    phase1_current=(((float)(conv[0]-conv[3]))/4096.0f)*phase_1_gain; 
    phase3_current=(((float)(conv[1]-conv[4]))/4096.0f)*phase_3_gain; 
    phase2_current=(((float)(conv[2]-conv[5]))/4096.0f)*phase_2_gain; 
    }

    ... please follow the directions in the article How to Submit a Compiler Test Case.

    Thanks and regards,

    -George

  • I cannot reproduce the problem with C2000 16.9.6.LTS with the options you provided; as far as I can tell, the code is correct.  I've enclosed a cut-down test case demonstrating that the compiler does the right thing.  You say that you have observed "(float)(conv[0]-conv[3]) is equal to 0.0".  How were you able to do this?  Were you using a printf statement or the CCS watch window?

    /*
      -v28
      -ml
      -mt
      --cla_support=cla0
      --float_support=fpu32
      --tmu_support=tmu0
      --vcu_support=vcu0
      -Ooff
      --opt_for_speed=5
      --fp_mode=relaxed
      --advice:performance=all
      -g
      --c99
      --diag_warning=225
      --diag_wrap=off
      --display_error_number
     */
    
    #include <stdio.h>
    #include <stdint.h>
    
    float phase_1_gain=3.3f;
    float phase_2_gain=3.3f;
    float phase_3_gain=3.3f;
    
    void try_signed()
    {
        int16_t conv[6] = { 1500, 1500, 1500, 0, 0, 0 };
    
        float phase1_current=(((float)(conv[0]-conv[3]))/4096.0f)*phase_1_gain;
        float phase3_current=(((float)(conv[1]-conv[4]))/4096.0f)*phase_3_gain;
        float phase2_current=(((float)(conv[2]-conv[5]))/4096.0f)*phase_2_gain;
    
        printf("p1c: %f\n", phase1_current);
        printf("p2c: %f\n", phase2_current);
        printf("p3c: %f\n", phase3_current);
    }
    
    void try_unsigned()
    {
        uint16_t conv[6] = { 1500, 1500, 1500, 0, 0, 0 };
    
        float phase1_current=(((float)(conv[0]-conv[3]))/4096.0f)*phase_1_gain;
        float phase3_current=(((float)(conv[1]-conv[4]))/4096.0f)*phase_3_gain;
        float phase2_current=(((float)(conv[2]-conv[5]))/4096.0f)*phase_2_gain;
    
        printf("p1c: %f\n", phase1_current);
        printf("p2c: %f\n", phase2_current);
        printf("p3c: %f\n", phase3_current);
    }
    
    void main()
    {
        try_signed();
        try_unsigned();
    }
    
  • Yes, the variable was seen in debug mode in "Expressions". How I do a "printf" without display? Is it also in debug mode?

    Thanks

    Luis Gonçalves
  • If you're debugging the system under CCS, and you can set breakpoints on the device, and you have a little bit of heap available, you should be able to use the stop-mode printf support on the device. Please see processors.wiki.ti.com/.../Tips_for_using_printf

    As to the expression window, it may be the case that the debugging information got corrupted and caused CCS to display an incorrect value for the contents of the variable. We can explore that if you like. Set a breakpoint in the while loop after one of the statements and look at the value of the expression &phase1_current. This will be an address in memory. Open a memory browser window to that address and tell me the next 32 bits in memory.
  • More, if "conv[0]-conv[3]" is negative "phase1_current=-Inf". If positive then "phase1_current=0".

    And by some time, The CCS do not let me to make breakpoints. You can confirm with the project I provided

    "We can explore that if you like", ------ Please, do!!!! With the project I sent, please.

    I had also problems in conversions from float to integer that I solved with FPU intrinsic instructions of the compiler. I confirmed that it was working with blinking leds of the board. It ran also in debug mode but with the program running at free will. But that code is not included in the provided project
  • In mode Release works. I tested with a "if ... then" with the leds

  • Hello

    Have you resolved your issue?
    If not, can you state what your current issue or question is?

    Best regards
    Chris
  • For now, the Release version seems working. Later, I added more Heap and Stack. Seems that I can do now breakpoints perhaps due to more heap/stack. I did not experiment yet the "printf".
  • Alright great!
    I'm going to close this, if you come across another issue or question, start a new post.

    Best regards
    Chris