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.

CCS/LAUNCHXL-F28379D: Convert a Float to String.

Part Number: LAUNCHXL-F28379D


Tool/software: Code Composer Studio

Greetings,

I am new to working with embedded ti dev boards. I have been trying to write a few functions that will print strings or char* arrays to an LCD screen. I have working printChar and printString functions I have created. I can't seem to get any of the online examples I find that appear to work with gcc to work with the device.

These examples found below

xa_phase will work and makes a string of "45.00"

xb_phase will give the string ".0.00" and is not displaying on the screen.

Both of these functions appear to be breaking in some cases and adding a decimal at the first position of the char* array. This causes it to not print on the LCD screen. I have been trying to avoid sprintf and the like to avoid the space requirements necessary. 

I am trying to be able to take in floating point numbers that will be produced by the ADC and display them on the LCD for a Senior Design Project. I am just getting started and I am sure more questions will follow.

Thanks, for any help you are able to offer.

I have tried this example of gcvt code I found.

// defines for gcvt
#define PSH(X) (*(buf++)=(X))
#define PSH1(X) (*(buf--)=(X))
#define PEEK() buf[-1]
#define POP() *(--buf) = '\0'
#define PLUS 1
#define SPACE 2

// function to convert from float to string
char * gcvt(float f, uint16_t ndigit, char * buf)
{
 int i;
 uint32_t z,k;
 //int exp = 0;
 char *c = buf;
 float f2,t,scal;
 int   sign = 0;

 if((int)ndigit == -1)
   ndigit = 5;

 /* Unsigned long long only allows for 20 digits of precision
  * which is already more than double supports, so we limit the
  * digits to this.  long double might require an increase if it is ever
  * implemented.
  */
 if (ndigit > 20)
     ndigit = 20;

 if (f < 0.0) {
   sign = 1;
   f = -f;
    buf++;
 }

 scal = 1;
 for (i=ndigit; i>0; i--)
     scal *= 10;
 k = f + 0.1 / scal;
 f2 = f - k;
 if (!f) {
   PSH('0');
   if(ndigit > 0)
     PSH('.');
   for (i=0;i<ndigit;i++)
     PSH('0');
      PSH(0);
    return c;
 }

 i = 1;
 while (f >= 10.0) {
   f /= 10.0;
   i++;
 }

 buf += i + ndigit + 1;

 PSH1(0);

 if(ndigit > 0) {
     t = f2 * scal;
    z = t + 0.5;
   for (i = 0;i < ndigit;i++)
   {
     PSH1('0'+ (z % 10));
      z /= 10;
   }
   PSH1('.');
 }
 else
   PSH1(0);

 do {
   PSH1('0'+ (k % 10));
   k /= 10;
 }while (k);

 if (sign)
   PSH1('-');
 return c;
}



#define precision 2  //precision for decimal digits

// function to convert float to string
char* ftos (float num, char *buff)
{
    float f = num;
    char *str = buff;
    int a,b,c,k,l=0,m,i=0;

    // check for negative float
    if(f<0.0)
    {

        str[i++]='-';
        f*=-1;
    }

    a=f;    // extracting whole number
    f-=a;   // extracting decimal part
    k = precision;

    // number of digits in whole number
    while(k>-1)
    {
        l = pow(10,k);
        m = a/l;
        if(m>0)
        {
            break;
        }
    k--;
    }

    // number of digits in whole number are k+1

    /*
    extracting most significant digit i.e. right most digit , and concatenating to string
    obtained as quotient by dividing number by 10^k where k = (number of digit -1)
    */

    for(l=k+1;l>0;l--)
    {
        b = pow(10,l-1);
        c = a/b;
        str[i++]=c+48;
        a%=b;
    }
    str[i++] = '.';

    /* extracting decimal digits till precision */

    for(l=0;l<precision;l++)
    {
        f*=10.0;
        b = f;
        str[i++]=b+48;
        f-=b;
    }

    str[i]='\0';

    return str;
}


These are the numbers I am passing to the different functions
float xa_mag = 1004.0, xa_phase = 45.0, wa_mag = 2546.0,
                wa_phase = 55.0;
float xb_mag = 324.34, xb_phase = 15.2, wb_mag = 547.0,
                wb_phase = 10.0;
float xc_mag = 220.22, xc_phase = 30.0, wc_mag = 65.4,
                wc_phase = 60.0;
float xn_mag = 745.99, xn_phase = 20.0, wn_mag = 66.66,
                wn_phase = 180.0;

  • Unfortunately, it is impractical for us to debug code that is not supplied by TI.  Here are some suggestions to consider.

    On C28x, the type char is 16-bit wide.  Sometimes C code presumes char is exactly 8-bits wide, and problems occur when this presumption is violated.  Carefully inspect all of the char operations in this code with this in mind.

    Debug this code on a hosted system like a Windows laptop.  Change all of the char variables to 16-bit wide variables and see what happens.  One way to do that ... include the standard header file <stdint.h> and replace char with int16_t

    Thanks and regards,

    -George

  • I will try that and see if it works thanks.