📄 ds18b20.h
字号:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint temp; // variable of temperature
sbit DS=P1^0; //define interface
void delay1(int us)
{
int s;
for(s=0;s<us;s++);
}
void reset(void) //send reset and initialization command
{
DS=0;
delay1(100);
DS=1;
delay1(4);
delay1(200);
}
//********读位**************//
bit read_bit(void) //read a bit
{
unsigned char i;
DS=0;
_nop_(); //拉低大于1us
DS=1;
for(i=0;i<3;i++); //在这15us之内来判定数据为0为1
return(DS); //返回值为0就是0,为1就是1。单总线只能拉低,不能置高位
}
//********写位****************
void write_bit(unsigned char bitval)
{
DS=0;
_nop_(); //拉低大于1us
if(bitval==1) DS=1; //bitval为1就是写1,为0就是写0
delay1(5); //在15-60us之间进行写数据
DS=1;
}
//**********读字节*********//
uchar read_byte(void) //read a byte date
{
unsigned char i;
unsigned char byte=0;
for(i=0;i<8;i++)
{
if(read_bit()) byte|=0x01<<i;
delay1(6);
}
return(byte);
}
//**********写字节*********//
void write_byte(uchar command) //write a byte to ds18b20
{
unsigned char i;
unsigned char temp;
for(i=0;i<8;i++)
{
temp=command>>i;
temp&=0x01;
write_bit(temp);
}
delay(5);
}
void tmpchange(void) //DS18B20 begin change
{
reset();
write_byte(0xcc); //直接向18b20发送温度变换命令
write_byte(0x44); //启动18b20进行温度转换
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
reset();
write_byte(0xcc); //直接向18b20发送温度变换命令
write_byte(0xbe); //读取温度寄存器的温度值
a=read_byte();//读低八位
b=read_byte();//读高八位
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 + -