/*********************************************************************************************************
DS18B20驱动程序
李强 2011/5/12
V1.2
主函数需要调用 ReadTemVal(char *pStr)这个函数即可,函数已经将温度转换成字符串,保存在字符数组pStr中。
已对温度的正负问题处理,读取小数点后三位(可更改)。
*********************************************************************************************************/
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
sbit TemBus =P1^6; //读取温度总线
//短暂延时函数
void delay(unsigned int uiDly)
{
while(uiDly --);
}
/*********************************************************************************************************
**函数名称: DS18B20_Init()
**函数功能: DS18B20初始化(复位操作)
**函数参数: 无
**返回值 : 初始成功返回0(低脉冲),否则返回0
**********************************************************************************************************/
bit DS18B20_Init()//如果DS18B20初始化成功,芯片应答0
{
//每一次读写之前都要对DS18B20进行复位操作,复位成功后发送一条ROM指令,最后发送RAM指令,
//这样才能对DS18B20进行预定的操作。复位要求主CPU将数据线下拉500微秒,然后 释放,
//当DS18B20收到信号后等待16~60微秒左右,后发出60~240微秒的存在低脉冲,主CPU收到此信号表示复位成功。
bit result = 1;
TemBus = 1; //先将数据线置高电平“1”
delay(10);
TemBus = 0; //数据线拉到低电平“0”
delay(100); //延时约100us
TemBus = 1; //先将数据线置高电平“1”
delay(10);
result = TemBus; //如果初始化成功,result = 0;
delay(100); //延时约100us
TemBus = 1; //为后续动作作准备
return result;
}
/*****************************************************************************************************
**函数名称: PutBit
**函数功能: 发送一位数据
**函数参数: 需要发送的bit b
**返回值 : 无
******************************************************************************************************/
void PutBit(bit b)
{
TemBus = 0;
_nop_();
TemBus = b;
delay(10);
TemBus = 1;
_nop_();
}
/**************************************************************************************************
**函数名称: PutBye
**函数功能: 向总线发送一位数据
**函数参数: 需要发送的字节
**返回值 : 无
***************************************************************************************************/
void PutBye(unsigned char date)
{
unsigned char i;
bit b;
for(i=0;i<8;i++)
{
b = (bit)(date & 0x01) ;
date >>=1;
PutBit(b);
}
}
/**************************************************************************************************
**函数名称: Getbit()
**函数功能: 向总线接受一位数据
**函数参数: 无
**返回值 : 接受的bit
***************************************************************************************************/
bit GetBit()
{
bit b;
TemBus = 1; //拉高数据线
_nop_();
TemBus = 0; //拉低数据线
_nop_();
TemBus = 1; //拉高数据线
_nop_();
_nop_();
b = TemBus; //读取一位
return b;
}
/**************************************************************************************************
**函数名称: GetBye
**函数功能: 向总线接受一个字节 (读取一个字节,从温度值低位开始读)
**函数参数: 无
**返回值 : 接受的字节
***************************************************************************************************/
char GetBye()
{
unsigned char date=0,i;
for(i=0;i<8;i++)
{
date >>= 1;
delay(30);
if(GetBit())
date |= 0x80;
}
return date;
}
/*******************************************************************************************************
**函数名称: ReadTemVal
**函数功能: 读取温度,并且返回温度值
**函数参数: 无
**返回值 : 读取成功返回温度值
*******************************************************************************************************/
float ReadTemVal()
{
unsigned char TemL;
unsigned char TemH;
int i;
float f;
if(DS18B20_Init()) //初始化有误,返回0
return 0;
else
{
PutBye(0xCC); //忽略 64 位 ROM 地址,直接向 DS1820 发温度变换命令
PutBye(0x44); //启动DS1820进行温度转换
DS18B20_Init() ;
PutBye(0xCC); //忽略 64 位 ROM 地址,直接向 DS1820 发温度变换命令
PutBye(0xBE); //读内部RAM中9字节的内容
TemL= GetBye(); //温度低位
TemH = GetBye(); //温度高位
i = TemH<<8|TemL;
if(TemH<= 0x0F) //温度为正(按正规则转换)
{
f = i*0.0625;
return f;
}
else //温度为负(按负规则转换)
{
f =(~i+1)*0.0625;
return f*(-1);
}
}
}