求C8051F310单片机与DS18B20传感器用Keil c语言设计程序

如题所述

DS18B20温度传感器C51程序,无CRC

2008-06-30 11:18

/********************************************************

* DS18B20温度传感器 *

* C51 *

* yajou 2008-06-28 无CRC *

********************************************************/

#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
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-25
这个我没做,关键你的使用的是内部晶振,还是外部晶振,单总线重点要算这个时间,你没给我晶振频率我也没发给你写,具体配置完交叉开关后,一直C51的就可以,性质是一样的。就是注意C8051F的频率比较快