📄 ds18b20.c
字号:
/********************************************************************************/
/* filename: DS18B20.c */
/* created : xillinx */
/* time : 2008-08-20 */
/* descript: 处理温度传感器的相关函数 */
/********************************************************************************/
#include "../CPU/CPU.h"
#include "../DS18B20/DS18B20.h"
unsigned char DS18B20_mflag;
void DSB20_initialize(void)
{ DS18B20_DQ=0x1;
}
/********************************************************************************/
/* funname : DSB20_delay_02_us() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 44.2368M的频率下,NOP是0.27US,02US需要7个NOP */
/********************************************************************************/
void DSB20_delay_02_us(void)
{ _nop_(); //* 0
_nop_();
_nop_();
_nop_();
_nop_();
}
/********************************************************************************/
/* funname : DSB20_delay_05_us() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 44.2368M的频率下,NOP是0.27US,05US需要18个NOP */
/********************************************************************************/
void DSB20_delay_05_us(void)
{ _nop_(); //* 0
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_(); //* 10
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_(); //* 18
}
/********************************************************************************/
/* funname : DSB20_delay_10_us() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 44.2368M的频率下,NOP是0.27US,10US需要37个NOP */
/********************************************************************************/
void DSB20_delay_10_us(void)
{ _nop_(); //* 0
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_(); //* 10
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_(); //* 20
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_(); //* 30
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_(); //* 37
}
/********************************************************************************/
/* funname : DSB20_delay_15_us() */
/* created : xillinx */
/* time : 2008-08-06 */
/********************************************************************************/
void DSB20_delay_15_us(void)
{ DSB20_delay_05_us();
DSB20_delay_10_us();
}
/********************************************************************************/
/* funname : DSB20_delay_40_us() */
/* created : xillinx */
/* time : 2008-08-06 */
/********************************************************************************/
void DSB20_delay_40_us(void)
{ DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
}
/********************************************************************************/
/* funname : DSB20_delay_100_us() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 直接调用10US函数来实现 */
/********************************************************************************/
void DSB20_delay_100_us(void)
{ DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
DSB20_delay_10_us();
}
/********************************************************************************/
/* funname : DSB20_delay_1000_us() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 直接调用100US函数来实现 */
/********************************************************************************/
void DSB20_delay_1000_us(void)
{ DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
}
/********************************************************************************/
/* funname : DSB20_reset_chip() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: 复位DS18B20芯片进行初始化 */
/********************************************************************************/
void DSB20_reset_chip(void)
{ DS18B20_DQ=0x0;
DSB20_delay_100_us(); //* 600US
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DSB20_delay_100_us();
DS18B20_DQ=0x1;
DSB20_delay_05_us();
while(DS18B20_DQ==0x1) //* 等待DS18B20_DQ变低
{ ;
}
while(DS18B20_DQ==0x0) //* 等待DS18B20_DQ变低
{ ;
}
DSB20_delay_100_us(); //* 等待信号
DSB20_delay_100_us();
}
/********************************************************************************/
/* funname : DSB20_write_1_time() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片写入数字'1' */
/********************************************************************************/
void DSB20_write_1_time(void)
{ DS18B20_DQ=0x0;
DSB20_delay_02_us(); //* 启动信号
DS18B20_DQ=0x1;
DSB20_delay_15_us(); //* 写信号阶段
DSB20_delay_40_us(); //* 检测阶段
DS18B20_DQ=0x1;
DSB20_delay_10_us(); //* 位间隔
}
/********************************************************************************/
/* funname : DSB20_write_0_time() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片写入数字'0' */
/********************************************************************************/
void DSB20_write_0_time(void)
{ DS18B20_DQ=0x0;
DSB20_delay_02_us(); //* 启动信号
DS18B20_DQ=0x0;
DSB20_delay_15_us(); //* 写信号阶段
DSB20_delay_40_us(); //* 检测阶段
DS18B20_DQ=0x1;
DSB20_delay_10_us(); //* 位间隔
}
/********************************************************************************/
/* funname : DSB20_write_1_bit() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片写入一位数据 */
/********************************************************************************/
void DSB20_write_1_bit(unsigned char mdata)
{ if(mdata&0x01) //* 写 1
{ DSB20_write_1_time();
}
else //* 写 0
{ DSB20_write_0_time();
}
}
/********************************************************************************/
/* funname : DSB20_write_8_bit() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片写入一字节数据 */
/********************************************************************************/
void DSB20_write_8_bit(unsigned char mdata)
{ unsigned char i;
for(i=0;i<8;i++)
{ DSB20_write_1_bit(mdata&0x1);
mdata>>=0x1;
}
}
/********************************************************************************/
/* funname : DSB20_read_1_bit() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片读取一位数据 */
/********************************************************************************/
unsigned char DSB20_read_1_bit(void)
{ unsigned char mdata;
DS18B20_DQ=0x0; //* 发起请求
DSB20_delay_05_us();
DS18B20_DQ=0x1; //* 结束请求
DSB20_delay_05_us(); //* 等待5US后采样数据
mdata=DS18B20_DQ; //* 采样数据
DSB20_delay_40_us(); //* 等待60US后启动第二次
DSB20_delay_10_us(); //* 位间隔
DSB20_delay_10_us(); //* 位间隔
return mdata;
}
/********************************************************************************/
/* funname : DSB20_read_8_bit() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片读取8位数据 */
/********************************************************************************/
unsigned char DSB20_read_8_bit(void)
{ unsigned char mdata=0x0;
unsigned char i;
for(i=0;i<8;i++)
{ mdata>>=0x1;
if(DSB20_read_1_bit()==0x1)
{ mdata|=0x80;
}
}
return mdata;
}
/********************************************************************************/
/* funname : DSB20_start_convert() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片读取8位数据 */
/********************************************************************************/
void DSB20_start_convert(void)
{ DSB20_reset_chip();
DSB20_write_8_bit(0xCC); //* 跳过ROM
DSB20_write_8_bit(0x44); //* 开始转换温度
}
/********************************************************************************/
/* funname : DSB20_read_temperature() */
/* created : xillinx */
/* time : 2008-08-06 */
/* descript: DS18B20芯片读取转换的温度数据 */
/* 缺省的启动转换后需要750MS才能够读取温度数据 */
/********************************************************************************/
unsigned int DSB20_read_temperature(void)
{ unsigned char hi,lo;
DSB20_reset_chip(); //* 初始化
DSB20_write_8_bit(0xCC); //* 跳过ROM
DSB20_write_8_bit(0xBE); //* 读存储器
lo=DSB20_read_8_bit();
hi=DSB20_read_8_bit();
return (hi<<8)+lo;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -