#include
#include"intrins.h"
#include
#include"DS18B20.h"
sbit DQ=P3^7;//ds18b20
uchar data disdata[5];
uint value;//温度值
uint value1;
uint vlalue_buf[8]=0;
unsigned char id[8][8]={0x28,0xEE,0x44,0x9F,0x14,0x16,0x02,0x87,
0x28,0xFF,0x95,0x5b,0xc0,0x17,0x01,0xee,
0x28,0xFF,0x0f,0x40,0xc0,0x17,0x01,0x48,
0x28,0xFF,0x1b,0x32,0xc0,0x17,0x02,0x66,
0x28,0xFF,0x48,0x42,0xc0,0x17,0x01,0xf7,
0x28,0xFF,0x8e,0x3b,0xc0,0x17,0x01,0x72,
};
void delay_18B20(unsigned int i)//延时1微秒
{
while(i--);
}
void ds1820rst()/*ds1820复位*/
{
unsigned char x=0;
DQ = 1; //DQ复位
delay_18B20(4); //延时
DQ = 0; //DQ拉低
delay_18B20(100); //精确延时大于480us
DQ = 1; //拉高
delay_18B20(40);
}
uchar ds1820rd()/*读数据*/
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{ DQ = 0; //给脉冲信号
dat>>=1;
DQ = 1; //给脉冲信号
if(DQ)
dat|=0x80;
delay_18B20(10);
}
return(dat);
}
void ds1820wr(uchar wdata)/*写数据*/
{unsigned char i=0;
for (i=8; i>0; i--)
{ DQ = 0;
DQ = wdata&0x01;
delay_18B20(10);
DQ = 1;
wdata>>=1;
}
}
sum_value(uint x,uint max,uint min)
{
uint i,sum_vlalue=0;
for(i=0;i
{
read_temp(i);
sum_vlalue+=vlalue_buf[i];
}
sum_vlalue=sum_vlalue/x;
value=sum_vlalue;
if(value>max||value
{
didi();
}
return (value);
}
read_temp(uint z)/*读取温度值并转换*/
{
uchar a,b,y=0,i=0;
ds1820rst();
ds1820wr(0x55);//*跳过读序列号*/
for(y=0;y<8;y++)
{
ds1820wr(id[z][y]);
}
ds1820wr(0x44);//*启动温度转换*/
ds1820rst();
ds1820wr(0x55);//*跳过读序列号*/
for(y=0;y<8;y++)
{
ds1820wr(id[z][y]);
}
ds1820wr(0xbe);//*读取温度*/
a=ds1820rd();
b=ds1820rd();
value=b;
value<<=8;
value=value|a;
if(value<0x0fff)
flag=0;
else
{
value=~value+1;
flag=1;
}
value=value*(0.625);//温度值扩大10倍,精确到1位小数
// }
vlalue_buf[z]=value;
return(vlalue_buf[z]);
}