ds18b20的驱动
时间:10-02
整理:3721RD
点击:
为什么我这个程序显示不了温度啊,很简单的一个数码管显示,为什么不行呢?
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^7;
uchar code du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar temp;
void delay(uint a)
{
while(a--);
}
void init_ds18b20()
{
uchar n;
DQ=1;
delay(8);
DQ=0;
delay(80);
DQ=1;
delay(8);
n=DQ;
delay(4);
}
void write_byte_18b20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delay(4);
DQ=1;
dat=dat>>1;
}
delay(4);
}
uchar read_byte_18b20()
{
uchar i,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ)
{
value|=0x80;
delay(4);
}
}
return value;
}
uchar read_temp()
{
uchar a,b;
init_ds18b20();
write_byte_18b20(0xcc);
write_byte_18b20(0x44);
delay(100);
init_ds18b20();
write_byte_18b20(0xcc);
write_byte_18b20(0xbe);
a=read_byte_18b20();
b=read_byte_18b20();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void display()
{
P1=0xef;
P0=du[temp%10];
delay(500);
P1=0xdf;
P0=du[temp/10];
delay(500);
}
void main()
{
while(1)
{
temp=read_temp();
display();
}
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^7;
uchar code du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar temp;
void delay(uint a)
{
while(a--);
}
void init_ds18b20()
{
uchar n;
DQ=1;
delay(8);
DQ=0;
delay(80);
DQ=1;
delay(8);
n=DQ;
delay(4);
}
void write_byte_18b20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;
delay(4);
DQ=1;
dat=dat>>1;
}
delay(4);
}
uchar read_byte_18b20()
{
uchar i,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ)
{
value|=0x80;
delay(4);
}
}
return value;
}
uchar read_temp()
{
uchar a,b;
init_ds18b20();
write_byte_18b20(0xcc);
write_byte_18b20(0x44);
delay(100);
init_ds18b20();
write_byte_18b20(0xcc);
write_byte_18b20(0xbe);
a=read_byte_18b20();
b=read_byte_18b20();
b<<=4;
b+=(a&0xf0)>>4;
return b;
}
void display()
{
P1=0xef;
P0=du[temp%10];
delay(500);
P1=0xdf;
P0=du[temp/10];
delay(500);
}
void main()
{
while(1)
{
temp=read_temp();
display();
}
}
连一点注释都没。
自己急急忙忙写的