#include"reg52.h"
unsigned table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char table2[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
sbit clk=P2^0;
sbit AB=P2^1;
sbit com1=P2^2;
sbit com2=P2^3;
sbit com3=P2^4;
sbit com4=P2^5;
sbit dq=P3^6;
bit mark;
sbit beef=P3^1;
unsigned char temp=0;
void delay1()
{ int i,j,k;
for(i=0;i<1;i++)
for(j=0;j<2;j++)
for(k=0;k<180;k++);
}
void delay2(z)
{
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void timer0()
{
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
void delay_1ms(unsigned char time)
{
unsigned char a,b;
for(a=time;a>0;a--)
for(b=200;b>0;b--);
}
void delay2us(unsigned char t)
{
while(--t);
}
void fun(num)
{
int m;
for(m=0;m<8;m++)
{
clk=0;
AB=num&0x01;
num=num>>1;
clk=1;
}
}
bit ds18b20(void)
{
bit flag;
dq=1;
delay2us(10);
dq=0;
delay2us(200);
delay2us(200);
dq=1;
delay2us(50);
flag=dq;
delay2us(25);
return flag;
}
/*向18b20写入一个字节的数据*/
void write_dat(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
dq=0;
delay2us(2);
dq=dat&0x01;
delay2us(22);
dq=1;
dat>>=1;
}
}
/*读取18b20一个字节的数据*/
unsigned char read_dat(void)
{
unsigned char i,dat=0;
for(i=0;i<8;i++)
{
dq=0;
dat>>=1;
dq=1;
if(dq)
dat|=0x80;
delay2us(25);
}
return(dat);
}
/*读取温度*/
void get_temp(void)
{
unsigned char a[2]={0x00,0x00};
ds18b20();
write_dat(0xcc);
write_dat(0x44);
delay_1ms(2);
ds18b20();
write_dat(0xcc);
write_dat(0xbe);
a[0]=read_dat();
a[1]=read_dat();
temp=(((a[0]&0xf0)>>4))|((a[1]&0x0f)<<4);
/*********************温度数据处理程序段
if((b&0xf8)==0xf8)
{
mark=1;
b<<=8;
temp=((~(a+b))+1)*0.625;
}
else
{
mark=0;
b<<=8;
temp=(a+b)*0.625;
} */
}
main()
{
unsigned char g,s; int num,k;
TR0=1; P1=0xfe;
while(1)
{
get_temp();
g=temp%10;
s=temp/10;
com1=0;
com2=1;
fun(table[g]);
delay1();
com1=1;
com2=0;
fun(table[s]);
delay1();
if((s>=3)&(g>=8))
{
beef=0;
if(TF0==1)
{
num++;
if(num==20)
{ timer0();
num=0;
k++;
if(k==8)
{k=0;P1=0xef;}
P1=table2[k];
}
TF0=0;
}
}
else
{
beef=1;
if(TF0==1)
{
num++;
if(num==20)
{
timer0();
num=0;
k++;
if(k==8){k=0;P1=0xef;}
P1=table1[k];
}
TF0=0;
}
}
}
}