这是另一种显示方式,结果还是不变,求各位司机大神指导
/////////////////////////////////////////////////
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit duan=P2^6;
sbit wei=P2^7;
sbit sda=P2^2;
uchar code num[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay_ms(uint t) //1毫秒延时
{
uint a,b;
for(a=t;a>0;a--)
for(b=0;b<113;b++);
}
void delay_us(uint t) //11微妙延时
{
for(;t>0;t--);
}
void display(uchar bai,uchar shi,uchar ge)
{
P0=num[bai];
duan=1;
duan=0;
P0=0xfe;
wei=1;
wei=0;
delay_ms(5);
P0=(num[shi]|0x80);
duan=1;
duan=0;
P0=0xfd;
wei=1;
wei=0;
delay_ms(5);
P0=num[ge];
duan=1;
duan=0;
P0=0xfb;
wei=1;
wei=0;
delay_ms(5);
}
void init()
{
sda=0;
delay_ms(1);
sda=1;
while(sda==1);
while(~sda==1);
delay_ms(1);
}
void write_data(uint dat)
{
uint i;
bit b;
for(i=8;i>0;i--)
{
b=dat&0x01;
dat=dat>>1;
if(sda==0)//写0
{
sda=0;
delay_us(6);
sda=1;
_nop_();
}
else //写1
{
sda=0;
delay_us(1);
sda=1;
delay_us(6);
}
}
}
uint read_data()
{
uchar i;
uchar dat=0;
for(i=8;i>0;i--)
{
sda=0;
dat>>=1;
_nop_();
sda=1;
if(sda==1)
dat=0|0x80;
delay_us(6);
}
return dat;
}
uint gettemp()
{
uchar data1,data2;
uint data16=0;
float tt;
init();
write_data(0xcc);
write_data(0x44);
while(sda==0);
init();
write_data(0xcc);
write_data(0xbe);
data1=read_data();
data2=read_data();
data16=data16|data2;
data16=(data16<<8)|data1;
tt=data16*0.0625;
data16=tt*10+0.5;
return data16;
}
void main()
{
uint temp;
uchar bai,shi,ge;
while(1)
{
temp=gettemp();
bai=temp%1000/100;
shi=temp%100/10;
ge=temp%10;
display(bai,shi,ge);
}
delay_ms(5);
}
|