ds18b20 程序仿真不成功 高手帮忙看下

2024-12-15 01:40:47
推荐回答(1个)
回答1:

你看下我的程序 没错误 可已运行:和你的差不对!

#include
#define uchar unsigned char
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit DQ=P3^3;
uchar tempL=0;
uchar tempH=0;
float temperature;
void delay(unsigned int k)
{unsigned int n;
n=0;
while(n{n++;
}
return;
}

void delay1(void)
{int k;
for(k=0;k<400;k++);
}
void display(int k)
{ uchar w,s;
P1=0xfD;
w=k/10;
P0=tab[w];
delay1();

P1=0xfE;
s=k%10;
P0=tab[s];
delay1();

/*P1=0xfb;
P0=tab[k%1000%100/10];
delay1();

P1=0xf7;
P0=tab[k%1000%100%10];
delay1(); */

P1=0xff;
}

Init_DS18B20(void)
{uchar x=0;
DQ=1;
delay(8);
DQ=0;
delay(85);
DQ=1;
delay(14);
}

Read0neChar(void)
{
uchar i=0;
uchar dat=0;
for(i=8;i>0;i--)
{DQ=1;
delay(1);
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
Write0neChar(unsigned char dat)
{uchar i=0;
for(i=8;i>0;i--)
{DQ=0;
DQ=dat&0x01;
delay(5);
DQ=1;
dat>>=1;
}
delay(4);
}
float ReadTemperature(void)
{
Init_DS18B20();
Write0neChar(0xcc);
Write0neChar(0x44);
delay(125);
Init_DS18B20();
Write0neChar(0xcc);
Write0neChar(0xbe);
tempL=Read0neChar();
tempH=Read0neChar();
temperature=((tempH*256)+tempL)*0.0625;
delay(200);
return(temperature);
}

void main()
{
float i;
int h;
P1=0XFF;
P0=0XFF;
while(1)
{
i=ReadTemperature();
h=(int)(i);

display(h);

}
}