#include "head_file.h" sbit dq = P2^7; void sdelay(unsigned int i)//延迟函数 { while(i--); } void DS18B20_Reset()//初始化 { unsigned char x=0; dq=1; sdelay(8); dq=0; sdelay(80); dq=1; sdelay(14); sdelay(20); } void DS18B20_Write_Byte(unsigned char dat) { unsigned char i=0; for(i=8;i>0;i--) { dq=0; dq=dat&0x01; sdelay(5); dq=1; dat>>=1; } } unsigned char DS18B20_Read_Byte() { unsigned char i=0,dat=0; for(i=8;i>0;i--) { dq=0; dat>>=1; dq=1; if(dq) dat|=0x80; sdelay(8); } return(dat); } unsigned int GET_Temperature() //获得当前温度 { unsigned char a=0,b=0; unsigned int t=0; float tt=0; EA = 0; DS18B20_Reset(); DS18B20_Write_Byte(0xCC);//跳过ROM DS18B20_Write_Byte(0x44);//开启温度转换 sdelay(5); DS18B20_Reset(); DS18B20_Write_Byte(0xCC); DS18B20_Write_Byte(0xBE);//读暂存器 a=DS18B20_Read_Byte(); b=DS18B20_Read_Byte(); EA = 1; t = b<<8; t+=a; t=t*0.625; t=t%1000; t/=10; return(t); }