Hi Guys,
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:
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:
Help is appreciated
Cheers
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++;
}
Help is appreciated
Cheers