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;
}