代码:void delay_us(uchar t)//微秒延时
{
while(t--)
{
;
}
}
void init_ds18b20() //数据初始化
{
DQ=1;
delay_us(6);
DQ=0;
delay_us(600);
DQ=1;
delay_us(30);
}
void write_ds18b20(uchar dat) //写数据
{
uchar i;
for(i=0; i<8; i++)
{
DQ=0;
DQ=dat&0x01;
delay_us(30);
DQ=1;
dat=dat>>1;
}
delay_us(10);
}
uchar read_ds18b20() //读数据
{
uchar i=0,readat=0;
for(i=0; i<8; i++)
{
DQ=0;
readat=readat>>1; //八位全部都是0,向右移一位后,仍然全是0
DQ=1;
if(DQ) //如果DQ为1,readat进行或运算,如果为0,则子语句不执行,直接for循环此时采集的数据是0
{
readat=readat|0x80; //此时最高位为1 ,然后再进行for循环 最高位成为第七位,依次往复
}
delay_us(6);
}
return readat;
}
void check_rom(uchar a) //匹配序列号
{
uchar j;
write_ds18b20(0x55);
if(a==1)
{
for(j=0; j<8; j++)
{
write_ds18b20(table1[j]);
}
}
if(a==2)
{
for(j=0; j<8; j++)
{
write_ds18b20(table2[j]);
}
}
}
uint change_ds18b20(uchar z)//测量指定的温度传感器的温度
{
uchar tl,th;
init_ds18b20(); //初始化
write_ds18b20(0xcc); //跳过
init_ds18b20();
if(z==1)
{
check_rom(1); // 匹配rom1,第1个
}
if(z==2)
{
check_rom(2); // 匹配rom2,第2个
}
write_ds18b20(0x44);//启动温度转换
init_ds18b20();
write_ds18b20(0xcc);//跳过
init_ds18b20();
if(z==1)
{
check_rom(1); // 匹配rom1,第1个
}
if(z==2)
{
check_rom(2); // 匹配rom2,第2个
}
write_ds18b20(0xbe); //启动读暂存器。读内部ram中9字节温度数据
tl=read_ds18b20();
th=read_ds18b20();
t=th;
t=t<<8;
t=t|tl;
if(th < 0x80)//正温度
{
fg=0;
}
if(th>= 0x80)//负温度
{
fg=1;
t=~t+1;
}
t=t*(0.0625*10);
return t;
}
void display_lcd1602(uchar z)//显示各个温度
{
//温度的个十百千位换算出来,方便显示
t1=t/1000;
t2=t%1000/100;
t3=t%100/10;
t4=t%10;
if(z==1)
{
write_com(0x80);
write_dat('T');
write_dat('1');
write_dat(':');//显示当前温度
if(fg==0)//正温度显示
{
write_dat(' ');
}
else//负温度
{
write_dat('-');
}
if(t>999)
{
write_dat(t1+0x30);
}
else//是0不显示
{
write_dat(' ');
}
if(t>99)
{
write_dat(t2+0x30);
}
else//是0不显示
{
write_dat(' ');
}
write_dat(t3+0x30);
}
else if(z==2)
{
write_com(0x80+8);
write_dat('T');
write_dat('2');
write_dat(':');//显示当前温度
if(fg==0)//正温度显示
{
write_dat(' ');
}
else//负温度
{
write_dat('-');
}
if(t>999)
{
write_dat(t1+0x30);
}
else//是0不显示
{
write_dat(' ');
}
if(t>99)
{
write_dat(t2+0x30);
}
else//是0不显示
{
write_dat(' ');
}
write_dat(t3+0x30);
}
}
{
while(t--)
{
;
}
}
void init_ds18b20() //数据初始化
{
DQ=1;
delay_us(6);
DQ=0;
delay_us(600);
DQ=1;
delay_us(30);
}
void write_ds18b20(uchar dat) //写数据
{
uchar i;
for(i=0; i<8; i++)
{
DQ=0;
DQ=dat&0x01;
delay_us(30);
DQ=1;
dat=dat>>1;
}
delay_us(10);
}
uchar read_ds18b20() //读数据
{
uchar i=0,readat=0;
for(i=0; i<8; i++)
{
DQ=0;
readat=readat>>1; //八位全部都是0,向右移一位后,仍然全是0
DQ=1;
if(DQ) //如果DQ为1,readat进行或运算,如果为0,则子语句不执行,直接for循环此时采集的数据是0
{
readat=readat|0x80; //此时最高位为1 ,然后再进行for循环 最高位成为第七位,依次往复
}
delay_us(6);
}
return readat;
}
void check_rom(uchar a) //匹配序列号
{
uchar j;
write_ds18b20(0x55);
if(a==1)
{
for(j=0; j<8; j++)
{
write_ds18b20(table1[j]);
}
}
if(a==2)
{
for(j=0; j<8; j++)
{
write_ds18b20(table2[j]);
}
}
}
uint change_ds18b20(uchar z)//测量指定的温度传感器的温度
{
uchar tl,th;
init_ds18b20(); //初始化
write_ds18b20(0xcc); //跳过
init_ds18b20();
if(z==1)
{
check_rom(1); // 匹配rom1,第1个
}
if(z==2)
{
check_rom(2); // 匹配rom2,第2个
}
write_ds18b20(0x44);//启动温度转换
init_ds18b20();
write_ds18b20(0xcc);//跳过
init_ds18b20();
if(z==1)
{
check_rom(1); // 匹配rom1,第1个
}
if(z==2)
{
check_rom(2); // 匹配rom2,第2个
}
write_ds18b20(0xbe); //启动读暂存器。读内部ram中9字节温度数据
tl=read_ds18b20();
th=read_ds18b20();
t=th;
t=t<<8;
t=t|tl;
if(th < 0x80)//正温度
{
fg=0;
}
if(th>= 0x80)//负温度
{
fg=1;
t=~t+1;
}
t=t*(0.0625*10);
return t;
}
void display_lcd1602(uchar z)//显示各个温度
{
//温度的个十百千位换算出来,方便显示
t1=t/1000;
t2=t%1000/100;
t3=t%100/10;
t4=t%10;
if(z==1)
{
write_com(0x80);
write_dat('T');
write_dat('1');
write_dat(':');//显示当前温度
if(fg==0)//正温度显示
{
write_dat(' ');
}
else//负温度
{
write_dat('-');
}
if(t>999)
{
write_dat(t1+0x30);
}
else//是0不显示
{
write_dat(' ');
}
if(t>99)
{
write_dat(t2+0x30);
}
else//是0不显示
{
write_dat(' ');
}
write_dat(t3+0x30);
}
else if(z==2)
{
write_com(0x80+8);
write_dat('T');
write_dat('2');
write_dat(':');//显示当前温度
if(fg==0)//正温度显示
{
write_dat(' ');
}
else//负温度
{
write_dat('-');
}
if(t>999)
{
write_dat(t1+0x30);
}
else//是0不显示
{
write_dat(' ');
}
if(t>99)
{
write_dat(t2+0x30);
}
else//是0不显示
{
write_dat(' ');
}
write_dat(t3+0x30);
}
}

