DS18B20程序
单片机
#include "18B20.h"
unsigned char TTest()
{
unsigned char a,b,t1,t2;
Reset_18B20(); //复位总线
TS_Write_Byte(0xCC); //忽略ROM匹配操作
TS_Write_Byte(0x44); //温度转换命令
Delay60();
Delay60();
Delay60();
Delay60();
Delay60();
Reset_18B20();
TS_Write_Byte(0xCC); //忽略ROM匹配操作
TS_Write_Byte(0xBE); //读取RAM寄存器
a=TS_Read_Byte(); //读取第一字节
b=TS_Read_Byte(); //读取第二字节
t1=b<<4;
t1=t1|(a&0xf0)>>4;
t2=(a&0x0f)*0.625;
return t1;
}
void Delay15()
{
unsigned char i;
for(i=8;i>0;i--);
}
void Delay60()
{
unsigned char i;
for(i=30;i>0;i--);
}
void Reset_18B20(void)
{
unsigned char i;
DQ = 0; //单片机将DQ拉低
for(i=0;i<8;i++) //精确延时大于480us 小于960us Delay60();
DQ = 1; //拉高总线
_nop_();
while(DQ); //15~60us 后接收60-240us的存在脉冲


