I'm using microCpro for pic.
here is my program:
unsigned int adc_rd0,adc_rd1,adc_rd2;
unsigned char ch,ch1; //
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
//char * text;
char txt2[3];
char txt3[3];
char txt4[4];
char text0[] ="Inverter";
char text1[] ="ON";
char text2[] ="OFF";
char text3[] ="B-Low!!!";
char text4[] ="Ch";
char text5[] ="Full";
char text6[] ="AC";
//char text7[] ="Low!";
char text8[] ="Overload!";
//
long tlong, tlong1;
long clear;
int cnt_inverter,cnt_over, cnt_charge,cnt_charge_clear,cnt_main;
void main()
{
TRISC = 0xFF; // PORTC all input
TRISA = 0xFF;
TRISB = 0b00000001;
ADCON1 = 0x00;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(1000);
while(1)
{
// clear LCD
clear++;
while(clear<=2)
{
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
break;
}
// main logic sequence...........
if(RC2_bit==1)// inverter signal available.
{
if(RC0_bit==1 || RC1_bit ==1)
{
cnt_inverter = 0;
cnt_over++;
if(cnt_over<=2)
{
clear = 0;
}
else
{
while(RC0_bit==1)// low bat signal available
{
Lcd_Out(2,2,text3); // battery low
break;
}
while(RC1_bit==1)// overload signal available
{
Lcd_Out(2,2,text8); // Overload
break;
}
}
}
else // Inverter on
{
cnt_over=0;
cnt_inverter++;
while(cnt_inverter<=2)
{
clear = 0;
break;
}
Lcd_Out(1,1,text0); // inverter
Lcd_Out(1,10,text1); // on
// Read Battery voltage 0 to 30V
ADCON0 = 0b00000001;
adc_rd2 = ADC_Read(0); // A/D conversion. Pin RA2 is an input.
//Lcd_Out(2,8,text); // Write result in the second line
tlong1 = (long)adc_rd2 *117.6470588235294; // Convert the result in millivolts
tlong1 = tlong1;
ch1 = tlong1/1000; // Extract volts (thousands of millivolts)
ch1 = (tlong1 / 10000) % 10;
Lcd_Out(2,2,"B:");
Lcd_Chr(2,4,48+ch1); // Write result in ASCII format
ch1 = (tlong1 / 1000) % 10;
Lcd_Chr(2,5,48+ch1); // Write result in ASCII format
Lcd_Chr_CP('.');
ch1 = (tlong1 / 100) % 10; // Extract hundreds of millivolts
Lcd_Chr_CP(48+ch1); // Write result in ASCII format
ch1 = (tlong1 / 10) % 10; // Extract tens of millivolts
Lcd_Chr_CP(48+ch1); // Write result in ASCII format
Lcd_Chr_CP('V');
cnt_main = 0;
}
}
else // inverter signal not available
{
cnt_inverter = 0;
cnt_main++;
while(cnt_main<=1)
{
clear = 0;
break;
}
while(RC4_bit==1)// mains on
{
while(RC3_bit==1 || RC5_bit==1)// either charging or charge full
{
while(RC3_bit==1) // charging
{
Lcd_Out(2,2,text4); // charge
Lcd_Out(2,5,text1); // on
//Lcd_Out(2,12," "); // on
cnt_charge++ ;;
while(cnt_charge<=1)
{
clear = 0;
break;
}
break;
}
while(RC5_bit==1) // charge full
{
cnt_charge=0;
Lcd_Out(2,2,text4); // charge
Lcd_Out(2,5,text5); // Full
break;
}
break;
}
// show mains here
Lcd_Out(1,1,text6); // AC
Lcd_Out(1,4,text1); // on
// read Mains voltage voltage
ADCON0 = 0b00100001;
adc_rd0 = ADC_Read(4); // A/D conversion. Pin RA2 is an input.
//Lcd_Out(1,1,text); // Write result in the second line
tlong = (long)adc_rd0 *19.6078431372549; // Convert the result in millivolts
tlong = tlong*44/1000;
ch = tlong; // Extract volts (thousands of millivolts)
txt2[0]=(ch/100) +48;
txt2[1]=((ch/10)%10) +48;
txt2[2]=((ch/1)%10) +48;
//txt2[3]=(ch%10) +48;
txt2[3]=0;
Lcd_Out(1,8,"AC:");
Lcd_Out(1,11, txt2);
Lcd_Chr_CP('V');
break;
}
}
} // while
} // void main