DS18B20 处理正负温度值。
寄存器格式
例子
//计算温度值
//参数 高字节,低字节
double CaculateTemp(uint8_t tmh, uint8_t tml)
{uint8_t th;uint8_t tl;double temp = 0;tl = tml & 0x0F;//取低字节后四位th = (tmh << 4) + (tml >> 4);//取高字节后三位和低字节前四位temp = (int)th;//整数部分if (tmh > 0x08){th = ~th + 1;//取反加一temp = -th;//负数}temp += tl * 0.0625;//小数部分printf(" Ds18b20ReadData temp=%3.3f \n", temp);return temp;
}
CaculateTemp(0xFE,0x6F);//用表格中的例子测试
结果