標題:
單片機溫度傳感器代碼有幾句看不懂temp=tt*10+0.5
[打印本頁]
作者:
4564897
時間:
2019-4-19 23:29
標題:
單片機溫度傳感器代碼有幾句看不懂temp=tt*10+0.5
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;
}
作者:
chtyise
時間:
2019-4-20 08:20
tt=temp*0.0625 :DS18B20讀取的值后四位二進制數(shù)是小數(shù),要轉換十進制。因為二進制的0.0001 = 十進制0.0625,所以十進制溫度值=讀出值乘以0.0625
temp=tt*10+0.5 :返回值temp是uint,無符號整形,tt浮點型,有四位小數(shù),浮點型轉化無符號整形后面小數(shù)會丟掉。乘10,為了保留最后一位小數(shù),加0.5,是四舍五入。
tt=12.9345 * 10 + 0.5 返回值temp = 129 溫度12.9
tt=129.545 * 10 + 0.5 返回值temp = 130 溫度13.0
作者:
wulin
時間:
2019-4-20 08:40
取整數(shù),小數(shù)部分四舍五入。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1