?? 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 + -