代码如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code table1[]={
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0x87,0xff,
0xef};
sbit wela=P2^7;
sbit dula=P2^6;
sbit ds=P2^2;
void delay(uchar i)
{
uchar j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void dsinit()//
{
uint i;
ds=0;
i=103;
while(i>0)i--;
ds=1;
i=4;
while(i>0)i--;
}
bit readbit()
{
uint i;
bit b;
ds=0;
i++;
ds=1;
i++;
i++;
b=ds;
i=8;
while(i>0)i--;
return b;
}
uchar rdadbyte()
{
uint i;
uchar j,dat;
dat=0;
for(i=0;i<8;i++)
{
j=readbit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
void writebyte(uchar dat)
{
uint i;
uchar j;
bit b;
for(j=0;j<8;j++)
{
b=dat&0x01;
dat>>=1;
if(b)
{
ds=0;
i++;
i++;
ds=1;
i=8;
while(i>0)i--;
}
else
{
ds=0;
i=8;
while(i>0)i--;
ds=1;
i++;
i++;
}
}
}
void sendchangecmd()
{
dsinit();
delay(1);
writebyte(0xcc);
writebyte(0x44);
}
void sendreadcmd()
{
dsinit();
delay(1);
writebyte(0xcc);
writebyte(0xbe);
}
uint gettmp()
{
uint value;
float t;
uchar low,high;
sendreadcmd();
low=readbyte();
high=readbyte();
value=high;
value<<=8;
value|=low;
t=value*0.0625;
value=t*10+0.5;
return value;
}
void display(uint v)
{
uchar count;
uchar datas[]={0,0,0};
datas[0]=v/100;
datas[1]=v%100/10;
datas[2]=v%10;
for(count=0;count<3;count++)
{
wela=0;
P0=((0xfe<<count)|(0xfe>>(8-count)));
wela=1;
wela=0;
dula=0;
if(count!=1)
P0=table[datas[count]];
else
P0=table1[datas[count]];
dula=1;
dula=0;
delay(5);
dula=0;
P0=0x00;
dula=1;
dula=0;
}
}
void main()
{
uchar i;
uint value;
while(1)
{
sendchangecmd();
value=gettmp();
for(i=0;i<3;i++)
display(value);
}
} |