#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uint tempe;
sbit DQ=P3^5;
sbit p=P3^1;
sbit q=P3^2;
sbit r=P3^3;
sbit s=P3^4;
void delay1(uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--) ;
}
void delay(uint t)
{
while(t--);
}
void display(void)
{
p=0;
P1=table[tempe/10];
delay1(1);
P1=0xff;
p=1;
q=0;
P1=table[tempe];
delay1(1);
P1=0xff;
q=1;
r=0;
P1=0x9c;
delay1(1);
P1=0xff;
r=1;
s=0;
P1=0xc6;
delay1(1);
P1=0xff;
s=1;
}
void init_ds18b20(void)
{
uchar sample;
DQ=1;
delay(8);
DQ=0;
delay(80);
DQ=1;
delay(8);
sample=DQ; //return(sample)带返回检测器件响应
delay(4);
}
void wr_ds18b20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delay(4);
DQ=1;
dat>>=1;
}
delay(4);
}
uchar re_ds18b20(void)
{
uchar i;
uchar result;
for(i=0;i<8;i++)
{
DQ=0;
result>>=1;
_nop_();
DQ=1;
if(DQ)
result|=0x80;
delay(4);
}
return(result);
}
uchar rd_temperature(void)
{
uchar a,b;
init_ds18b20();
wr_ds18b20(0xcc); //跳过ROM
wr_ds18b20(0x44); //启动温度测量
delay(300);
init_ds18b20();
wr_ds18b20(0xcc);
wr_ds18b20(0xbe);
a=re_ds18b20();
b=re_ds18b20();
b<<=4;
b+=(a&0xf0)>>4;
return(b);
}
void main()
{
while(1)
{
tempe=rd_temperature();
display();
delay(1);
display();
delay(1);
}
}