[求助]请高手帮我看一下这个程序(关于DS18B20) 点击:223 | 回复:1



piaoye1985

    
发表于:2009-06-30 22:13:08
楼主

#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char 
#define uint unsigned int
sbit ds=P2^2;
sbit duan=P2^6;
sbit wei=P2^7;
#define NOP _nop_() ;_nop_() ;_nop_ ();_nop_() ;_nop_() ;_nop_()
extern void delay100us(uchar x);
extern void delay20us(uchar m);
uchar code table【】={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar data disputf【3】;
uchar code bitbuf【】={0xfe,0xfd};

void intial()
{
// ds=1;
// _nop_() ;
ds=0;
delay100us(5);
ds=1;
delay100us(2);
}

void write_0()
{
ds=0;
delay100us(1);
ds=1;
NOP;
}

void write_1()
{
ds=0;
NOP;
ds=1;
delay20us(5);
}

bit read_bit()
{
uchar value;
ds=1; _nop_();
ds=0;
NOP;
ds=1;
delay20us(5);
value=ds;
NOP;
if(value) return 1;
return 0;
}
write_byte(uchar dat)
{
uchar i,wr;
for(i=0;i<8;i++)
{
wr=dat&0x01;
dat=dat>>1;
if(wr)
write_1();
else
write_0();
}
}

uchar read_byte()
{
uchar i,j,rd=0;
for(i=0;i<8;i++)
{
j=read_bit();
rd=rd>>1;
rd=rd|(j<<7);
}
return rd;
}

start_1820()
{
intial();
write_byte(0xcc);
write_byte(0x44);
}

read_temp()
{
uchar a,b;
uchar ge=1,shi=2;
uint temp, dat;
intial();
write_byte(0xcc);
write_byte(0xbe);
a=read_byte();
b=read_byte();
temp=a|(b<<8);
if(temp&0x0800)
{
disputf【2】=0x40;
temp=65536-temp;
dat=temp*0.0625;
shi=dat/10;
ge=dat%10;
}

else
{
disputf【2】=0xff;
dat=(temp&0x0fff)*0.0625;
shi=dat/10;
ge=dat%10;
}
disputf【0】=ge;
disputf【1】=shi;

}

display()
{

static data a=0;
duan=1;
P0=disputf【2】;
duan=0;
wei=1;
P0=0xfb;
wei=0;

duan=1;
P0=table【disputf【a】】;
duan=0;
wei=1;
P0=bitbuf【a】;
wei=0;
a++;
if(a>1) a=0;
}

void timer1() interrupt 3
{
TR1=0;
TH1=0xfb;
TL1=0xc3;
TR1=1;
display();

}

void main()
{

TMOD=0x10;
TH1=0xfb;
TL1=0xc3;
TR1=1;
ET1=1;
EA=1;
start_1820();
delay100us(20);
read_temp();
while(1)
{

int i;
start_1820();

for(i=0;i<7500)
delay100us(1);
read_temp();
}
}

本来的意思是想让他显示室温值,但是发光二极管显示显示为00



楼主最近还看过

SSI ļʱ