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.

Converting float numbers to string



Hi all,

I need help. I need to send a float number via UART but i need a way to convert this number into a string to send character by character. I am using the MSP430F1611 and IAR. I tried to program some codes but I had no success.

I tried the first time to separate directly the integer part and the fractional part  but had problems with numbers like 54.0034 because the output of the fractional part was allways 34 and not 0034.

Also I tried to use the function sprintf() but didn't work. I found in the documentation that this function does not support float numbers for the MSP430.

I need to solve this problem so i made the code below....I think it is easy to understand. I want to separate the float number in different digits like this: 345.678 ==> "3" "4" "5" "." "6" "7" "8" and send the diferent numbers via UART. I programed this code with DevC software and it worked perfectly, quite the opposite with IAR. It doesn't work with IAR. I know that this code has some restrictions like that i can use only three integers and three decimals but i don't get any other idea about how to do this. It should be a simple problem but i think that it is defeating me...

Anybody can help me?, maybe telling me what is wrong with my code, or with other ideas, or with any other code to send a float via UART with a MSP430...

 

static void SendValue(float data)    
{
  
    static int number;
    static char digits[7]={0,0,0,0,0,0,0};
    static int i=0;
   
   
    number = abs(data*1000);
    
    digits[6]= (number % 10) + 48; //add 48 for the corresponding ASCII number
      number = number/10;
    digits[5]= numero % 10 + 48;
     number = number/10;
    digits[4]= nnumber % 10 + 48;
      number = number/10;
      digits[3]= 46;  //"."
    digits[2]= number % 10 + 48;
     number = number/10;
    digits[1]= number % 10 + 48;
      number = number/10;
    digits[0]= number % 10 + 48;
      //number = number/10;
   
    if (data <0) 
    { TXBUF0 = 45; //"-"
        while (!(IFG1 & UTXIFG0));
    }
   
  for(i=0; i<7 ; i++)
  {
     TXBUF0 = (digits[i]);
      while (!(IFG1 & UTXIFG0));
  }

}

I hope you can help me.Thank you very much for your help in advance.

Greetings

Alejos

  • Hi Alejos,

    what is this 'abs' in your typecast? The typecast (float to integer) should look like:

    number = (int) (data*1000);

    Rgds
    aBUGSworstnightmare 

  • Hi aBUGSworstnightmare,

    you are right, it was my mistake. Anyway, the code didn't work also with this correction.

    Finally I came back to my first code to solve this problem and I got something feasible to make the conversion from float to string.

     

    static void SendValue (float data)   
    {
      
        static int pint;
        static int pfract;
        static char string[7];
        int len,i,sign;

        sign=(int)data;
        pint =(int)data;
        pfract= (int)((data - pint)*1000);
        pint = abs(pint);
        pfract = abs(pfract);
       
        sprintf(string,"%03d.%03d",pint,pfract);

        len=strlen(string);
        if (sign < 0)
        {
          TXBUF0= 45; //"-"
            while (!(IFG1 & UTXIFG0));
        } 
        for(i=0 ; i<len ; i++)
        {
            TXBUF0= string[i];
            while (!(IFG1 & UTXIFG0));
        }
        _NOP();
    }

     

    Easier and smaller...This code works showing 3 digits for the integer part and 3 digits for the fractional part...

    Thanks for your help...

     

    Regards

    Alejos

  • Hi,

       Alejos, simply using sprintf i think the problem can solved

    sprintf(buffer,"%6.3f",floatdata);

    for(i=0;i<len;i++)

    {

    TXBUF0=buffer[i];

    while(!(IFG1&UTXIFG0));

    }

     

    Am i right??

    Regards

    Kshatriya

     

  • Hey your code works fantastic

    problem is it takes to much of code size and much time in execution

    is there anyway we can do float to string much quicker and less code size

    Regards

    Bharat.u

  • Bharat Uddagiri said:
    it takes to much of code size and much time in execution

    If that is a concern for you, then why are you even using floating-point at all??

    Floating point will inherently give much larger code size and much greater execution time than integer arithmetic!!

  • Hi,

    just idea.

    If we can floating point convert to integer- fixed point by multiplying 100 or 1000 or else, we can use a very old connversion method binary to binary coded decimal (BCD).

    We have to shift our integer into result register MSB first bit by bit and check each four bits are them less than 5, if not - perform BCD correction by adding 3 to this four bits.

    I think it is a most powerfull method to get BCD- it is less expensive than dividing several times by 10 and getting reminder. It is easer to solve it in Assambler.

    Please find an attachment for illustration.

      

    At the end we can seperate each BCD sign and add '0' to get a literal.

    Regards,

     

    Juris

     

     

     

**Attention** This is a public forum