📄 ds18b20_2.h
字号:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint temp2; // variable of temperature
sbit DS2=P1^1; //define interface
void delay2(uchar count)
{
while(count>0) count--;
}
void reset2(void) //send reset and initialization command
{
DS2=0;
delay2(100);
DS2=1;
delay2(4);
delay2(200);
}
bit read_bit2(void) //read a bit
{
bit temp;
DS2=0;
_nop_();
DS2=1;
_nop_();
temp=DS2;
delay2(200);
return temp;
}
uchar read_byte2(void) //read a byte date
{
uchar i,byte=0;
bit j;
for(i=0;i<8;i++)
{
byte=_cror_(byte ,1);
j=read_bit2();
if(j==0) byte=byte|0x00;
else byte=byte|0x80;
}
return byte;
}
void write_byte2(uchar command) //write a byte to ds18b20
{
uchar i;
for(i=0;i<8;i++)
{
if((command & 0x01)==0)
{
DS2=0;
delay2(8);
DS2=1;
_nop_();
}
else
{
DS2=0;
_nop_();
DS2=1;
delay2(8);
}
command=_cror_(command,1);
}
}
void tmpchange2(void) //DS18B20 begin change
{
reset2();
write_byte2(0xcc); //直接向18b20发送温度变换命令
write_byte2(0x44); //启动18b20进行温度转换
}
uint tmp2() //get the temperature
{
float tt;
uchar a,b;
reset2();
write_byte2(0xcc); //直接向18b20发送温度变换命令
write_byte2(0xbe); //读取温度寄存器的温度值
a=read_byte2();//读低八位
b=read_byte2();//读高八位
temp2=b;
temp2<<=8; //two byte compose a int variable
temp2=temp2|a;
tt=temp2*0.0625;
temp2=tt*10+0.5;
return temp2 ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -