///////////////////////////////////////////////////////////////////
/*名稱:單片機讀取DS18b20時鐘芯片 /
/*作者:周光明 /
/*時間:2010年12月6日 /
/*功能: /
/* /
/* /
///////////////////////////////////////////////////////////////////*/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^3;
bit SIGN_FLAG;
////////////////////////
void yanshids18b20(uint t)
{
while(t--);
}
//////////////////////////初始化
chushids18b20()
{
uchar dushu=0;
DQ=1;
yanshids18b20(8);
DQ=0;
yanshids18b20(80);
DQ=1;
yanshids18b20(14);
dushu=DQ;
yanshids18b20(60);
return(dushu);
}
////////////////////////讀一個字節
duds18b20()
{
uchar i,j;
uchar temp=0;
for(i=0;i<8;i++)
{
DQ=0;
temp>>=1;
DQ=1;
for(j=0;j<3;j++);
if(DQ)temp|=0x80;
yanshids18b20(4);
}
return(temp);
}
/////////////////////////寫一個字節
xieds18b20(uchar shuju)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=shuju&0x01;
yanshids18b20(5);
DQ=1;
shuju>>=1;
}
yanshids18b20(1);
}
///////////////////////////////啟動溫度轉換
qidongds18b20()
{
chushids18b20();
if(~chushids18b20())
{
xieds18b20(0xcc);
xieds18b20(0x44);
}
}
/////////////////////////讀取溫度
duquds18b20()
{
uchar c1,c2;
uchar temp1,temp2,temp3;
float tt=0;
chushids18b20();
xieds18b20(0xcc);
xieds18b20(0xbe);
c1=duds18b20();
c2=duds18b20();
temp1=c1>>4;
temp2=c2<<4;
temp3=temp1+temp2;
if(temp2&0x80)
{
temp3=256-temp3;
SIGN_FLAG=1;
}
else SIGN_FLAG=0;
return(temp3);
}
/////////////////////////////////////