// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections
float I; //to store Current value
char Current[14];
float v; //to store voltage value
char voltage[5];
void Voltage_Read() //Vrms calculation
{
float max;
int i;
int t[40];
ADCON0=0b01000001;
delay_us(20);
ADCON0.ADON=1;
for(i=0; i<=39; i++)
{
v= ADC_Read(0);
v=v*(10.0/1023.0);
v=(v-5.0);
t[i]=v*110.1909091;
}
ADCON0.ADON=0;
max=t[0];
for(i=0; i<=39; i++)
{
if(max<t[i])
max=t[i];
}
max=max*.757106781;
ByteToStr(max,voltage);
Lcd_out(1,9,voltage);
delay_ms(1000);
}
void Current_Read()
{
ADCON0=0b01000101;
delay_us(20);
ADCON0.B2=1;
I=ADC_read(1); //reading ADC value
I=I*0.0048875; //finding voltage from ADC reading
I=I/4.4; //dividing by amplifying factor
I=I/120; //dividing by burden resistor value to get secondary current
I=I*2500; //multiplying secondary Current by transformer turns ratio
FloatToStr(I,Current);
delay_us(100);
Lcd_out(2,9,Current);
delay_ms(1000);
}
void main()
{
TRISA = 0xFF; // set all pins of PORT A as input
TRISB =0x00;
ADCON1=0b00000000;
ADCON0=0b01000000;
ANSEL =0b0000011;
ANSELH=0x00;
CM1CON0.B7=0;
CM1CON0.B5=0;
CM2CON0.B7=0;
CM2CON0.B5=0;
Delay_ms(100);
ADCON0.B0=1;
delay_us(20);
ADCON0.B2=1;
//OPTION_REG = 0b00101000; // set TOCKI as clock counter
Lcd_Init();
delay_us(100);
Lcd_Cmd(_LCD_CLEAR);
delay_us(100);
Lcd_Cmd(_LCD_CURSOR_OFF);
delay_us(100);
while(1)
{
Lcd_out(1,1,"Voltage:");
delay_us(100);
Lcd_Chr(1,12,'V');
Lcd_out(2,1,"Current:");
delay_us(100);
Lcd_Chr(2,16,'A');
Voltage_Read();
delay_ms(100);
Current_Read();
}// while
}// void main