I have float to ASCII function but sometime it displays the output on GLCD after converting float into ASCII and i can print on GLCD but sometime it stops. One of friend on forum suggested to use buffer instead of pointer.
The code for float to ASCII is as below:
Code:
void ftoa(float value, char *string)
{
if (value < 0) {
*string++ = '-';
value = -value;
}
sprintf(string, (const far rom char *) "%lu.%02u",
(long) value,
(int) ((value - (long) value) * 100. + 0.5));
}
I am receiving value via EUSART and storing in 8 byte long buffer and converting into float and it is working fine using the below code:
Code:
unsigned char buf[9]; //array to hold 9 bytes receive whole string via EUSART
unsigned char buffer[7];//array to hold 7 bytes , print the string of interest
static float floatval;//
char *str; /* Pointer */
// receive whole string
for (i=0; i < 9; i++)
{
while (PIR1bits.RCIF == 0) //wait until there is a character ready
; //do nothing
buf[i] = RCREG; //copy received char into array at current index position
}
// print the whole string on GLCD (8 byte long)
for (i=1; i < 9; i++) // Print on the GLCD
{
PutChar(buf[i]);
};
// print the string without any alphanumeric character (7 byte long)
for (i=1;i<8;i++)
{
buffer[i] = buf [i];
}
floatval = atof(buf); // ASCII to float conversion
ftoa(floatval,str); // Float to ASCII conversion
while ( *str ) { // Put String on GLCD
PutChar(*str);
str++;
}
Once I receive the data I want to convert into other unit. I am receiving the data which is in terms of tonnes (8 character long string).
e.g. 0001.87t
I have to convert into kilonewton(kN). To do so I have to convert the received string into float.
Code:
floatval = atof(buf); // ASCII to float conversion
kN = floatval* 9.806 ; // convert to kN , I forgot to include in first post
ftoa(floatval,str); // Float to ASCII conversion
after this conversion I have to print on the GLCD so again I can't display the float directly therefore need to convert back to string using ftoa.
You mean the GLCD doesn't work first time??? I'm having that issue as well at the moment... Its a T6963C but I have to reset more than three times before it runs...
I not sure which bit of code you are referencing... is it the code I quoted in post #3... The use of the cast to const rom char* just wont help you atall.
If you increment your pointer like this there is no way to come back to the start of the string. Use proper indexing like I showed you in the other thread.
Bad method:
Code:
char str[20] /* memory for 20 characters */
ftoa(floatval,str); // Float to ASCII conversion
while ( *str ) { // Put String on GLCD
PutChar(*str);
str++;
}
Good method:
Code:
char str[20] /* memory for 20 characters */
ftoa(floatval,str); // Float to ASCII conversion
int i=0;
while ( str[i] ) // Put String on GLCD
{
PutChar(str[i]);
i++;
}