#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned int dudata[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f};
unsigned int wedata[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
sbit we=P2^1;
sbit du=P2^0;
sbit DQ=P2^2;
uchar tempdata[6];
uchar temph,templ,temp,flag_get,num;
void ds18B20 ();
void write_B(uchar dat);
uchar read_B();
uint duqutemp ();
void display ();
void delay (uchar i)
{
while(i--);
}
void display ()
{
sta
tic int i=0;
P0=0;
we=1;
we=0;
P0=wedata
;
we=1;
we=0;
P0=tempdata;
du=1;
du=0;
delay(200);
i++;
if(i==6)
i=0;
}
void main ( )
{
while(1)
{
tempdata[5]=0x39; //最后一位显示温度符号 cC
tempdata[0]=0;
tempdata[1]=dudata[temph/100];
tempdata[2]=dudata[(temph%100)/10];
tempdata[3]=dudata[(temph%100)%10];
tempdata[4]=dudata[templ];
display();
if(flag_get==1)
{
temp=duqutemp();
temph=temp>>4;
templ=temp&0x0f;
templ=templ*6/10;
flag_get=0;
}
}
}
void ds18B20 ()
{
uchar n;
DQ=1;
delay(8);
DQ=0;
delay(80);//600us
DQ=1;
delay(8); //等待15到60毫秒
n=DQ;
delay(8);
}
void write_B (uchar dat)
{
uchar t;
for(t=0;t<8;t++)
{
DQ=0;
DQ=dat|0x01;
delay(8);// 45us
DQ=1;
dat>>=1; //从最低位写入数据
}
delay(4);
}
uchar read_B()
{
uchar t,value;
for(t=0;t<8;t++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ)
value|=0x80; //1000 0000 从最低位开始读取
delay(4);
}
return (value);
}
uint duqutemp ()
{
uchar a,c,t;
ds18B20();
write_B(0xcc); //跳过rom
write_B(0x44); //启动温度转换 开始检测温度
delay(200); //给时间读取温度
ds18B20 ();
write_B(0xcc);//
write_B(0xbe); // 读取温度
a=read_B();
c=read_B();
c<<=8;
t=a+c;
return t;
}
看了半天还是不明白哪里出错了 ,求高手赐教,万分感激!!!
没有 针脚检查很多遍了
一周热门 更多>