基于STC89C52RC单片机DS18B20温度传感器实例源代码
/******************************************************************************/
// 函数名称:Ds18b20_Get_Temp
// 输入参数:*tmpValue-存储温度结果
// 输出参数:无
// 函数功能:将读到的温度存放到*tempVlue中
/******************************************************************************/
void Ds18b20_Get_Temp(uint8 *tempValue){
uint8 valueLow,valueHigh;
uint16 resultLow; //范围超过了255
Ds18b20_Init();
Ds18b20_Write_Cmd(0xcc);//skip rom
Ds18b20_Write_Cmd(0x44);//convert T
//Delay750ms();//busycheck
Ds18b20_Busy_Check();
Ds18b20_Init();
Ds18b20_Write_Cmd(0xcc);//skip rom
Ds18b20_Write_Cmd(0xbe);//read scratchpad
valueLow = Ds18b20_Read_Data();//read byte0
valueHigh = Ds18b20_Read_Data();//read byte1
resultLow = (valueLow & 0x0f) * 625;
valueLow >>= 4;
*(tempValue+0) = (valueHigh *16 + valueLow)/100 + 0x30; //温度值的百位
*(tempValue+1) = (valueHigh *16 + valueLow)%100/10 + 0x30;//温度值的十位
*(tempValue+2) = (valueHigh *16 + valueLow)%100%10 + 0x30;//温度值的个位
*(tempValue+4) = resultLow/1000 + 0x30; //小数点后第一位
*(tempValue+5) = resultLow%1000/100 + 0x30; //小数点后第二位
*(tempValue+6) = resultLow%1000%100/10 + 0x30; //小数点后第三位
*(tempValue+7) = resultLow%1000%100%10 + 0x30; //小数点后第四位
}
/******************************************************************************/
// 函数名称:Ds18b20_Busy_Check
// 输入参数:无
// 输出参数:无
// 函数功能:DS18B20忙信号检测
/******************************************************************************/
void Ds18b20_Busy_Check(void){
bit flag; //0-busy 1-done
do{
DQ = 0;
_nop_();
_nop_();
DQ = 1;
_nop_();
_nop_();
flag = DQ;
Delay68us();
led = !flag;
}while(!flag);
}
/******************************************************************************/
// 函数名称:Ds18b20_Init
// 输入参数:无
// 输出参数:返回0表示检测到DS18B20
// 函数功能:DS18B20初始化
/******************************************************************************/
bit Ds18b20_Init(void){
bit flag;
DQ = 0;
Delay500us();
DQ = 1;
Delay68us();
flag = DQ;
Delay500us();
return flag;
}
/******************************************************************************/
// 函数名称:Ds18b20_Write_Low
// 输入参数:无
// 输出参数:无
// 函数功能:DS18B20写0
/******************************************************************************/
void Ds18b20_Write_Low(void){
DQ = 0;
Delay68us();
DQ = 1;
_nop_();
_nop_();
}
/******************************************************************************/
// 函数名称:Ds18b20_Write_High
// 输入参数:无
// 输出参数:无
// 函数功能:DS18B20写1
/******************************************************************************/
void Ds18b20_Write_High(void){
DQ = 0;
_nop_();
_nop_();
DQ = 1;
Delay68us();
}
/******************************************************************************/
// 函数名称:Ds18b20_Write_Cmd
// 输入参数:myCmd-命令
// 输出参数:无
// 函数功能:DS18B20写命令
/******************************************************************************/
void Ds18b20_Write_Cmd(uint8 cmd){
uint8 i;
for(i=0;i<8;i++){
if(cmd&0x01)Ds18b20_Write_High();
else Ds18b20_Write_Low();
cmd >>= 1;
}
}
/******************************************************************************/
// 函数名称:Ds18b20_Read_Data
// 输入参数:无
// 输出参数:返回温度值
// 函数功能:从DS18B20读数据
/******************************************************************************/
uint8 Ds18b20_Read_Data(void){
uint8 tmpRead=0x00;
uint8 i;
for(i=0;i<8;i++){
tmpRead>>=1;
DQ = 0;
_nop_();
_nop_();
DQ = 1;
_nop_();
_nop_();
if(DQ)tmpRead|=0x80;//读1
//else tmpRead|=0x00;//读0
Delay68us();
}
return tmpRead;
}
更多推荐



所有评论(0)