本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
#include"reg52.h"
#define uint unsigned int
#define uchar unsigned char
sbit duan=P2^0;
sbit wei=P2^1;
sbit CON=P0^0;
sbit DQ=P0^1;
uchar code shu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar code xiaoshu[16]={0x00,0x01,0x01,0x02,0x03,
0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
void rst();
void del(uint z);
void delay(uint z);
void write(uchar val);
uchar read();
float rwd();
void du();
void we();
void display();
float i;
void main()
{
while(1)
{
i=rwd();
if(i<=100)
CON=0;
else
CON=1;
display();
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void del(uint z)
{
while(z--);
}
void du()
{
duan=0;
duan=1;
}
void we()
{
wei=0;
wei=1;
}
void rst()/***复位*/
{
uchar p=1;
while(p)
{
while(p)
{
DQ=1;
;;
DQ=0;
del(50);
DQ=1;
del(6);
p=DQ;
}
del(45);
p=~DQ;
}
DQ=1;
}
void write(uchar val)/*写*/
{
uchar i;
for(i=8;i>0;i--)
{
DQ=1;
DQ=0;
DQ=val&0x01;
del(6);
val>>=1;
}
DQ=1;
del(1);
}
uchar read()
{
uchar i,value=0;
for(i=8;i>0;i--)
{
DQ=1;
value>>=1;
DQ=0;
DQ=1;
if(DQ)
value|=0x80;
del(6);
}
DQ=1;
return value;
}
float rwd()/*读温度*/
{
float zs,xs;
uchar temp[2];
EA=0;
rst();
write(0xcc);
write(0xbe);
temp[0]=read();
temp[1]=read();
rst();
write(0xcc);
write(0x44);
xs=xiaoshu[temp[0]&0x0f];
xs/=10;
zs=(temp[0]>>4)+(temp[1]<<4);
return (zs+xs);
EA=1;
}
void display()
{
uint k=i*10;
if(k>1000)
{
P3=shu[1];
du();
P3=0xf7;
we();
k-=1000;
delay(1);
}
P3=0xff;
we();
P3=shu[k/100];
du();
P3=0xfb;
we();
delay(1);
P3=0xff;
we();
P3=shu[k%100/10]&0x7f;
du();
P3=0xfd;
we();
delay(1);
P3=0xff;
we();
P3=shu[k%10];
du();
P3=0xfe;
we();
delay(1);
P3=0xff;
we();
} |