关键程序:
void readtemp_ds18b20() //读取温度
{
uint temp32;
uchar templ,temph;
EA=0;
rest_ds18b20();//复位
write_ds18b20(0xcc); //跳过读序列号的操作
write_ds18b20(0x44); //启动温度转换
delay_50us(15);
rest_ds18b20();//复位
write_ds18b20(0xcc); //跳过读序列号的操作
write_ds18b20(0xbe); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
templ=read_ds18b20();//低八位
temph=read_ds18b20();//高八位
temp32=(temph<<8)|templ;//合并为十六位
if((temph&0xf0))//判断温度的正负性
{
temp32=~temp32+1;
temp32=temp32*0.625;//温度值扩大10倍
temp=-temp32;//温度为负数
}
else//为正数
{
temp=temp32*0.625; //温度值扩大10倍
}
EA=1;
}
void show_18B20()//显示18B20的温度
{
uchar i;
LcdSetCursor(2,7); //显示位置
i=temp/100;
write_LCD_data(tex[ i]);// //显示百位,即温度十位
i=temp/10%10;
write_LCD_data(tex[i ]);// //显示十位,即温度个位
write_LCD_data('.');//显示小数点
i=temp%10;
write_LCD_data(tex[i ]);//显示个位,即温度小数部分
}
|