温度传感器
读整数
u16 read_temperture(void)
{
u16 temp_return=0;
u8 H=0,L=0;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
twice:
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
L= Read_DS18B20();
H= Read_DS18B20();
temp_return=(u16)(H<<8 |L)*0.0625 ; //只读整数,读一位小数就乘0.625
while(temp_return=85) //跳过上电85,重新读取即可 读小数/10即可
{
goto twice;
}
return temp_return;
}
u8 read_temperture(void) //读取整数温度
{
u8 temp_return=0;
u8 H=0,L=0;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
twice:
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
L= Read_DS18B20();
H= Read_DS18B20();
temp_return=(H<<4 |L>>4) ;
while(temp_return==85) //跳过上电85,重新读取即可
{
goto twice;
}
return temp_return;
}
修改分辨率
void Config_18B20()
{
init_ds18b20();
Write_DS18B20(0xcc);//跳过ROM
Write_DS18B20(0x4e);//写暂存器指令4E
Write_DS18B20(0x7d);//写高速缓存器TH高温限值125度 这个随便写
Write_DS18B20(0x00);//写高速缓存器TL低温限值0度 这个随便写
Write_DS18B20(0x1f);//写配置寄存器4
//0x1f : 0.5000°C 转换时间93.75ms
//0x3f : 0.2500°C 转换时间187.5ms
//0x5f : 0.1250°C 转换时间375ms
//0x7f : 0.0625°C 转换时间750ms
init_ds18b20();
}