unsigned char CRam1[32];
float F1;
unsigned int i,j,k;
unsigned char *fltToa (float x, unsigned char *str,char precision)
{
/* converts a floating point number to an ascii string */
/* x is stored into str, which should be at least 30 chars long */
unsigned char *adpt;
int ie, i, k, ndig;
double y;
adpt=str;
ndig = ( precision<=0) ? 7 : (precision > 22 ? 23 : precision+1);
ie = 0;
/* if x negative, write minus and reverse */
if ( x < 0)
{
*str++ = '-';
x = -x;
}
/* put x in range 1 <= x < 10 */
if (x > 0.0) while (x < 1.0)
{
x *= 10.0; // a la place de =*
ie--;
}
while (x >= 10.0)
{
x = x/10.0;
ie++;
}
// in f format, number of digits is related to size
ndig += ie; // a la place de =+
//round. x is between 1 and 10 and ndig will be printed to
// right of decimal point so rounding is ...
for (y = i = 1; i < ndig; i++)
y = y/10.;
x += y/2.;
if (x >= 10.0) {x = 1.0; ie++;}
if (ie<0)
{
*str++ = '0'; *str++ = '.';
if (ndig < 0) ie = ie-ndig;
for (i = -1; i > ie; i--) *str++ = '0';
}
for (i=0; i < ndig; i++)
{
k = x;
*str++ = k + '0';
if (i == ie ) *str++ = '.';
x -= (y=k);
x *= 10.0;
}
*str = '\0';
return (adpt);
}
void aligne8()
{
j=strlen(CRam1);
while ( j<8)
{
k=fprintf(_H_USART,"x"); // replace "x" by " "
j++;
}
k=fprintf(_H_USART,"%s%c",CRam1,9);
}
// in main program .........................
// test flottant to ascii
F1=34.5789;
//k=fprintf(_H_USART,"F1=34.5789 -> %s\r\n",fltToa(F1,CRam1,4));
k=fprintf(_H_USART,"F1=34.5789 -> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=0.3244;
k=fprintf(_H_USART,"F1=0.3244 -> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=1.9876;
k=fprintf(_H_USART,"F1=1.9876 -> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=22.5431;
k=fprintf(_H_USART,"F1=22.5431 -> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=352.1234;
k=fprintf(_H_USART,"F1=352.1234-> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=0.0;
k=fprintf(_H_USART,"F1=0.0-> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=65537.9872;
k=fprintf(_H_USART,"F1=65537.9872-> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=9998887654321.1234;
k=fprintf(_H_USART,"F1=9998887654321.1234-> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
F1=-128.4567;
k=fprintf(_H_USART,"F1=-128.4567-> %s%c",fltToa(F1,CRam1,4),9);
aligne8();
Energydisplay(F1);
Put_RS(TAB);
FloatToLCD(F1);
CRLF();
Tempo(100000L) ;
do
{}
while(1);
......