📄 ds18b20.c
字号:
/******************************************************************
DS18B20.c file
作者:Computer-lov
建立日期:2008-05-18
修改日期:2008-05-18
版本:V1.0
版权所有,盗版必究。
Copyright(C) Computer-lov 2008-2018
All rights reserved
********************************************************************/
#include "at89x52.h"
#include "uart.h"
#define DS_DQ P1_6
uint8 DS18B20Rom[8];
int16 Temperature;
/********************************************************************
函数功能:延迟2*Xms函数。
入口参数:X。
返 回:无。
备 注:在11.0592MHz晶体下,延时约2倍Xms。
********************************************************************/
void DelayX2us(uint8 x)
{
while(--x);
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:DS18B20函数。
入口参数:无。
返 回:0:复位失败;1:复位成功。
备 注:复位失败可能是因为没有设备。
********************************************************************/
uint8 DS18B20Reset(void)
{
DS_DQ=0;
DelayX2us(250);
DS_DQ=1;
DelayX2us(26);
if(DS_DQ)
{
return 0; //没有应答
}
else //有应答
{
while(!DS_DQ); //等待应答信号完毕
}
return 1;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:往DS18B20写一字节函数。
入口参数:需要写入的数据。
返 回:无。
备 注:无。
********************************************************************/
void DS18B20WriteByte(uint8 d)
{
uint8 i;
for(i=0;i<8;i++)
{
DS_DQ=0;
DelayX2us(2);
if(d&0x01)
{
DS_DQ=1;
}
else
{
DS_DQ=0;
}
DelayX2us(30);
DS_DQ=1;
d>>=1;
DelayX2us(1);
}
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:DS18B20读一字节函数。
入口参数:无。
返 回:读回的一字节数据。
备 注:无。
********************************************************************/
uint8 DS18B20ReadByte(void)
{
uint8 i;
uint8 d;
for(i=0;i<8;i++)
{
d>>=1;
DS_DQ=0;
DelayX2us(4);
DS_DQ=1;
DelayX2us(4);
if(DS_DQ)
{
d+=0x80;
}
DelayX2us(20);
}
return d;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:读取DS18B20的64bit ROM函数。
入口参数:无。
返 回:无。
备 注:结果保存在全局变量DS18B20Rom数组中。
********************************************************************/
void DS18B20ReadRom(void)
{
uint8 i;
EA=0;
if(!DS18B20Reset()) //复位失败
{
EA=1;
Prints("\r\nDS18B20 Reset failed. Maybe there no DS18B20 device.\r\n");
return; }
DS18B20WriteByte(0x33);
for(i=0;i<8;i++)
{
DS18B20Rom[7-i]=DS18B20ReadByte();
}
EA=1;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:写DS18B20Scratchpad函数。
入口参数:无。
返 回:无。
备 注:设置为12位分辨率。
********************************************************************/
void DS18B20WriteScratchpad(void)
{
EA=0;
if(!DS18B20Reset()) //复位失败
{
EA=1;
return; }
DS18B20WriteByte(0xCC); //Skip ROM
DS18B20WriteByte(0x4E); //Write Scratchpad command
DS18B20WriteByte(0x00); //TH
DS18B20WriteByte(0x00); //TL
DS18B20WriteByte(0x7F); //12位分辨率
EA=1;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:读DS18B20 Scratchpad函数。
入口参数:无。
返 回:无。
备 注:读出温度值保存在全局变量Temperature中。
********************************************************************/
void DS18B20ReadScratchpad(void)
{
EA=0;
if(!DS18B20Reset()) //复位失败
{
EA=1;
return; }
DS18B20WriteByte(0xCC); //Skip ROM
DS18B20WriteByte(0xBE); //Read Scratchpad command
Temperature=DS18B20ReadByte(); //TEMPERATURE LSB
Temperature|=((uint16)(DS18B20ReadByte()))<<8; //TEMPERATURE MSB
EA=1;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:启动DS18B20转换函数。
入口参数:无。
返 回:无。
备 注:无。
********************************************************************/
void DS18B20ConvertT(void)
{
EA=0;
if(!DS18B20Reset()) //复位失败
{
EA=1;
return; }
DS18B20WriteByte(0xCC); //Skip ROM
DS18B20WriteByte(0x44); //ConvertT command
EA=1;
}
////////////////////////End of function//////////////////////////////
/********************************************************************
函数功能:DS18B20初始化函数。
入口参数:无。
返 回:无。
备 注:读取ROM,设置12位分辨率。
********************************************************************/
void DS18B20Init(void)
{
DS18B20ReadRom(); //读ID号
DS18B20WriteScratchpad(); //
DS18B20ConvertT();
}
////////////////////////End of function//////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -