char CRam1[32]; // for float conversion
char CRam0[6]; // for int conversion
float F1;
unsigned int M1; // raw ADC0 measure
void ADC_Init(void);
unsigned int ADC_Read(unsigned char channel) ;
void Volt_Display(float Fx,int l,int c);
unsigned char *Float2Ascii (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);
}
// LCD 4x20
void Volt_Display(float Fx,int l,int c)
{
int i,j,k;
CRam1[0]=0;
Float2Ascii(Fx,CRam1,l) ;
/*
if (l==1) Lcd_Cmd(0x80+c);
if (l==2) Lcd_Cmd(0xC0+c);
if (l==3) Lcd_Cmd(0x94+c);
if (l==4) Lcd_Cmd(0xD4+c);
*/
j=strlen(CRam1);
// cadrage fin du resultat à la 14em position du LCD
k=14-(j+c); // commence donc à la position
for (i=0;i<c;i++) Lcd_Cmd( _LCD_MOVE_CURSOR_RIGHT );
for (i=0;i<k;i++)
{
Lcd_Chr_CP(' '); // remplissage par des blancs
//Lcd_cmd(_LCD_MOVE_CURSOR_RIGHT);//ou deplacement du cursor
}
// terminaison de la ligne
Lcd_Out_CP(CRam1); // floating value itself
Lcd_Chr_CP(' ');
Lcd_Chr_CP('V');
}
void main()
{
... etc .....
ADCON0 = 0x81;
M1= ADC_Read(0); // result is the average on 32 meaures
F1=(float)M1*0.0048828125; // coeff= 5.00V/1024
Lcd_Cmd(_LCD_SECOND_ROW );
WordToStr(M1,CRam0); // attention le buffer ne doit pas etre trop long !! sinon deborde du LCD...
Lcd_Out_CP(CRam0);
Lcd_Cmd(_LCD_SECOND_ROW ); // affichage cadre à droite , avec 'V' en 16em position
Volt_Display(F1,3,8); // 3 digits apres la virgule en 8em position
..........