float v;
char txt[15];
float t[20] absolute 0x1A0; // bank3
float max;
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);
}
void voltage_READ(void)
{
int i;
ADCON0 = 0x81; // ADON = 1, Channel A.0 select, Fosc/32 select
ADCON1 = 0x8E; // 0x8E,0xC0 port a as analog, right justified output
ADCON0.ADON=1;
for(i=0; i<=19; i++)
{
v= ADC_Read(0);
v =v*(10.0/1023.0);
v=(v-5.0);
IRP_bit=1;
t[i]=v*110.1909091;
IRP_bit=0;
}
ADCON0.ADON=0;
max=t[0];
for(i=0; i<=19; i++)
{
if(max<t[i])
max=t[i];
}
max=max*12,9612673;
// FloatToStr(max, txt);
i=Float2Ascii (max,txt,3); // 3 decimales
Lcd_Out(1,9,txt);
Delay_ms(200);
}
void current_READ(void)
{
int i,current;
ADCON0.ADON=1;
for(i=0; i<=19; i++)
{
v= ADC_Read(0); // <- i only have RA0 as analog !!
v =v*(10.0/1023.0);
v=(v-5.0);
IRP_bit=1;
t[i]=v*10;
IRP_bit=0;
}
ADCON0.ADON=0;
max=t[0];
for(i=0; i<=19; i++)
{
if(max<t[i])
max=t[i];
}
max=max*.707106781;
i=Float2Ascii (max,txt,3); // 3 decimales
Lcd_Out(2,10,txt);
// FloatToStr(max, txt);
// Lcd_Out(2,8,txt);
Delay_ms(1000);
}
//==============================
void main()
{
Init_Hardware();
ADC_Init();
ADCON0 = 0x81; // ADON = 1, Channel A.0 select, Fosc/32 select
ADCON1 = 0x8E; // 0x8E,0xC0 port a as analog, right justified output
LCD_PWR=1; // RD5 =LCD_PWR voir schema 103-00032 en C2
Lcd_Init();
Delay_ms(2000);
ADCON0.ADCS1=1;
ADCON0.ADCS1=0;
ADCON0.ADON=0;
while(1)
{
Lcd_Out(1,1, "voltage:");
Lcd_Out(2,1, "Current:");
voltage_READ();
current_READ();
}
}