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.

MSP430G2955 converts string to float



Hey, Guys:

I'm a newer of MSP430G2 series.
Now, I'm try to convert string to float for value check.
Because system can accept user to input the value by keypad and show on LCD(16X2 char type).

For example. unsigned char Str[]={'1','.','5','3'};, and after convert it should be 1.53.
But in CCS debug mode, it becomes 1.00, not 1.53.

Does anyone can give me some suggestion or any hint? 
I'm really confuse.....It can work on Dev C, but work wrong in CCS.....

Please, if you have any suggestion, please help me to clear this probe.

Much thanks.

The test code list at below:

#include <msp430.h>
#include <math.h>

float string2float(unsigned char *Str);
float FinalValue = 0;
float FinalValueArray[4];

/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
unsigned char Str[]={'1','.','5','3'};
unsigned char i;

for (i=0;i<=2;i++)
{
FinalValue = string2float(Str);
FinalValueArray[i] =(double) string2float(Str);
}


return 0;
}


float string2float(unsigned char *Str)
{
unsigned char i,IntCount,TotalStringNumber;

float DotCount = 0; //DotCount is for integer and float gain count (Note: Its different integer and float calculate.)

float floatValue = 0;
float floatValueTemp = 0;

DotCount = 0; // clear DotCount

TotalStringNumber = sizeof(Str); // calculate the Valudfe string count
DotCount = 0;

/* count dot address */
for(IntCount = 0; IntCount < TotalStringNumber; IntCount++)
{
if(Str[IntCount] == '.') break;
DotCount ++;
}

IntCount = DotCount-1;

/* Translate char before DOT (int) */
for(i = 0; i < DotCount ; i++){
floatValueTemp += Str[i] - '0'; //ASCII code transfer '0' = 0x30
floatValueTemp = floatValueTemp * pow(10,IntCount);
floatValue += floatValueTemp;
floatValueTemp = 0;
IntCount -= 1;
}
/* Translate char after DOT (float) */
floatValueTemp = 0;
for(i = (DotCount+1);i < TotalStringNumber; i++ ){
floatValueTemp += Str[i] - '0'; //ASCII code transfer '0' = 0x30
floatValueTemp = floatValueTemp / pow(10,i-DotCount);
floatValue += floatValueTemp;
floatValueTemp = 0;

}
return floatValue;
}

  • I think the problem might be connected to:

    TotalStringNumber = sizeof(Str); // calculate the Valudfe string count
    

    The "sizeof" construct returns the size of the object it is applied to. It does not return the length of the string!

    In this case it will return "2", as that is size of a pointer (unless you use the large data model, in which case it will return 4). Try to use "strlen()" instead.

        -- Anders Lindgren, IAR Systems

  • Anders Lindgren:

    Thanks for your kindly help. Yes, I found the problem, and I also found strlen() has some different in CCS.
    The strlen() will calculates the array elements, and return. But this function also calculate the 'NULL'.
    So, if I use it to count the array element length which need to decrease 1.

    Now, I solved the problem. Thank you, without your suggestion, maybe I will keep in this trap.

    Much thanks.

    Simon. Lee

**Attention** This is a public forum