#include <STC12C5A.H>
#define uchar unsigned char
#define uint unsigned int
sbit cs=P2^0;
sbit dat=P2^1;
sbit clk=P2^2;
sbit psb=P2^3;
sbit ret=P2^4;
sbit reley=P2^6;
sbit reley1=P2^7;
sbit aa=P1^0;
sbit fang_xiang1=P0^0;
sbit inc1=P0^1;
sbit cs1=P0^2;
sbit fang_xiang2=P0^3;
sbit inc2=P0^4;
sbit cs2=P0^5;
sbit jia1=P3^2;
sbit jian1=P3^3;
sbit jia2=P3^4;
sbit jian2=P3^5;
bit flag;
sbit key=P3^6;
uchar ADC_P1_EN,led;
uchar *t[]={"上电压值:","下电压值:","比值:"};
uchar code t1[]={"电流值:"};
float current;
uchar a1,a2,a3;
uint m1;
bit flag;
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--);
for(j=110;j>0;j--);
}
void delayms(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void send_byte(uchar da)
{
uchar i;
for(i=0;i<8;i++)
{
if(da&0x80)
dat=1;
else
dat=0;
clk=0;
clk=1;
da=da<<1;
}
}
void write_com(uchar command)
{
cs=1;
delay(5);
send_byte(0xf8);
send_byte(command&0xf0);
send_byte((command<<4)&0xf0);
cs=0;
}
void write_da(uchar temp)
{
cs=1;
delay(5);
send_byte(0xfa);
send_byte(temp&0xf0);
send_byte((temp<<4)&0xf0);
cs=0;
}
//LCD初始化
void lcd_init()
{
psb=0;
ret=1;
write_com(0x30);
delay(5);
write_com(0x01);
delay(5);
write_com(0x06);
delay(5);
write_com(0x0c);
delay(5);
}
void disp(uchar x,uchar*str)
{
uchar temp;
temp=*str;
write_com(x);
while(temp!='\0')
{
write_da(temp);
str++;
temp=*str;
}
}
uint get_adc_dat(uchar num)
{
uchar adc_flag=0;
uint dat;
ADC_RES=0;
ADC_RESL=0;
ADC_CONTR=0xe8|num;
while(adc_flag==0)
{
adc_flag=ADC_CONTR&0x10;
}
ADC_CONTR=ADC_CONTR&0xe7;
dat=ADC_RES*4+ADC_RESL;
return(dat);
}
void chu_li()
{
if(jia1==0)
{
delayms(1);
if(jia1==0)
{
cs=0;
fang_xiang1=1;
inc1=1;
inc1=0;
inc1=1;
cs1=1;
while(!jia1);
}
}
if(jian1==0)
{
delayms(1);
if(jian1==0)
{
cs1=0;
fang_xiang1=0;
inc1=1;
inc1=0;
inc1=1;
cs1=1;
while(!jian1);
}
}
if(jia2==0)
{
delayms(1);
if(jia2==0)
{
cs2=0;
fang_xiang2=1;
inc2=1;
inc2=0;
inc2=1;
cs=1;
while(!jia2);
}
}
if(jia2==0)
{
delayms(1);
if(jian2==0)
{
cs2=0;
fang_xiang2=0;
inc2=1;
inc2=0;
inc2=1;
cs2=1;
while(!jian2);
}
}
}
void key_scan()
{
if(key==0)
{
delayms(1);
if(key==0)
{
flag=~flag;
while(!key);
}
}
}
//主函数
void main()
{
uint i;
aa=0;
delayms(100);
aa=1;
lcd_init();
reley=0;
reley1=0;
disp(0x80,t1);
ADC_P1_EN=0xff;
led=1;
while(1)
{
key_scan();
current=get_adc_dat(2)/1024.0;
current=4.94545*current;
if(current>4.40)
{
led=1;
reley=1;
for(i=0;i<10;i++)
delayms(50000);
}
else
{
reley=0;
if(flag=0)
{
if(current>1.47&¤t<1.53)
{
reley1=1;
led=0;
delayms(10);
}
}
else
{
led=1;
reley1=0;
delayms(10);
chu_li();
}
}
m1=current*100;
a1=current;
a2=m1%100/10;
a3=m1%10;
write_com(0x84);
write_da(a1+48);
write_da('.');
write_da(a2+48);
write_da(a3+48);
}
}
|