#include<stdio.h>
#include<reg51.h>
#include<math.h>
sbit OR=P0^4;
sbit xiaoshu=P1^7;
sbit K1=P2^0;
sbit K2=P2^1;
sbit K3=P2^2;
sbit LBEN=P2^3;
sbit HBEN=P2^4;
sbit RUN=P2^5;
sbit GRE=P2^6;
sbit RED=P2^7;
sbit S1=P3^5;
sbit S2=P3^4;
unsigned char idata times;
float idata dl;
unsigned char shuxian[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned int yima(unsigned char m)
{int z;
z=shuxian[m];
return(z);
}
void deng(unsigned char x)
{unsigned char i;
i=1;
while (i<x)i++;
}
void dianliu1(float pp) /*电流在nA档,小数点不显示*/
{unsigned char a,b,c,d;
unsigned int tt;
pp=pp*10;
tt=(unsigned int)pp;
a=tt/1000;
b=tt/100%10;
c=tt/10%10;
d=tt%10;
S1=0;S2=0;
if(a==0) {P1=0x00;}
else if(a>0){P1=yima(a);deng(200);}
S1=1;S2=0;
P1=yima(b);deng(120);
S1=0;S2=1;
P1=yima(c);deng(120);
S1=1;S2=1;
P1=yima(d);deng(50);
}
void dianliu2(float pp) /*电流在nA档,小数点不显示*/
{unsigned char a,b,c,d;
unsigned int tt;
if (pp<0.01)
{pp=0;}
pp=pp*1000;
tt=(unsigned int)pp;
a=tt/1000;
b=tt/100%10;
c=tt/10%10;
d=tt%10;
S1=0;S2=0;
if(a==0) {P1=0x00;}
else if(a>0){P1=yima(a);deng(200);}
S1=1;S2=0;P1=yima(b);deng(200);
S1=0;S2=1;
P1=yima(c); deng(200);
S1=1;S2=1;
P1=yima(d);deng(50);
}
void dianliu3(float pp)
{unsigned char a,b,c,d;
unsigned int tt;
pp=pp*100;
tt=(unsigned int)pp;
a=tt/1000;
b=tt/100%10;
c=tt/10%10;
d=tt%10;
S1=0;S2=0;
if(a==0) {P1=0x00;}
else if(a>0){P1=yima(a);deng(200);}
S1=1;S2=0;
P1=yima(b); deng(200);
S1=0;S2=1;
P1=yima(c); deng(200);
S1=1;S2=1;
P1=yima(d);deng(50);
}
void dianliu4(float pp)
{unsigned char a,b,c,d;
unsigned int tt;
pp=pp*10;
tt=(unsigned int)pp;
a=tt/1000;
b=tt/100%10;
c=tt/10%10;
d=tt%10;
S1=0;S2=0;
if(a==0) {P1=0x00;}
else if(a>0){P1=yima(a);deng(200);}
S1=1;S2=0;
if(a==0&&b==0) {P1=0x00;}
else if(b>0){P1=yima(b);deng(200);}
S1=0;S2=1;
P1=yima(c);xiaoshu=1; deng(200);
S1=1;S2=1;
P1=yima(d);deng(50);
}
void dianliu5(float pp)
{unsigned char a,b,c,d;
unsigned int tt;
pp=pp*100;
tt=(unsigned int)pp;
a=tt/1000;
b=tt/100%10;
c=tt/10%10;
d=tt%10;
S1=0;S2=0;
if(a==0) {P1=0x00;}
else if(a>0){P1=yima(a);deng(200);}
S1=1;S2=0;
P1=yima(b);xiaoshu=1;deng(200);
S1=0;S2=1;
P1=yima(c); deng(200);
S1=1;S2=1;
P1=yima(d);deng(50);
}
void dianliu6(float pp)
{unsigned char a,b,c,d;
unsigned int tt;
tt=(unsigned int)pp;
a=tt/1000;
b=tt/100%10;
c=tt/10%10;
d=tt%10;
S1=0;S2=0;
if(a==0) {P1=0x00;}
else if(a>0){P1=yima(a);deng(200);}
S1=1;S2=0;
P1=yima(b);deng(120);
S1=0;S2=1;
P1=yima(c);deng(120);
S1=1;S2=1;
P1=yima(d);deng(50);
}
void baojin()
{S1=0;S2=0;P1=0x40;deng(60);
S1=1;S2=0;P1=0x79;deng(60);
S1=0;S2=1;P1=0x40;deng(2000);
}
float VIbian(unsigned int voltage,unsigned char ti)
{float current;
switch(ti) { case 0x00: {current=voltage/4000.0;}break;
case 0x01: {current=voltage/400.0;}break;
case 0x02: {current=voltage/40.0;}break;
case 0x03: {current=voltage/4.0;}break;
default:;
}
if(current<0.001) current=0;
return (current);
}
void DataIn_int0() interrupt 0
{
unsigned char m;
unsigned int idata ps=0;
unsigned int idata pp,qq,ph,pl;
P0=0xff; EX0=0; HBEN=0; LBEN=1;ph=P0&0x3f; P0=0xff;
HBEN=1; LBEN=0; pl=P0;
qq=(((unsigned int)ph&0x000f)<<8)+(unsigned int)pl;
if(qq>3800)
{RUN=0;
switch(times)
{case 0x00: {K1=1;K2=0; times=0x01; }break;
case 0x01: {K1=0;K3=1; times=0x02; }break;
case 0x02: {K1=0;K3=0; times=0x03; }break;
}
m=0; RUN=1; P0=0xff; HBEN=0; LBEN=1; ph=P0&0x3f;
P0=0xff; HBEN=1; LBEN=0; pl=P0;
qq=(((unsigned int)ph&0x00ff)<<8)+(unsigned int)pl;
}
else if(times!=0&&qq<300)
{RUN=0;
switch(times)
{case 0x01: {K1=1;K2=1; times=0x00; }break;
case 0x02: {K1=1;K2=0; times=0x01; }break;
case 0x03: {K1=0;K3=1; times=0x02; }break;
}
m=0; RUN=1; P0=0xff; HBEN=0; LBEN=1; ph=P0&0x3f; P0=0xff;
HBEN=1; LBEN=0; pl=P0;
qq=(((unsigned int)ph&0x000f)<<8)+(unsigned int)pl;
}
else{
P0=0xff; HBEN=0; LBEN=1; ph=P0&0x3f; P0=0xff; HBEN=1; LBEN=0; pl=P0;
ps=(((unsigned int)ph&0x000f)<<8)+(unsigned int)pl;
pp=pp+ps;
if(m>=0&&m<10) m=m+1;
else m=0;
if(m==0) { pp=pp/10;
dl=VIbian(pp,times);pp=0;
}
}
EX0=1;
}
main()
{ unsigned int idata i;
EX0=1; IT0=1; ET0=1;
K1=1;K2=1;times=0x00;
EA=1;
RUN=1;
for(i=0;i<1625;i++);
for(;;)
{
if(times==0x00)
{
if(dl>0&&dl<10){dianliu2(dl);RED=1;GRE=0;}
else if(dl>=10&&dl<100){dianliu3(dl);RED=1;GRE=0;}
else if(dl>=100&&dl<910){dianliu1(dl);RED=1;GRE=0;}
else ;
}
else if(times==0x01){dianliu5(dl);GRE=1;RED=0;}
else if(times==0x02){dianliu4(dl);GRE=1;RED=0;}
else if(times==0x03){dianliu6(dl);GRE=1;RED=0;}
}
}