标题: DS18b20温度怎么计算? [打印本页]

作者: nan1325738369    时间: 2018-5-15 11:39
标题: DS18b20温度怎么计算?
uint retemp(){
        uint a,b,tt;
        uint t;
        init_ds18b20();
        writebyte(0xcc);
        writebyte(0x44);
        init_ds18b20();
        writebyte(0xcc);
        writebyte(0xbe);
        a = readbyte();
        b = readbyte();
        t = b;
        t <<= 8;
        t = t|a;
        if((t<=0xfc90)&&(t>=0x00)){                                 //当温度大于-55° 小于0°
                num = 0;
    tt = t*0.0625*10;
                return(tt);
        }else if((t>0x00)&&(t<=0xffff)){                             //当温度大于0°小于127°时
                num = 1;
    tt = (~t+2)*0.0625*10;
                return(tt);
        }
}


作者: 老田8310    时间: 2018-5-15 15:05
你从18D20读到的16位数据,中间八位就是温度的整数位值。可以参考下面的程序:
work_temp(uint tem)
{
uchar n=0;
if(tem>6348)                        // 温度值正负判断
     {tem=65536-tem;n=1;}            // 负温度求补码,标志位置1
      display[4]=tem&0x0f;           // 取小数部分的值
  display[0]=ditab[display[4]];  // 存入小数部分显示值
      display[4]=tem>>4;             // 取中间八位,即整数部分的值
  display[3]=display[4]/100;     // 取百位数据暂存
  display[1]=display[4]%100;     // 取后两位数据暂存
  display[2]=display[1]/10;      // 取十位数据暂存
  display[1]=display[1]%10;  

作者: zl2168    时间: 2018-5-15 21:16
本帖最后由 zl2168 于 2018-5-15 21:17 编辑

实例97  一线式DS18B20测温
Proteus仿真一下,确认有效。
实例97 DS18B20测温.rar (51.78 KB, 下载次数: 19)

以上摘自张志良编著《80C51单片机仿真设计实例教程——基于Keil CProteus》清华大学出版社ISBN 978-7-302-41682-1内有常用的单片机应用100案例,用于仿真实验操作,电路与程序真实可靠可信可行。





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1