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.

float to char conversion with CCS 5.5

Other Parts Discussed in Thread: MSP430G2553

I am a hardware man (former retired electronics engineer) who wants to extend his knowledge to embeded software. I am a newbie with C langage.

My LaunchPad equiped with a MSP430g2553 use a DS18B20 to measure temperature. This part of software works. I want to transmit via UART my temperature (which is a float)(celsius is a float) to my PC.

Here is my code:

	while(1)
	{
		if(GO) // Button pressed
		{
			temperature=GetData();
			celsius= temperature*.0625;
			sprintf(string,"%f degres\n\r",celsius);
			i=0;
			do
			{
			UCA0TXBUF = string[i++]; // send character i in Tx buffer
			while(!(IFG2 & UCA0TXIFG)); // Wait for TX buffer to be ready for new data
			}
			while(i<sizeof string-1);

		}
	}

sprint don't works and the result is only: " degres" on my PC !!!


Is there a method to convert a float into a string of characters ?

  • Dr.No said:
    sprint don't works and the result is only: " degres" on my PC !!!

    Embedded runtime libraries happen to have different versions, those "size optimized" does not support float printf/scanf formatters and maybe something else (I don't care - don't use). So you shall check your compiler Library Configuration Options, play with them. I know that IAR by default uses limited libraries and (s)printf formatter, no clue about CCS.

  • Thanks llmars for your answer.

    There is no problem to compile this code but the function doesn't work ?!

    I only have an information:

    Recommend moving them to RAM during run time or not using
         as these are processing/power intensive

    on the sprintf line

  • Dr.No said:
    There is no problem to compile this code but the function doesn't work ?!

    Sprintf function is precompiled already, resides in C run-time library. Thing is that compilers have different versions of libraries - as I already said. You have to find how to change them so you have one with sprintf that supports float. Look for project "generic" options, runtime libraries or something like that. Sorry, I have no experience with CCS, I can tell you how to do it in IAR.

    Something like: Properties->CCS General->Advanced Settings and verify that the Runtime support library field

    As illustration here's excerpt from SLAU132H:

    --printf_support={full| Enables support for smaller, limited versions of the printf function
    nofloat|minimal} family (sprintf, fprintf, etc.) and the scanf function family (sscanf,
    fscanf, etc.) run-time-support functions. The valid values are:
    • full: Supports all format specifiers. This is the default.
    • nofloat: Excludes support for printing and scanning floating-point
    values. Supports all format specifiers except %a, %A, %f, %F, %g,
    %G, %e, and %E.
    • minimal: Supports the printing and scanning of integer, char, or
    string values without width or precision flags. Specifically, only
    the %%, %d, %o, %c, %s, and %x format specifiers are supported
    There is no run-time error checking to detect if a format specifier is
    used for which support is not included. The --printf_support option
    precedes the --run_linker option, and must be used when performing
    the final link.

  • I would multiply the floating point number by 10000 (for 4 decimal places. i.e. 99.xxxx) and convert to long integer like so:

    #include <msp430g2553.h>
    #include <stdio.h>
    
    volatile float temperature;
    volatile long int temp;
    volatile  unsigned int hundreds, tens, units, tenths, hundredths, thousandths, tenthousandths;
    volatile long int remainder;
    
    
    unsigned char string[30];
    main ()
    {
    	
    
    
            temperature=25.0625;
            temperature *= 10000;//250625.
            temp = (long int)temperature;//250625
    
            tens = temp/100000;//2
            remainder = temp - tens*100000;//50625
            units = remainder/10000;//5
            remainder = remainder - units*10000;//625
            tenths = remainder/1000;//0
            remainder = remainder - tenths*1000;//625
            hundredths = remainder/100;//6
    
    
    
            sprintf(string, "Degrees =  %d%d.%d%d", tens, units, tenths, hundredths);
            while (1);
    
    
    }
    

    Continue along the same lines to get the remaining decimals. And if the temperature is greater than 99 start with a 100000 multiplier to get 3 digits.

  • @ llmars & Joseph Raslavsky

    Many thanks for your very detailed answers. I'll try to found the good library and I'll also try your solution Joseph.

    Happy New Year 2014 all my best greetings from France to you and yours

  • Just a small note...

    Enabling float type to be printable using sprintf function will increase your code size for quite an amount... so be careful with that. G2553 has small flash memory which sometimes not sufficient... (happened to me once)

    Also, happy new year to you too :)

  • Ilmars said:
    • full: Supports all format specifiers. This is the default.

    This line is misleading. Yes, it may be the linker default if no --printf_support option is used. However, the project default is "minimal", AFAIK.

    So the support level has to be changed in the project settings, from minimal to full.

    And indeed, it adds significant amount of code to the binary. (converting float numbers to ASCII is quite a complex task)

  • If you want build yourself library you can use these information as reference:

    1. Build an "Number" ASCII (0 - 9) array, include ".";

    2. According the IEEE 754 standard analysis the single precision floating point. 

    3. Suggest you use assembly language improve the efficiency. 

    4. If you want build the PC side software, I suggest you use the char array and UNION data structure.

  • Hi Joseph,


    I am working on similar code. I am working on F28069 processor (C2000 application) and doing coding using CCS

    I also want to convert float to char and I tried the logic which you posted but the sprintf function doesn't seem working.

    Here I attached the screenshot of CCS Debug window. Please suggest suitable solution for this problem asap

  • Viswa said:
    I am working on F28069 processor

    Then why you write in msp430 microcontroller forum? :)

  • Hi llmars...

    As I Saw relavent question, I posted my query I didnt checked the Forum... Sorry

  •      

    Viswa said:

    Hi Joseph,


     but the sprintf function doesn't seem working.

      You don't need sprintf. Just use an ascii table to convert to a character. Adding 0x30 does it for the digits 0 thru 9

            valueNew[0] = hundreds + 0x30;
            valueNew[1] = tens + 0x30;
            valueNew[2] = units + 0x30;
            valueNew[3] = tenths + 0x30;
            valueNew[4] = hundredths + 0x30;
            valueNew[5] = etc
            valueNew[6] = etc
            valueNew[7] = '\0'; //string terminator

**Attention** This is a public forum