#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^2;
sbit smg1=P2^4;
sbit smg2=P2^5;
sbit smg3=P2^6;
uchar temp;
unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned char code smg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78};
void init_ds18b20(void);
void delay(uint t);
void write_byte(uchar dat);
uchar read_byte(void);
uchar readtemperature(void);
void display(void);
//************************************************
//延时函数,在12MHz的晶振频率下
//大约50us的延时
//************************************************
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void main(void)
{
while(1)
{
temp=readtemperature();
display();
}
}
void delay(uint t)
{
while(t--);
}
void init_ds18b20(void)
{
uchar n;
DQ=1;
delay(8);
DQ=0;
delay(80);
DQ=1;
delay(8);
n=DQ;
delay(4);
}
void write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delay(4);
DQ=1;
dat>>=1;
}
delay(4);
}
uchar read_byte(void)
{
uchar i,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ)
value|=0x80;
delay(4);
}
return value;
}
uchar readtemperature(void)
{
uchar a,b;
init_ds18b20();
write_byte(0xcc); //跳过ROM
write_byte(0x44); //启动温度测量
delay(300);
init_ds18b20();
write_byte(0xcc);
write_byte(0xbe);
a=read_byte();
b=read_byte();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void display(void)
{
smg3=0;
smg2=0;
smg1=0;
P1=smg_du[temp%10];
delay_50us(20);
smg2=0;
smg1=1;
P1=smg_du[temp/10];
delay_50us(20);
}