/****************************************Copyright (c)************************************************** ** ** 研 发 部 ** **--------------文件信息-------------------------------------------------------------------------------- **文 件 名: DS18B20.c **创 建 人: **最后修改日期: **描 述: ** **--------------历史版本信息---------------------------------------------------------------------------- ** 创建人: ** 版 本: ** 日 期: ** 描 述: ** **------------------------------------------------------------------------------------------------------ ** 修改人: ** 版 本: ** 日 期: ** 描 述: ** **--------------当前版本修订------------------------------------------------------------------------------ ** 修改人: ** 日 期: ** 描 述: ** **------------------------------------------------------------------------------------------------------ ********************************************************************************************************/ #include "DS18B20.h" /********************************************************************************************************* ** 全局变量 ********************************************************************************************************/ unsigned char Temperature; /********************************************************************************************************* ** 函数名称:Delay_us(unsigned char us) ** 函数功能:延时uS ** 入口参数: ** 出口参数: ** 说 明:us小于等于2时,延时2uS,系统时钟12.25M ********************************************************************************************************/ void Delay_us (unsigned char us) {
if (us <= 2) { _nop_(); _nop_(); _nop_(); _nop_(); return; } _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); us -= 3;
while(us) { us--; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ unsigned char OWTouchReset (void) { unsigned char result;
DQ = 0; // Drives DQ low Delay_us(Slot_H/2); Delay_us(Slot_H/2); DQ = 1; // Releases the bus Delay_us(Slot_I); result = DQ; // Sample for presence pulse from slave Delay_us(Slot_J/2); // Complete the reset sequence recovery Delay_us(Slot_J/2); return result; // Return sample presence pulse result } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ void OWWriteBit (unsigned char BitData) { if (BitData) { DQ = 0; // Drives DQ low Delay_us(Slot_A); DQ = 1; // Releases the bus Delay_us(Slot_B); } else { DQ = 0; // Drives DQ low Delay_us(Slot_C); DQ = 1; // Releases the bus Delay_us(Slot_D); } } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ unsigned char OWReadBit (void) { unsigned char result;
DQ = 0; // Drives DQ low Delay_us(Slot_A); DQ = 1; // Releases the bus Delay_us(Slot_E); result = DQ; // Sample the bit value from the slave Delay_us(Slot_F); // Complete the time slot and 10us recovery
return result; } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ void OWWriteByte (unsigned char Data) { unsigned char i;
// Loop to write each bit in the byte, LS-bit first for (i=8;i!=0;i--) { OWWriteBit(Data & 0x01);
// shift the data byte for the next bit Data = (Data >> 1); } } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ unsigned char OWReadByte (void) { unsigned char i,result=0;
for (i=8;i!=0;i--) { // shift the result to get it ready for the next bit result = (result >> 1);
// if result is one, then set MS bit if (OWReadBit()) result |= 0x80; } return result; } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ unsigned char DS18B20_Init (void) { EA = 0; ////////////////
if (OWTouchReset()) { EA = 1; //////////////// return 0xE0; }
OWWriteByte(OW_SkipROM); OWWriteByte(OW_WriteScratchpad); OWWriteByte(0x1E); // TH=30 OWWriteByte(0x19); // TL=25 OWWriteByte(0x3F); // DS18B20配置寄存器R1,R0=01
if (OWTouchReset()) { EA = 1; //////////////// return 0xE0; }
OWWriteByte(OW_SkipROM); OWWriteByte(OW_CopyScratchpad); EA = 1; ////////////////
return 0; } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ unsigned char ReadDS18B20 (void) { unsigned char i,result=0;
EA = 0; ////////////////
if (OWTouchReset()) { EA = 1; //////////////// return 0xE0; }
OWWriteByte(OW_SkipROM); OWWriteByte(OW_ReadScratchpad); for (i=9;i!=0;i--) { result = OWReadByte(); if (9 == i) { Temperature = ((result >> 2) & 0x3F);// 温度保留2bit小数 } else if (8 == i) { if (result & 0xF8) { Temperature = 0; // 负温度归零 } else if (result & 0x04) { Temperature = 0xFF; // 温度超64饱和 } else { Temperature |= ((result << 6) & 0xC0); // 温度保留6bit整数 } } } EA = 1; ////////////////
return 0; } /********************************************************************************************************* ** 函数名称: ** 函数功能: ** 入口参数: ** 出口参数: ** 说 明: ********************************************************************************************************/ void DS18B20ConvT (void) { EA = 0; //////////////// if (OWTouchReset()) { EA = 1; //////////////// return; }
OWWriteByte(OW_SkipROM); OWWriteByte(OW_ConvertT);
EA = 1; //////////////// }
|