/******=====精度设置(无反回值)======*******/
void DS18B20_setup()///res这形参
{
while(DS18B20_Reset());///
WriteOneChar(0x4e);/////送入0x4E
delayms(10);
WriteOneChar(0x63);
delayms(10);
WriteOneChar(0x00);
delayms(10);
WriteOneChar(0x1f);///精度设置,
delayms(10);
}
/*******读取温度值,*********/
uint ReadTemperature()////char型函数只能反回char型数据
{
uint temp;
float temp_ft;
uchar a=0;
uchar b=0;
uchar t=0;
while (DS18B20_Reset());///复位成功while=0向下执行
WriteOneChar(0xcc); ///跳过 ROM
WriteOneChar(0x44); //启动温度转换
//delayms(790);////判忙,读取DQ的值,闲时DQ=1,忙时DQ=0
while(!DS18B20_ReadBit());
while (DS18B20_Reset());///从新复位
WriteOneChar(0xcc);
WriteOneChar(0xbe); ////读取暂存器
a=ReadOneChar();///设a 读到0xa7
b=ReadOneChar();////设b 读到0xe6 高8位
temp=b;
temp<<=8;
temp=temp|a;///这里将a b两个8位合并成了16位
temp_ft=0.0625*temp;///得到实际温度值
temp=temp_ft*10+0.5;//
return(temp);
}
|