自己寫(xiě)的AT89C2051電子溫度計(jì)
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit GW=P3^0;
sbit SW=P3^1;
sbit BW=P3^2;
sbit DQ=P3^3;
code uchar NUM[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uint temp_value;
void delay(uchar a)
{
while(a--);
}
void init_DS18B20()
{
uchar b=0;
DQ=1;
delay(8);
DQ=0;
delay(100);
DQ=1;
delay(14);
b=DQ;
delay(20);
}
uchar read(void)
{
uint i, dat=0;
for(i=8;i>0;i--)
{
DQ=1;
_nop_();_nop_();
dat>>=1;
DQ=0;
_nop_();_nop_();_nop_();_nop_();
DQ=1;
_nop_();_nop_();_nop_();_nop_();
if(DQ)
dat|=0x80;
delay(12);
}
DQ=1;
return(dat);
}
void write(uchar dat)
{
uchar d=0;
for(d=8;d>0;d--)
{
DQ=1;
_nop_();_nop_();
DQ=0;
delay(1);
DQ=dat&0x01;
delay(12);
dat=dat/2;
}
DQ=1;
delay(2);
}
void ReadTemp()
{
uint a=0;
uint b=0;
#include
#define uchar unsigned char
#define uint unsigned int
sbit GW=P3^0;
sbit SW=P3^1;
sbit BW=P3^2;
sbit DQ=P3^3;
code uchar NUM[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uint temp_value;
void delay(uchar a)
{
while(a--);
}
void init_DS18B20()
{
uchar b=0;
DQ=1;
delay(8);
DQ=0;
delay(100);
DQ=1;
delay(14);
b=DQ;
delay(20);
}
uchar read(void)
{
uint i, dat=0;
for(i=8;i>0;i--)
{
DQ=1;
_nop_();_nop_();
dat>>=1;
DQ=0;
_nop_();_nop_();_nop_();_nop_();
DQ=1;
_nop_();_nop_();_nop_();_nop_();
if(DQ)
dat|=0x80;
delay(12);
}
DQ=1;
return(dat);
}
void write(uchar dat)
{
uchar d=0;
for(d=8;d>0;d--)
{
DQ=1;
_nop_();_nop_();
DQ=0;
delay(1);
DQ=dat&0x01;
delay(12);
dat=dat/2;
}
DQ=1;
delay(2);
}
void ReadTemp()
{
uint a=0;
uint b=0;
init_DS18B20();
write(0xCC);
write(0x44);
delay(100);
init_DS18B20();
write(0xCC);
write(0xBE);
delay(100);
write(0xCC);
write(0x44);
delay(100);
init_DS18B20();
write(0xCC);
write(0xBE);
delay(100);
a=read();
b=read();
temp_value=((b<<8)|a)*0.0625;
}
void display (uchar num0,uchar num1,uchar num2)
{
P1=NUM[num0];
GW=0;
delay(200);
GW=1;
b=read();
temp_value=((b<<8)|a)*0.0625;
}
void display (uchar num0,uchar num1,uchar num2)
{
P1=NUM[num0];
GW=0;
delay(200);
GW=1;
P1=NUM[num1];
SW=0;
delay(200);
SW=1;
P1=NUM[num2];
BW=0;
delay(200);
BW=1;
}
main()
{
uint a ,b,c;
while(1)
{
ReadTemp();
a=temp_value%10;
b=temp_value/10%10;
c=temp_value/100%10;
display(a,b,c);
}
}
完整版本的源代碼下載:http://www.51hei.com/f/ds7865k.rar
SW=0;
delay(200);
SW=1;
P1=NUM[num2];
BW=0;
delay(200);
BW=1;
}
main()
{
uint a ,b,c;
while(1)
{
ReadTemp();
a=temp_value%10;
b=temp_value/10%10;
c=temp_value/100%10;
display(a,b,c);
}
}
完整版本的源代碼下載:http://www.51hei.com/f/ds7865k.rar
下面是硬件原理圖:
評(píng)論