📄 ds18b20.h
字号:
/****************************DS18B20_H****************************/
#ifndef __DS18B20_H__
#define __DS18B20_H__
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
unsigned char databuff[] = {0,0};
unsigned char temp;
sbit DQ18B20 = P2^7;
void Delayus(unsigned int t)
{
for (;t>0;t--);
}
void Write_18b20(uchar val)
{
uchar i;
for(i=8;i>0;i--)
{
DQ18B20=1;_nop_();_nop_(); //从高拉倒低
DQ18B20=0;_nop_();_nop_();_nop_();_nop_(); //5 us
DQ18B20=val&0x01; //最低位移出
Delayus(5); //55 us 15us~120us
val=val/2; //右移1位
}
DQ18B20=1;
Delayus(1);
}
/*void Write_18b20(unsigned char ddata) //写18B20数据
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ18B20=1;_nop_();_nop_(); //从高拉倒低
DQ18B20 = 0;
_nop_();_nop_();_nop_();_nop_(); //5 us
DQ18B20 = ddata&0x01;
ddata=ddata >> 1;
Delayus(6); // Delay68us
DQ18B20 = 1;
}
DQ18B20 = 1;
Delayus(1); // Delay 18us
}
/*Write_18b20(unsigned char WriteData)
{
unsigned char i;
unsigned char tmpData;
for(i=0;i<8;i++)
{
tmpData=WriteData&0x01;
WriteData>>=1;
if(tmpData)
{
DQ18B20=0;
DQ18B20=1;
Delayus(4);
}
else
{
DQ18B20=0;
Delayus(4);
DQ18B20=1;
}
}
}
unsigned char Read_18b20(void) //读18B20
{
unsigned char i,temp;
for(i=0;i<8;i++)
{
DQ18B20 =1;
temp=temp>>1;
DQ18B20 = 0;
Delayus(1);
DQ18B20 =1;
Delayus(1);
if(DQ18B20 ==0)
{
temp=temp&0x7f;
}
else
{
temp=temp|0x80;
}
Delayus(5); //Delay 58us
}
DQ18B20=1;
return temp;
}
/*
//读一个字节
unsigned char Read_18b20()
{
unsigned char i;
unsigned char ReadData=0;
for(i=0;i<8;i++)
{
DQ18B20=0;
ReadData>>=1;
DQ18B20=1;
if(DQ18B20)
ReadData|=0x80;
Delayus(4);
}
return ReadData;
} */
unsigned char Read_18b20(void)
{
unsigned char i;
unsigned char value=0;
for(i=8;i>0;i--)
{
DQ18B20=1;_nop_();_nop_();
value>>=1;
DQ18B20=0;_nop_();_nop_();_nop_();_nop_(); //4 us
DQ18B20=1;_nop_();_nop_();_nop_();_nop_(); //4 us
if(DQ18B20)value|=0x80;
Delayus(6); //66 us
}
DQ18B20=1;
return(value);
}
/*void Init_18b20(void) //18b20的初始化
{
DQ18B20 =0;
Delayus(50); //Delay 508us
DQ18B20 =1;
Delayus(5); //Delay 108us
while(DQ18B20);
Delayus(10); //Delay 108us
DQ18B20 =1;
}*/
void Init_18b20()
{
DQ18B20=0;
Delayus(60); //下拉500us
DQ18B20=1; //释放总线
Delayus(6);
while(DQ18B20); //等待应答信号
while(~DQ18B20); //等待释放总线
}
/*Init_18b20(void)
{
char presence=1;
while(presence)
{
while(presence)
{
DQ18B20=1;_nop_();_nop_();//从高拉倒低
DQ18B20=0;
Delayus(50); //550 us
DQ18B20=1;
Delayus(6); //66 us
presence=DQ18B20; //presence=0 复位成功,继续下一步
}
Delayus(45); //延时500 us
presence=~DQ18B20;
}
DQ18B20=1; //拉高电平
} */
unsigned char Get_temp_value() //获取温度值
{
Init_18b20();
Delayus(200);
Write_18b20(0xcc); //跳过ROM地址
Write_18b20(0x44); //温度变换
Init_18b20();
Delayus(1);
Write_18b20(0xcc);
Write_18b20(0xbe);//读温度
databuff[0]=Read_18b20();
databuff[1]=Read_18b20();
temp=databuff[1];
temp<<=8;
temp=temp|databuff[0]; // 两字节合成一个整型变量。
//temp_value_integer=(((databuff[0]<<4)&0xf0)|((databuff[1]>>4)&0x0f));
// temp_value_decimal=(databuff[0]&0x000f)*625;
return temp;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -