#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
sbit DQ=P1^0;
bdate uchar dat;
sbit dat0=dat^0;
sbit dat7=dat^7;
uchar dp[16]={0,0,1,1,2,3,3,4,5,5,6,6,7,8,8,9};
uchar code segtab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar dbuf[4]={0,0,0,0};
void disp(void)
{ uchar i,n,wx;
wx=0xfe;
for(n=0;n<4;n++)
{P2=wx;
P0=segtab[dbuf[n]];
if(n==1) P0=P0&0x7F;
wxl=(wx>>1)+1;
for(i=1;i<200;i++);
P0=0xff;
}
}
void delay(unsigned int i)
{unsigned int j;
for(j=i;j>0;j--);
}
bit reset(void)
{bit flag;
DQ=0;
delay(50);
DQ=1;
delay()
flag=DQ;
delay(25);
return(flag);
}
void ds18b20_init(void)
{
while(1)
{
if(!reset())
{
DQ=1;
delay(40);
break;
}
else
reset();
}
}
void wrbyte(uchar dada)
{uchar i,dat;
dat=dada;
for(i=8;i>0;i--)
{
DQ=0;
delay(1);
DQ=dat0;
dat=dat>>1;
delay(1);
DQ=1;
}
}
uchar rdbyte(void)
{ uchar i;
uchar dat=0;
for(i=8;i>0;i--)
{ dat=dat>>1;
DQ=0;
_nop_();
DQ=1;
delay15(1);
dat7=DQ;
dalay15(4);
}
return(dat);
}
void convert(void)
{
ds18b20_init(void)
wrbyte(0xcc);
wrbyte(0x44);
}
int readt(void)
{uchar h,1;
ds18b20_init(void)
wrbyte(0xcc);
wrbyte(0xbe);
l=rdbyte();
h=rdbyte();
return(h*256+1);
}
void tdatacl(int x)
{
bit zf;
zf=0;
if(x<0)
{zf=1;
x=-x;
}
dbuf[0]=dp[t&0x0f];
x=x>>4;
dbuf[3]=x/100;
x=x%100;
dbuf[2]=x/10;
dbuf[1]=x%10;
if(zf==1)
{if(dbuf[2]==0)
{dbuf[3]=0x13;
dbuf[2]=0x12;
}
else
dbuf[3]=0x12;
}
else
{if(dbuf[3]==0)
{if(dbuf[2]==0)
dbuf[2]=0x13;
dbuf[3]=0x13;
}
}
}
void main(void)
{
int t;
while(1)
{convert();
t=readt();
tdatacl(t);
disp();
}
}
|