📄 18b20.h
字号:
/********************18b20driver********************/
/****************designed by zhangbiao**************/
/*********************2007.8.8**********************/
//0x33 读rom 0x55 匹配rom 0xcc 跳过rom 0xf0 搜索rom 0xec 告警搜索
//0x4e 写暂存存储器 0xbe 读暂存存储器 0x48 复制暂存存储器 0x44 温度变换 0xb8 重新调出 0xb4 读电源
sbit DS=P3^5;
unsigned int temp;
void delay18b20(unsigned int count) //delay18b20 延时子程序
{
unsigned int i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void reset(void) // 发送初始化命令子程序
{
unsigned int i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit readbit(void)
{ unsigned char i;
bit dat;
DS=0;i++;
DS=1;i++;i++;
dat=DS;
i=8;while(i>0) i--;
return(dat);
}
unsigned char read(void)
{ unsigned char i,j,dat=0;
for(i=1;i<=8;i++)
{ j=readbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void write(unsigned char dat)
{ unsigned i,j;
bit t;
for(j=1;j<=8;j++)
{
t=dat&0x01;
dat=dat>>1;
if(t)
{ DS=0;i++;i++;
DS=1;
i=8;while(i>0) i--;}
else
{ DS=0;
i=8;while(i>0) i--;
DS=1;i++;i++;
}
}
}
void change()
{ reset();
delay18b20(1);
write(0xcc);
write(0x44);
}
float tmp() // 得到温度值
{
float tt;
unsigned char a,b;
reset();
delay18b20(1);
write(0xcc);
write(0xbe);
a=read();
b=read();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
return(tt);
}
void readrom() // 读取温度传感器的序列号子函数
{
unsigned char sn1,sn2;
reset();
delay18b20(1);
write(0x33);
sn1=read();
sn2=read();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -