登录|立即注册|使用QQ帐号登录
论坛 > 24小时必答区
发帖|
看4144|回2|收藏
楼主 ID:516297 只看他
2019-4-19 23:29
DS18B20中获取温度中tt=temp*0.0625;temp=tt*10+0.5;这两句什么意思啊,为什么要有temp=tt*10+0.5??这时候tt不就是温度啦吗??这个求得是什么,能举例子说明吗?


uint tmp()               //get the temperature
{
  float tt;
  uchar a,b;
  dsreset();
  delay(1);
  tmpwritebyte(0xcc);
  tmpwritebyte(0xbe);
  a=tmpread();
  b=tmpread();
  temp=b;
  temp<<=8;             //two byte  compose a int variable
  temp=temp|a;
  tt=temp*0.0625;
  temp=tt*10+0.5;
  return temp;
}
沙发 ID:337069 只看他
2019-4-20 08:20
tt=temp*0.0625  :DS18B20读取的值后四位二进制数是小数,要转换十进制。因为二进制的0.0001 = 十进制0.0625,所以十进制温度值=读出值乘以0.0625
temp=tt*10+0.5 :返回值temp是uint,无符号整形,tt浮点型,有四位小数,浮点型转化无符号整形后面小数会丢掉。乘10,为了保留最后一位小数,加0.5,是四舍五入。
tt=12.9345 * 10 + 0.5    返回值temp = 129      温度12.9
tt=129.545 * 10 + 0.5    返回值temp = 130      温度13.0
板凳 ID:213173 只看他
2019-4-20 08:40
取整数,小数部分四舍五入。

51黑电子论坛

Powered by Discuz! X3.1

首页|标准版|触屏版|电脑版