请选择 进入手机版 | 继续访问电脑版
查看: 2213|回复: 1

51的DS18B20的C程序

[复制链接]
发表于 2015-9-23 21:30:36 | 显示全部楼层 |阅读模式
#include "reg51.h"
#include "intrins.h"
#include "DS18B20.h"

/********************************************************
* us延时程序                                                 *
********************************************************/
void Delayus(uchar us)   
{
    while(us--); //12M,一次6us,加进入退出14us(8M晶振,一次9us)  
}

/********************************************************
* DS18B20初始化                                         *
********************************************************/
bit Ds18b20_Init(void) //存在返0,否则返1
{
bit temp = 1;
uchar outtime = ReDetectTime; //超时时间
while(outtime-- && temp)
{
   Delayus(10); //(250)1514us时间可以减小吗
   ReleaseDQ();
   Delay2us();
   PullDownDQ();
   Delayus(100); //614us(480-960)
   ReleaseDQ();
   Delayus(10); //73us(>60)
   temp = dq;
   Delayus(70); //us
}
return temp;
}
/********************************************************
* 写bit2DS18B20                                        *
********************************************************/
void Ds18b20_WriteBit(bit bitdata)
{
if(bitdata)
{
   PullDownDQ();
   Delay2us();   //2us(>1us)
   ReleaseDQ(); //(上述1-15)
   Delayus(12); //86us(45- x,总时间>60)
}else
{
   PullDownDQ();
   Delayus(12); //86us(60-120)
}
ReleaseDQ();
Delay2us();    //2us(>1us)
}
/********************************************************
* 写Byte DS18B20                                       *
********************************************************/
void Ds18b20_WriteByte(uchar chrdata)
{
uchar ii;
for(ii = 0; ii < 8; ii++)
{
   Ds18b20_WriteBit(chrdata & 0x01);
   chrdata >>= 1;
}
}
/********************************************************
* 写 DS18B20                                              *
********************************************************/
//void Ds18b20_Write(uchar *p_readdata, uchar bytes)
//{
// while(bytes--)
// {
//   Ds18b20_WriteByte(*p_readdata);
//   p_readdata++;
// }
//}

/********************************************************
* 读bit From DS18B20                                 *
********************************************************/
bit Ds18b20_ReadBit(void)
{
bit bitdata;
PullDownDQ();
Delay2us();   //2us( >1us)
ReleaseDQ();
Delay8us();   //8us( <15us)
bitdata = dq;
Delayus(7); //86us(上述总时间要>60us)
return bitdata;
}

/********************************************************
* 读Byte DS18B20                                      *
********************************************************/
uchar Ds18b20_ReadByte(void)
{
uchar ii,chardata;
for(ii = 0; ii < 8; ii++)
{
   chardata >>= 1;
   if(Ds18b20_ReadBit()) chardata |= 0x80;
}
return chardata;
}

/********************************************************
* 读 DS18B20 ROM                                    *
********************************************************/
bit Ds18b20_ReadRom(uchar *p_readdata) //成功返0,失败返1
{
uchar ii = 8;
if(Ds18b20_Init()) return 1;
Ds18b20_WriteByte(ReadROM);
while(ii--)
{
   *p_readdata = Ds18b20_ReadByte();
   p_readdata++;
}
return 0;
}

/********************************************************
* 读 DS18B20 EE                                        *
********************************************************/
bit Ds18b20_ReadEE(uchar *p_readdata) //成功返0,失败返1
{
uchar ii = 2;
if(Ds18b20_Init()) return 1;
Ds18b20_WriteByte(SkipROM);
Ds18b20_WriteByte(ReadScr);
while(ii--)
{
   *p_readdata = Ds18b20_ReadByte();
   p_readdata++;
}
return 0;
}

/********************************************************
* 温度采集计算                                             *
********************************************************/
bit TempCal(float *p_wendu) //成功返0,失败返1 (温度范围-55 --- +128)
{
uchar temp[9],ii;
uint tmp;
float tmpwendu;
TR1 = 0;
TR0 = 0;
//读暂存器和CRC值-----------------------
if(Ds18b20_ReadEE(temp))
{
   TR1 = 1;
   TR0 = 1;
   return 1;
}
//-------------------------------------
    //CRC校验------------------------------
//
//此处应加入CRC校验等
//
//
//-------------------------------------

//使温度值写入相应的wendu[i]数组中-----
for(ii = i; ii > 0; ii--)  
{
   p_wendu++;
}
i++;
if(i > 4) i = 0;
//-------------------------------------

//温度正负数处理-----------------------
//
//-------------------------------------

//温度计算-----------------------------
tmp = temp[1];   //
tmp <<= 8;    //
tmp |= temp[0];   //组成温度的两字节合并
tmpwendu = tmp;
*p_wendu = tmpwendu / 16;
//-------------------------------------

//开始温度转换-------------------------
if(Ds18b20_Init())
{
   TR1 = 1;
   TR0 = 1;
   return 1;
}
Ds18b20_WriteByte(SkipROM);
Ds18b20_WriteByte(Convert);
ReleaseDQ(); //寄生电源时要拉高DQ
//------------------------------------
TR1 = 1;
TR0 = 1;
return 0;
}
//////////DS18B20.h/////////////////////////
/********************************************************
* I/O口定义                                                    *
********************************************************/
sbit dq = P1^3;
sbit dv = P1^4; //DS18B20强上拉电源

/********************************************************
* 命令字定义                                                 *
********************************************************/
#define uchar unsigned char
#define uint unsigned int

#define ReleaseDQ()   dq = 1;   //上拉/释放总线
#define PullDownDQ() dq = 0;   //下拉总线
#define Delay2us()   _nop_();_nop_(); //延时2us,每nop 1us
#define Delay8us()   _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
//设置重复检测次次数,超出次数则超时
#define     ReDetectTime    20

//ds18b20命令
#define     SkipROM      0xCC
#define     MatchROM     0x55
#define     ReadROM      0x33
#define     SearchROM    0xF0
#define     AlarmSearch 0xEC
#define     Convert      0x44
#define     WriteScr     0x4E
#define     ReadScr      0xBE
#define     CopyScr      0x48
#define     RecallEE     0xB8
#define     ReadPower    0xB4

/********************************************************
* 函数                                                            *
********************************************************/
void Delayus(uchar us);   
//void Dog(void);
bit Ds18b20_Init(void); //DS18B20初始化,存在返0,否则返1
void Ds18b20_WriteBit(bit bitdata);   //写bit2DS18B20
void Ds18b20_WriteByte(uchar chrdata); //写Byte DS18B20
void Ds18b20_Write(uchar *p_readdata, uchar bytes); //写 DS18B20
bit Ds18b20_ReadBit(void);   //读bit From DS18B20
uchar Ds18b20_ReadByte(void); //读Byte DS18B20
bit Ds18b20_ReadRom(uchar *p_readdata); //读 DS18B20 ROM:成功返0,失败返1
bit Ds18b20_ReadEE(uchar *p_readdata); //读 DS18B20 EE :成功返0,失败返1
bit TempCal(float *p_wendu); //成功返0,失败返1 (温度范围-55 --- +128)

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入因仑

本版积分规则

快速回复 返回顶部 返回列表