#include "reg51.h"
#include " intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit dq=P3^7;
bit flag;
sbit sm1=P1^3;
sbit sm2=P1^4;
uchar d1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar d2[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
void delay(uchar n)
{
do
{
_nop_();
_nop_();
_nop_();
n--;
}
while(n);
}
void chushi()
{
dq=1;
delay(10);
dq=0;
delay(120);
dq=1;
delay(30);
if(dq==0)
flag==1;
else
flag==0;
delay(40);
}
uchar readbyte()
{
uchar i;
uchar date=0;
for(i=8;i>0;i--)
{
dq=0;
delay(1);
dq=1;
date>>=1;
if(dq)
date|=0x80;
delay(11);
}
return date;
}
void writebyte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
dq=0;
delay(1);
dat=dat&0x01;
delay(14);
dq=1;
dat=dat>>1;
delay(20);
}
}
uint readtemp()
{
uchar a,b;
uint temp;
chushi();
writebyte(0xcc);
writebyte(0xbe);
a=readbyte();
b=readbyte();
temp=b;
temp=temp<<8;
temp=temp|a;
temp=temp*0.0625;
return temp;
}
display(uint temp)
{
sm1=1;
sm2=0;
P2=d1[(temp/10)&0x0f];
delay(1000);
sm1=0;
sm2=1;
P2=d1[(temp%10)&0x0f];
delay(1000);
}
void main()
{
while(1)
{
chushi();
writebyte(0xcc);
writebyte(0x44);
display(readtemp());
}
}
|