unsigned char gett(void)
{
Init_DS18B20(); //将DS18B20初始化
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
for(time_DS18B20=0;time_DS18B20<100;time_DS18B20++);
//温度转换需要一点时间
Init_DS18B20(); //将DS18B20初始化
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器,前两个分别是温度的低位和高位
temp_l=ReadOneChar();
//低位在前
temp_h=ReadOneChar();
//高位在后
flag1=temp_h&0xf8;
if(flag1)
{ n=flag1;
temp_h=~temp_h;
if(temp_l==0)temp_h++;
//若低8位全为0且温度为负,取补时就要向高位进1
temp_l=~temp_l+1;
}
temnum=(temp_h*256+temp_l)/16;
return temnum; //返回温度值
}
上面是我的程序,但是temnum无法读出为负时的值,如何修改才能让返回的温度值temnum具有正负性