You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.9 KiB
108 lines
2.9 KiB
3 years ago
|
#include "DS18B20.h"
|
||
|
|
||
|
sbit DS18B20=P3^2; //定义数据线
|
||
|
|
||
|
void delay_ms(unsigned int x)//延时1ms
|
||
|
{
|
||
|
unsigned int y,z;
|
||
|
for(y=x;y>0;y--)
|
||
|
for(z=110;z>0;z--);
|
||
|
}
|
||
|
|
||
|
uchar DS18B20_Init() //初始化。成功返回1,不成功返回0.
|
||
|
{
|
||
|
uchar i;
|
||
|
DS18B20 = 0; //将总线拉低480微秒~960微秒
|
||
|
i = 70;
|
||
|
while(i--); //延时642微秒
|
||
|
DS18B20 = 1; //拉高总线,等待后续反馈
|
||
|
i = 0;
|
||
|
while(DS18B20) //等待DS18B20拉低总线
|
||
|
{
|
||
|
delay_ms(1); //定义的延迟1ms的延迟函数
|
||
|
i++;
|
||
|
if(i>5) //等待5ms以上
|
||
|
{
|
||
|
return 0; //初始化失败
|
||
|
}
|
||
|
}
|
||
|
return 1; //初始化成功
|
||
|
}
|
||
|
|
||
|
uchar DS18B20_ReadByte() //通过读取8次状态位达到读取字节的目的
|
||
|
{
|
||
|
uchar byte, bi;
|
||
|
uint i, j;
|
||
|
for(j=8; j>0; j--)
|
||
|
{
|
||
|
DS18B20 = 0; //先将总线拉低
|
||
|
i++;
|
||
|
DS18B20 = 1; //然后释放总线
|
||
|
i++;
|
||
|
i++; //延时6微秒等待数据稳定
|
||
|
bi = DS18B20; //读取数据,从最低位开始读取
|
||
|
byte = (byte >> 1) | (bi << 7); //从最低位开始记录,将byte依次右移
|
||
|
i = 4; //读取完之后等待48微秒再接着读取下一个数
|
||
|
while(i--);
|
||
|
}
|
||
|
return byte; //将读取完的字节保存
|
||
|
}
|
||
|
|
||
|
void DS18B20_WriteByte(uchar dat)//函数通过循环写8个位实现写一个字节
|
||
|
{
|
||
|
uint i, j;
|
||
|
for(j=0; j<8; j++)
|
||
|
{
|
||
|
DS18B20 = 0; //每写入一位数据之前先把总线拉低
|
||
|
i++;
|
||
|
DS18B20 = dat & 0x01; //然后写入一个数据,从最低位开始
|
||
|
i=6;
|
||
|
while(i--); //延时68微秒
|
||
|
DS18B20 = 1; //释放总线,给总线1微秒恢复时间才能接着写入下一位
|
||
|
dat >>= 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int DS18B20_ReadTemp() //读取的温度有两个字节,分别用tml和tmh存储两个字节
|
||
|
{
|
||
|
int temp = 0;
|
||
|
uchar tmh, tml;
|
||
|
DS18B20_Init(); //传感器初始化
|
||
|
delay_ms(1); //延时1微秒
|
||
|
DS18B20_WriteByte(0xcc); //跳过读取序列号操作
|
||
|
DS18B20_WriteByte(0x44); //温度转换指令
|
||
|
//完成温度转换功能
|
||
|
DS18B20_Init(); //传感器初始化
|
||
|
delay_ms(1);
|
||
|
DS18B20_WriteByte(0xcc); //跳过读取序列号操作
|
||
|
DS18B20_WriteByte(0xbe); //发送读取温度指令
|
||
|
//然后等待转换完后发送读取温度命令
|
||
|
tml = DS18B20_ReadByte(); //读取温度值共16位,先读低字节
|
||
|
tmh = DS18B20_ReadByte(); //再读高字节
|
||
|
temp = tmh; //先存高字节,后面通过移位存储低字节
|
||
|
temp <<= 8;
|
||
|
temp |= tml;
|
||
|
return temp;
|
||
|
}
|
||
|
|
||
|
|
||
|
uint Temper_change()
|
||
|
{
|
||
|
uint temper;
|
||
|
float tp;
|
||
|
//temper=read_temper();
|
||
|
temper=DS18B20_ReadTemp();
|
||
|
if(temper<0) //考虑负温度的情况
|
||
|
{
|
||
|
temper=temper-1;
|
||
|
temper=~temper;
|
||
|
tp=temper*0.0625; //16位温度转换成10进制的温度
|
||
|
temper=tp*100+0.5; //留两个小数点,并四舍五入
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
tp=temper*0.0625; //16位温度转换成10进制的温度
|
||
|
temper=tp*100+0.5; //留两个小数点,并四舍五入
|
||
|
}
|
||
|
return temper;
|
||
|
}
|