📄 18b20.h
字号:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2;
sbit diola=P2^5; //define interface
void delay1(uchar count)
{
while(count>0) count--;
}
void dsreset(void) //send reset and initialization command
{
DS=0;
delay1(100);
DS=1;
delay1(204);
}
bit tmpreadbit(void) //read a bit
{
bit temp;
DS=0;
_nop_();
DS=1;
_nop_();
temp=DS;
delay1(8);
return temp;
}
uchar tmpread(void) //read a byte date
{
uchar i,byte=0;
bit j;
for(i=0;i<8;i++)
{
byte=_cror_(byte,1);
j=tmpreadbit();
if(j==0) byte=byte|0x00;
else byte=byte|0x80;
}
return byte;
}
void tmpwritebyte(uchar command) //write a byte to ds18b20
{
uchar i;
for(i=0;i<8;i++)
{
if((command & 0x01)==0)
{
DS=0;
delay1(8);
DS=1;
_nop_();
}
else
{
DS=0;
_nop_();
DS=1;
delay1(8);
}
command=_cror_(command,1);
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
uint temp; // variable of temperature
dsreset();
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -