DS18B20单总线多点测温,程序经过匹配64位ROM地址的办法,完成单总线两点测温并1602显现。该程序要先知道DS18B20的64位ROM地址才能够,ROM地址的读取办法可参考上一篇的介绍。
程序代码:
#include
#define
#define
sbit
sbit
sbit
sbit
uchar
uchar
//uchar
uchar
uchar
int
float
uchar
//*****************LCD1602*******************************
void
{
uint
for(x
for(y
}
void
{
lcdrs
P0
delay(5);
lcden
delay(5);
lcden
}
void
{
lcdrs
P0
delay(5);
lcden
delay(5);
lcden
}
void
{
uchar
lcdrw
lcden
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(i
{
write_date(table1[i]);
delay(5);
}
write_com(0x80+0x41);
for(i
{
write_date(0x2d);
delay(100);
}
write_com(0x80);
for(i
{
write_date(table[i]);
delay(5);
}
write_com(0x80+0x40);
for(i
{
write_date(table[i]);
delay(5);
}
}
//*******************DS18B20****************************
void
{
uint
ds
i
while(i>0)
i–;
ds
i
while(i>0)
i–;
}
bit
{
uint
bit
ds
i++;
ds
i++;
i++;
dat
i
while(i>0)
i–;
return
}
uchar
{
uchar
dat
for(i
{
j
dat
}
return
}
void
{
uint
uchar
bit
for(j
{
testb
dat
if(testb)//写1
{
ds
i++;i++;
ds
i
while(i>0)
i–;
}
else
{
ds
i
while(i>0)
i–;
ds
i++;i++;
}
}
}
void
{
char
tempwrite(0x55);
if(a==1)
{
for(j=0;j<8;j++)
tempwrite(rom1[j]);//发送18B20的序列号,先发送低字节
}
if(a==2)
{
for(j=0;j<8;j++)
tempwrite(rom2[j]);//发送18B20的序列号,先发送低字节
}
}
uint
{
uchar
dsreset();
delay(1);
//tempwrite(0xcc);
//dsreset();
if(z==1)
matchrom(1);
if(z==2)
matchrom(2);
tempwrite(0x44);
delay(5);
dsreset();
delay(1);
//tempwrite(0xcc);
//tempwrite(0xcc);
//dsreset();
if(z==1)
matchrom(1);
if(z==2)
matchrom(2);
tempwrite(0xbe);
a
b
temp
temp<<=8;
temp
if(temp<0x0fff)
temp=~temp+1;
tflag=1;//负值标识符
f_temp
temp
f_temp
return
}
void
{
write_com(address);
write_date(date);
}
void
{
write_wendu(0x80,N);
write_wendu(0x81,O);
write_wendu(0x82,.);
write_wendu(0x84,:);
write_wendu(0x89,.);
write_wendu(0x8b,0xdf);
write_wendu(0x8c,C);
write_wendu(0x80+0x40,N);
write_wendu(0x81+0x40,O);
write_wendu(0x82+0x40,.);
write_wendu(0x84+0x40,:);
write_wendu(0x89+0x40,.);
write_wendu(0x8b+0x40,0xdf);
write_wendu(0x8c+0x40,C);
}
void
{
uint
w1
w2
w3
w4
if(z==1)
{
}
if(z==2)
{
addr=0x40;
write_com(0x80+0x40+3);
write_date(0x30+2);
}
if(tflag==0)
write_wendu(0x80+addr+5,0×20);
else
write_wendu(0x80+addr+5,0x2d);
if(w1==0)
{
write_wendu(0x80+addr+6,0×20);//百位为0,不显现
if((w1==0)&&(w2==0))
write_wendu(0x80+addr+7,0×20);
else
write_wendu(0x80+addr+7,0×30+w2);
write_wendu(0x80+addr+8,0×30+w3);
}
else
{
write_wendu(0x80+addr+6,0×30+w1);
write_wendu(0x80+addr+10,0×30+w4);
}
}
//************************************************
void
{
while(1)
{
init_1602wendu();
tempchange(1);
display_wendu(1);
tempchange(2);
display_wendu(2);
}
}