温度传感器DS18B20驱动程序Word格式.docx

上传人:b****4 文档编号:16360789 上传时间:2022-11-23 格式:DOCX 页数:11 大小:16.68KB
下载 相关 举报
温度传感器DS18B20驱动程序Word格式.docx_第1页
第1页 / 共11页
温度传感器DS18B20驱动程序Word格式.docx_第2页
第2页 / 共11页
温度传感器DS18B20驱动程序Word格式.docx_第3页
第3页 / 共11页
温度传感器DS18B20驱动程序Word格式.docx_第4页
第4页 / 共11页
温度传感器DS18B20驱动程序Word格式.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

温度传感器DS18B20驱动程序Word格式.docx

《温度传感器DS18B20驱动程序Word格式.docx》由会员分享,可在线阅读,更多相关《温度传感器DS18B20驱动程序Word格式.docx(11页珍藏版)》请在冰豆网上搜索。

温度传感器DS18B20驱动程序Word格式.docx

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

BOOLInitDs18b20(void)

{

BOOLflag=FALSE;

BITbt;

bt=ET0;

ET0=0;

DQ=1;

Delay70us

(1);

//稍作延时

DQ=0;

//复位

Delay70us(8);

//精确延时大于480us

//拉高总线,等待存在脉冲

//60-75us之间

if(0==DQ)

{

flag=TRUE;

}

//复位后大于480us延时

ET0=bt;

returnflag;

}

ReadByte

从DS18B20中读取一个字节的数据

读取的数据

UCHARReadByte(void)

UCHARi;

UCHARdat=0;

for(i=0;

i<

8;

i++)

dat>

>

=1;

DQ=0;

Delay5us();

DQ=1;

if(1==DQ)

{

dat|=0x80;

}

Delay70us

(1);

return(dat);

WriteByte

向DS18B20中写入一个字节的数据

写入的数据*

voidWriteByte(UCHARwDat)

for(i=0;

i<

i++)

DQ=0;

if((wDat&

0x01)!

=0)

DQ=1;

else

wDat>

ReadDs18b20

从DS18B20储存器中读取多字节数据

读取数据存入的地址和需要读取数据的数量*

voidReadScrat(UCHAR*prDat,UCHARnum)

for(i=0;

num;

*prDat=ReadByte();

prDat++;

}

ReadTemp

读取温度

读取的温度存入的地址*

成功返回1,否则返回0

BOOLReadTemp(float*prTemperature)

UCHARcounter=0;

UCHARtempBuff[9];

floattempDat=0;

ClrWDT();

while

(1)

if(InitDs18b20())

counter=0;

break;

counter++;

if(counter>

=ERR_COUNTER)//连续3次初始化失败,即认为DS18B20出现故障

{

return0;

}

WriteByte(SKIP_ROM);

//跳过读序号列号的操作

WriteByte(CONVERT_T);

//启动温度转换

Delay70us(20);

//延时至少500us,以供温度转换

=ERR_COUNTER)//连续3次初始化失败,即认为DS18B20出现故障

//跳过读序号列号的操作

WriteByte(READ_SCRAT);

//读取温度寄存器等(共可读9个寄存器)

ReadScrat(tempBuff,9);

if(0==CRC8(tempBuff,9))

if(0==(tempBuff[1]&

0x80))//为正温度

tempDat=(tempBuff[1]&

0x07)*0x100+tempBuff[0];

tempDat=((0xFF-tempBuff[1])&

0x07)*0x100+(0x100-tempBuff[0]);

tempDat=-tempDat;

*prTemperature=tempDat*0.0625;

returnTRUE;

returnFALSE;

GetDS18B20Temperature

2009/8/17

采样3次求均值作为温度值

温度储存的地址*

正常为1,故障为0

BOOLGetDS18B20Temperature(float*pgTemperature)

floattempVal=0;

3;

if(ReadTemp(&

tempDat))

{

tempVal+=tempDat;

i++;

=ERR_COUNTER)//连续10次温度采样错误,则判断为故障,并返回

returnFALSE;

*pgTemperature=tempVal/3;

//SendStr("

boxTemp:

"

);

//print(*pgTemperature,1);

//SendChar('

\n'

returnTRUE;

CRC8

CRC效验

需要效验的数据地址和个数*

效验结果

UCHARCRC8(UCHAR*point,UCHARCRClen)//效验成功则crc返回0

{

UCHARi,j,crc_8,crcbuff;

crc_8=0;

CRClen;

crcbuff=*point++;

for(j=0;

j<

j++)

{

if(((crc_8^crcbuff)&

0x01)==0)

crc_8>

else

{

crc_8^=0x18;

crc_8>

crc_8|=0x80;

}

crcbuff>

returncrc_8;

Delay5us

延时5us

voidDelay5us(void)

//UCHARi;

//for(i=0;

7;

i++);

_nop_();

#endif

Delay70us

延时70us

次数*

voidDelay70us(UCHARtimers)

UCHARi,j;

for(j=0;

timers;

j++)

for(i=0;

22;

//Delay5us();

_nop_();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 生产经营管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1