📄 ds18b20.h
字号:
#include<reg52.h>
#include <absacc.h>
/**************宏定义***************/
#define uchar unsigned char
#define uint unsigned int
/*************位定义****************/
sbit TMDAT=P2^0; //控制线兼数据线
unsigned char Temp_Integer=0; //温度整数部分
unsigned char Temp_Decimal=0; //温度小数部分
unsigned char fg=0; //温度正负标志
//-------------------------------------------------------------------------------------
//函数名称:dmsec
//入口参数:count
//函数功能:延时子程序
//-------------------------------------------------------------------------------------
void dmsec(uint count)
{
uint i;
while(count--)
{
for(i=0;i<125;i++){}
}
}
//-------------------------------------------------------------------------------------
//函数名称:tmreset
//入口参数:无
//函数功能:
//-------------------------------------------------------------------------------------
void tmreset(void)
{
uint i;
TMDAT=0;
i=103;
while(i>0) i--; //大约900us
TMDAT=1;
i=4;
while(i>0) i--;
}
//-------------------------------------------------------------------------------------
//函数名称:tmpre
//入口参数:无
//函数功能:等待DS18B20应答
//-------------------------------------------------------------------------------------
void tmpre(void)
{
uint i;
while(TMDAT);
while(~TMDAT);
i=4;
while(i>0) i--;
}
//-------------------------------------------------------------------------------------
//函数名称:tmrbit
//入口参数:无
//返回值: dat
//函数功能:在总线上读一位
//-------------------------------------------------------------------------------------
bit tmrbit(void)
{
uint i=0;
bit dat=0;
TMDAT=0;i++;
TMDAT=1;i++;i++;
dat=TMDAT;
i=8;
while(i>0) i--;
return(dat);
}
//-------------------------------------------------------------------------------------
//函数名称:tmrbyte
//入口参数:无
//返回值: dat
//函数功能:读一个字节
//-------------------------------------------------------------------------------------
uchar tmrbyte(void)
{
uchar i=0,j=0,dat=0;
for(i=1;i<=8;i++)
{
j=tmrbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
//-------------------------------------------------------------------------------------
//函数名称:tmwbyte
//入口参数:dat
//函数功能:写命令
//-------------------------------------------------------------------------------------
void tmwbyte(uchar dat)
{
uint i=0;
uchar j=0;
bit testb=0;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
TMDAT=0; //写1
i++;i++;
TMDAT=1;
i=8;while(i>0) i--;
}
else
{
TMDAT=0; //写0
i=8;while(i>0) i--;
TMDAT=1;
i++;i++;
}
}
}
//-------------------------------------------------------------------------------------
//函数名称:tmstart
//入口参数:无
//函数功能:开始转换
//-------------------------------------------------------------------------------------
void tmstart(void)
{
tmreset();
tmpre();
dmsec(1);
tmwbyte(0xcc);
tmwbyte(0x44);
}
//-------------------------------------------------------------------------------------
//函数名称:tmrbyte
//入口参数:无
//函数功能:读取温度值
//-------------------------------------------------------------------------------------
void tmrtemp()
{
uchar H_18B20=0,L_18B20=0,y1=0,y2=0;
fg=0;
tmreset(); //初始化DS18B20
tmpre();
dmsec(1);
tmwbyte(0xcc); //跳过ROM命令
tmwbyte(0xbe); //发送读取数据命令
L_18B20=tmrbyte(); //读取温度低八位数据
H_18B20=tmrbyte(); //读取温度高八位数据
if(H_18B20>0x7f) //最高位为1时温度是负
{
L_18B20=~L_18B20+1; //补码转换,取反加一
H_18B20=~H_18B20;
fg=1; //读取温度为负时fg=1
}
//温度小数部分
Temp_Decimal=L_18B20&0x0f;
//温度整数部分
L_18B20=L_18B20>>4;
H_18B20=H_18B20<<4;
Temp_Integer=H_18B20|L_18B20;
}
/*********************************************************************
*功能:延时子程序
*说明:形参Delayms=5000,时间延长大约为1s
*********************************************************************/
/*void Delay(uint Delayms)
{
uchar j;
uint i;
for(i=0;i<Delayms;i++)
for(j=0;j<255;j++);
} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -