本帖最后由 4564897 于 2019-4-19 20:19 编辑
#include <REG52.H>
#define uchar unsigned char
#define uint unsigned int
sbit DATA=P1^2;
sbit weixuan=P1^6;
sbit duxuan=P1^5;
sbit SCL=P1^3;
sbit SDA=P1^4;
uchar bai,shi,ge;
uchar code seg7[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uchar us)
{
while(us--);
}
void init_ds18b20()
{
uchar init_resu;
DATA=1;
delay(10);
DATA=0;
delay(80);
DATA=1;
delay(20);
init_resu=DATA;
delay(30);
DATA=1;
}
uchar readonechar()
{
uchar i;
uchar readresult;
for(i=8;i>0;i--)
{
readresult>>=1;
DATA=1;
delay(1);
DATA=0;
delay(1);
DATA=1;
delay(1);
if(DATA)
readresult|=0x80;
else
readresult=0x7f;
delay(8);
}
return readresult;
}
writeonechar(uchar dat)
{
uchar i;
for(i=8;i>0;i--)
{
DATA=0;
delay(1);
DATA=dat&0x01;
delay(10);
DATA=1;
dat>>=1;
}
delay(8);
}
void display(uchar bai,uchar shi,uchar ge)
{
weixuan=1;
P0=0xfe;
weixuan=0;
P0=0xff;
duxuan=1;
P0=seg7[bai];
duxuan=0;
delay(30);
weixuan=1;
P0=0xfd;
weixuan=0;
P0=0xff;
duxuan=1;
//P0=(seg7[shi]|0x80);
P0=seg7[shi];
duxuan=0;
delay(30);
weixuan=1;
P0=0xfb;
weixuan=0;
P0=0xff;
duxuan=1;
P0=seg7[ge];
duxuan=0;
delay(30);
}
int readtemp()
{
uchar a,b;
int t;
float tt;
init_ds18b20();
writeonechar(0xcc);
writeonechar(0x44);
init_ds18b20();
writeonechar(0xcc);
writeonechar(0xbe);
a=readonechar();
b=readonechar();
t=b;
t<<=8;
t|=a;
tt=t*0.0625;
t=tt*10+0.5;
return t;
}
void startlic()
{
SDA=1;
delay(1);
SCL=1;
delay(1);
SDA=0;
delay(1);
}
void stoplic()
{
SDA=0;
delay(1);
SCL=1;
delay(1);
SDA=1;
delay(1);
}
void responselic()
{
uchar i;
SCL=1;
delay(1);
while((SDA==1)&&(i<200))
i++;
SCL=0;
delay(1);
}
void lnitlic()
{
SDA=1;
delay(1);
SCL=1;
delay(1);
}
void write_bytelic(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp<<=1;
SCL=0;
delay(1);
SDA=CY;
delay(1);
SCL=1;
delay(1);
}
SCL=0;
delay(1);
SDA=1;
delay(1);
}
void writeaddressdatelic(uchar address,uchar date)
{
startlic();
write_bytelic(0xa0);
responselic();
write_bytelic(address);
responselic();
write_bytelic(date);
responselic();
stoplic();
}
void main()
{
int temp;
lnitlic();
while(1)
{
display(bai,shi,ge);
temp=readtemp();
bai=temp%1000/100;
shi=temp%100/10;
ge=temp%10;
display(bai,shi,ge);
}
}
|