专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

DS18B20 数字温度传感程序

作者:佚名   来源:本站原创   点击数:  更新时间:2010年12月12日   【字体:
///////////////////////////////////////////////////////////////////
/*名称:单片机读取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);
}
/////////////////////////////////////
关闭窗口

相关文章