#include<reg51.h>
#include<stdio.h>
#include<math.h>
#define dataport P1
sbit cap1=P0^0;
sbit cap2=P0^1;
sbit cap3=P0^2;
sbit cap4=P0^3;
sbit relay=P0^4;
sbit autoled=P0^5;
sbit manualled=P2^0;
sbit clsw=P0^6;
sbit amsw=P0^7;
char array[10];
//char array1[16];
void TOM1Delay(void);
sbit rs=P2^3;
sbit rw=P2^2;
sbit en=P2^1;
sbit sw1=P2^4;
sbit sw2=P2^5;
sbit sw3=P2^6;
sbit sw4=P2^7;
sbit white=P3^2;
sbit black=P3^3;
void delay_ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<=count;i++)
{
for(j=0;j<=56;j++);
}
}
void lcdcmd(unsigned char cmd) // send command to LCD
{
dataport=cmd;
rs=0,en=1;
delay_ms(2),
en=0;
}
void lcddata(unsigned char item) //send data to LCD
{
dataport=item;
rs=1,en=1;
delay_ms(2),en=0;
}
void lcd_init() //LCD initialize
{
lcdcmd(0x38);
lcdcmd(0x06);
lcdcmd(0x0c);
lcdcmd(0x01);
}
void lcd_string(unsigned char *text,unsigned int loc)
{
int i;
lcdcmd(loc);
delay_ms(500);
for(i=0;text[i]!='\0';i++)
{
lcddata(text[i]);
}
}
void TOM1Delay(void)
{
TMOD=0X01;
TL0=0XFD;
TH0=0X4B;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
void main(void)
{
int a=0, b=0;
float c=0, cd=0, d=0, e=0, f=0, pf=0, pf1=0, pf2=0, ipf=0;
rw=0;
relay=0;
lcd_init();
lcd_string("POWER FACTOR ", 0x80);
lcd_string("CONTROLLER ", 0xC0);
TMOD=0X01;
TL0=0X00;
TH0=0X00;
cap1=0; //relay1 on
cap2=0; //relay2 off
cap3=0;
cap4=0;
lcdcmd(0x01);
while(1)
{
//AUTO MODE //
if(amsw==1)
{
autoled=1;
manualled=0;
TL0=0X00;
TH0=0X00;
while(white==1);
while(white==0);
TR0=1;
while(black==0);
TR0=0;
a=TL0;
b=TH0;
c=b*256;
d=c+a;
d=d*1.085;
d=d/1000;
if(d<5)
{
e=d*18*0.017453292;
pf=cos(e);
sprintf(array,"%.2f msec",d);
lcd_string("I LAGS=", 0x80);
lcd_string(array, 0x87);
sprintf(array,"%.2f",pf);
lcd_string("PF=------", 0xC0);
lcd_string(array, 0xC3);
if(d<1)
{
lcd_string("OFF CAPACITORS ", 0xC9);
cap1=0; //relay1 off
cap2=0; //relay2 off
cap3=0;
cap4=0;
}
else if(d<2)
{
cap1=1; //relay1 on
cap2=0; //relay2 off
cap3=0;
cap4=0;
lcd_string("ONE capacitor ON ", 0xC9);
}
else if(d<3)
{
;
cap1=1; //relay1 on
cap2=1; //relay2 on
cap3=0;
cap4=0;
lcd_string("TWO capacitor ON ", 0xC9);
}
else if(d<4)
{
;
cap1=1; //relay1 on
cap2=1; //relay2 on
cap3=1;
cap4=0;
lcd_string("THREE capacitor ON ", 0xC9);
}
else if(d<5)
{
;
cap1=1; //relay1 on
cap2=1; //relay2 on
cap3=1;
cap4=1;
lcd_string("FOUR capacitor ON ", 0xC9);
}
}
else
{
cap1=0; //relay1 off
cap2=0; //relay2 off
cap3=0;
cap4=0;
cd=10-d;
e=cd*18*0.017453292;
pf=cos(e);
sprintf(array,"%.2f msec",cd);
lcd_string("I LEADS=", 0x80);
lcd_string(array, 0x88);
sprintf(array,"%.2f",pf);
lcd_string("PF=------", 0xC0);
lcd_string(array, 0xC3);
}
if(clsw==0)
{
relay=0;
}
else
{
relay=1;
}
}
// MANUAL MODE //
else
{
autoled=0;
manualled=1;
TL0=0X00;
TH0=0X00;
while(white==1);
while(white==0);
TR0=1;
TR0=0;
a=TL0;
b=TH0;
c=b*256;
d=c+a;
d=d*1.085;
d=d/1000;
pf=cos(e);
if(sw1==0)
{
cap1=1;
}
else
{
cap1=0;
}
if(sw2==0)
{
cap2=1;
}
else
{
cap2=0;
}
if(sw3==0)
{
cap3=1;
}
else
{
cap3=0;
}
if(sw4==0)
{
cap4=1;
}
else
{
cap4=0;
}
if(d<5)
{
sprintf(array,"%.2f msec",d);
lcd_string("I LAGS=", 0x80);
lcd_string(array, 0x87);
sprintf(array,"%.2f",pf);
lcd_string("PF = ----------- ", 0xC0);
lcd_string(array, 0xC5);
if(clsw==0)
{
relay=0; //lagging
pf1=pf;
}
else
{
relay=1; //compensated
delay_ms(2000);
pf2=pf;
ipf=pf2-pf1;
sprintf(array,"%.2f",ipf);
if(pf1>0)
{
lcdcmd(0x01);
lcd_string("IMPROOVED PF =", 0x80);
lcd_string(array, 0xC0);
}
else
{
lcdcmd(0x01);
lcd_string("plese measure PF", 0x80);
lcd_string("in lagging mode", 0xC0);
}
delay_ms(2000);
lcdcmd(0x01);
}
}
else
{
cd=10-d;
e=cd*18*0.017453292;
pf=cos(e);
sprintf(array,"%.2f msec",cd);
lcd_string("I LEADS=", 0x80);
lcd_string(array, 0x88);
sprintf(array,"%.2f",pf);
lcd_string("PF = ", 0xC0);
lcd_string(array, 0xC5);
if(clsw==0)
{
relay=0; //lagging
pf1=pf;
}
else
{
relay=1;
delay_ms(2000);
pf2=pf;
sprintf(array,"%.2f",ipf);
if(pf1>0)
{
lcdcmd(0x01);
lcd_string("IMPROOVED PF =", 0x80);
lcd_string(array, 0xC0);
}
else
{
lcdcmd(0x01);
lcd_string("plese measure PF", 0x80);
lcd_string("in lagging mode", 0xC0);
}
lcdcmd(0x01);
}
}
}
}