#include
#include
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
#define CLOCK P1_0
#define D_IN P1_1
#define D_OUT P1_2
#define _CS P1_3
sbit g1=P2^7;
sbit g2=P2^1;
sbit g3=P2^3;
sbit g4=P2^5;
ulong AD_ad1,AD_ad2;
float JiZhun_AD=2.5;
float AD_Data=0.0;
ulong AD_ad=0;
char chi;
unsigned char code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
void delay02s(void)
{
unsigned char i,j,k;
for(i=1;i>0;i--)
for(j=10;j>0;j--)
for(k=200;k>0;k--);
}
uint read1543(uchar port)
{
uint ad;
uint i;
uchar al=0,ah=0;
CLOCK=0;
_CS=0;
port<<=4;
for (i=0;i<4;i++) //将四位通道地址送1543
{
D_IN=(bit)(port&0x80);CLOCK=1;CLOCK=0;
port<<=1;
}
for (i=0;i<6;i++) //填6个CLOCK信号
{
CLOCK=1;CLOCK=0;
}
_CS=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();
_CS=0; //等待AD转换结束
_nop_();_nop_();_nop_();
for (i=0;i<2;i++) //D9,D8
{
D_OUT=1;CLOCK=1;ah<<=1;
if (D_OUT) ah +=0x01;
CLOCK=0;
}
for (i=0;i<8;i++) //取出D7--D0
{
D_OUT=1;CLOCK=1;al <<= 1;
if (D_OUT)
al +=0x01;
CLOCK=0;
}
_CS=1;
ad = (uint)ah;
ad <<= 8;
ad +=(uint)al; //
return(ad);
}
void main()
{
TMOD=0x01;
TH0=(65535-20000)/255;
TL0=(65535-20000)%255;
EA=1;ET0=1;TR0=1;
while(1)
{
P0=table[(AD_ad%10)];
g1=1;
delay02s();
g1=0;
P0=table[(AD_ad/10)%10];
g2=1;
delay02s();
g2=0;
P0=table[(AD_ad/100)%10];
g3=1;
delay02s();
g3=0;
P0=table[(AD_ad/1000)]|0X80;
g4=1;
delay02s();
g4=0;
}
}
zhongduan()interrupt 1
{
TH0=(65535-20000)/255;
TL0=(65535-20000)%255;
chi++;
if(chi==50)
{
chi=0;
AD_ad1 = read1543(0); // (通道0电压正端)
AD_Data = ((float)AD_ad1)*JiZhun_AD*2/1024+0.15005;//0.15经过实测需要加0.15减少误差
AD_ad =((AD_Data-2.68)/0.185)*1000;
}
}