#include<reg51.h>
#include<intrins.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
uint temp;
uchar i;
uint a;
uchar v;
uchar qian,bai,shi,ge;
void display();
void delay(uint z);
void time0_init();
void time1_init();
void AD_0809();
uchar code table1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code table2[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};
sbit ST=P2^6;
sbit OE=P2^5;
sbit EOC=P2^4;
sbit CLK=P2^3;
sbit ALE=P2^6;
sbit OUT=P3^6;
uint gatedate;
uchar flag;
float t;
void main()
{
flag=0;
time1_init();
time0_init();
while(1)
{
AD_0809();
if((v==4)||(v>4))
{
OUT=1; //继电器输出
}
else if(v>2&&v<4)
{
display();
t=(1.25*3)*(1.25*3*10)/(v*v);
printf("%.2f",t); //保留小数点后2位小数
t=t*100; //a累计计数100次为1s
display();//数码管显示程序
while(a==t) //a累计计数100次为1s
{
flag=1; //标记位改为1
OUT=1;
}
}
else
{
while(a==1500)
{
flag=0;
}
}
}
}
void time0_init()
{
TMOD=0x12;
TH0=(65536-2)/256;
TL0=(65536-2)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0()interrupt 2
{
CLK=~CLK;
}
void display()
{
P3=0xbe;
P0=table1[qian];
delay(2);
P3=0xbd;
P0=table2[bai];
delay(2);
P3=0xbb;
P0=table1[shi];
delay(2);
P3=0xb7;
P0=table1[ge];
delay(2);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer1()interrupt 1
{
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
a++;
i++;
if(i==10)
{
i=0;
temp++;
if(temp==9999)
temp=0;
if(flag==0)
{
qian=temp/1000;
bai=temp%1000/100;
shi=temp%100/10;
ge=temp%10;
}
}
}
void time1_init()
{
TMOD=0x12;
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
EA=1;
ET1=1;
TR1=1;
}
void AD_0809()
{
ST=0;
_nop_();
OE=0;
_nop_();
ST=1;
_nop_();
ST=0;
while(EOC==0);
OE=1;
gatedate=P1;
_nop_();
OE=0;
v=gatedate*5/255;
}
|