求解答
源代码:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2; //define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative
sbit wela1=P2^4; //?????????????????
sbit wela2=P2^5;
sbit wela3=P2^6; //?????????????????
sbit wela4=P2^7; //λ???????????????
sbit beep=P2^3;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef};
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //?????????????λ??????棬???????????????DAT??
}
return(dat);
}
void tmpwritebyte(uchar dat) //write a byte to ds18b20
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //get the temperature
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //two byte compose a int variable
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void display(uint temp) //???????
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
wela4=0;
P0=table[A1]; //?????λ
//wela1=0;
// wela1=1;
wela1=1;
delay(2);
wela1=0;
//wela1=0;
//P0=0xfe;
//wela=1;
//wela=0;
delay(1);
P0=table1[A2]; //????λ
//wela2=0;
wela2=1;
delay(2);
wela2=0;
//wela=0;
//P0=0xfd;
//wela=1;
//wela=0;
delay(1);
P0=table[A3]; //?????λ
//wela3=0;
wela3=1;
delay(2);
wela3=0;
//wela=0;
//P0=0xfb;
// wela=1;
//wela=0;
delay(1);
}
void main()
{
uchar a;
do
{
tmpchange();
for(a=10;a>0;a--)
{
display(tmp());
}
if(temp>=310) //????????31????????????????????????????????????????????????
{
P1=0x00;
beep=0;
}
else
{
beep=1;
P1=0xff;
}
} while(1);
}
源代码:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define D P0
#define W P2
sbit IO=P1^1;
sbit SCLK=P1^0;
sbit RST=P1^2;
/************???????????***********/
sbit s1=P3^5;
sbit s2=P3^6;
sbit s3=P3^7;
sbit led=P2^3;
uchar s, knum=0,snum,fnum;
bit flag;
/***********д???????************/
#define write_shi 0x84
#define write_fen 0x82
#define write_miao 0x80
/***********?????????************/
#define read_shi 0x85
#define read_fen 0x83
#define read_miao 0x81
bit miao_flag;
char miao_num;
char Dmiao;
char shi,fen,miao;
uchar d[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void t0_init()
{
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
uchar read_1302(uchar add)
{
uchar i,date;
RST=0;
for(i=0;i<8;i++)
{
RST=1;
SCLK=0;
IO=add&0x01;
add>>=1;
SCLK=1;
}
for(i=0;i<8;i++)
{
if(IO)
date=date|0x80;
else
date=date|0x00;
SCLK=1;
date>>=1;
SCLK=0;
}
RST=0;
date=date/16*10+date%16;
IO=0;
return date;
}
void write_1302(uchar add,uchar dat)
{
uchar i;
RST=0;
SCLK=0;
for(i=0;i<8;i++)
{
RST=1;
SCLK=0;
IO=add&0x01;
add>>=1;
SCLK=1;
}
for(i=0;i<8;i++)
{
RST=1;
SCLK=0;
IO=dat&0x01;
dat>>=1;
SCLK=1;
}
RST=0;
}
void init_1302()
{
flag=read_1302(0x81);
if(flag&0x80)
write_1302(0x8e,0x00);
write_1302(write_miao,0x56);
write_1302(write_fen,0x49);
write_1302(write_shi,0x14);
write_1302(0x90,0xa5);
write_1302(0x8e,0x80);
}
void display(uchar shi,uchar fen)
{
if(knum==0)
{
snum=30;
fnum=30;
}
if(knum==1)
{
fnum++;
snum=30;
}
if(knum==2)
{
snum++;
fnum=30;
}
if(snum>=30)
{
W=0x7F;
s=d[shi/10];
if(s==0xc0)
{
W=0xff;
D=0xff;
}
D=s;
delay(5);
D=0Xff;
if(miao_flag)
{
miao_flag=0;
W=0xBF;
D=d[shi%10];
delay(5);
D=0Xff;
W=0Xff;
}
else
{
W=0xBF;
D=d[shi%10];
delay(5);
D=0Xff;
W=0Xff;
}
if(snum==60)
snum=0;
}
if(fnum>=30)
{
W=0xDF;
D=d[fen/10];
delay(5);
D=0Xff;
W=0Xff;
W=0xEF;
D=d[fen%10];
delay(5);
D=0Xff;
W=0Xff;
if(fnum==60)
fnum=0;
D=0Xff;
W=0Xff;
}
}
void read_sf()
{
miao=read_1302(read_miao);
/*if(miao!=Dmiao)
{
Dmiao=miao;
TR0=1;
miao_flag=1;
} */
fen=read_1302(read_fen);
shi=read_1302(read_shi);
display(shi,fen);
}
void keyscan()
{
if(s3==0)
{
delay(10);
if(s3==0)
{
while(!s3)
display(shi,fen);
knum++;
if(knum==1)
{
write_1302(0x8e,0x00);
write_1302(write_miao,0x80);
}
if(knum==3)
{
knum=0;
write_1302(write_miao,0x00);
write_1302(0x8e,0x80);
}
}
}
if(knum==1)
{
if(s1==0)
{
delay(10);
if(s1==0)
{
while(!s1)
display(shi,fen);
fen++;
if(fen==60)
fen=0;
write_1302(write_fen,fen/10*16+fen%10);
// read_sf();
}
}
if(s2==0)
{
delay(10);
if(s2==0)
{
while(!s2)
display(shi,fen);
fen--;
if(fen==-1)
fen=59;
write_1302(write_fen,fen/10*16+fen%10);
read_sf();
}
}
}
if(knum==2)
{
if(s1==0)
{
delay(10);
if(s1==0)
{
while(!s1)
display(shi,fen);
shi++;
if(shi==24)
shi=0;
write_1302(write_shi,shi/10*16+shi%10);
read_sf();
}
}
if(s2==0)
{
delay(10);
if(s2==0)
{
while(!s2)
display(shi,fen);
shi--;
if(shi==-1)
shi=23;
write_1302(write_shi,shi/10*16+shi%10);
read_sf();
}
}
}
}
void main()
{
init_1302();
t0_init();
while(1)
{ if(miao_num<10)
led=0;
if(miao_num>=10)
led=1;
read_sf();
keyscan();
}
}
void t0_timer() interrupt 1
{
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
miao_num++;
if(miao_num>=25)
{
miao_num=0;
miao_flag=1;
}
}
|