|
本帖最后由 素还真 于 2017-9-11 12:06 编辑
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit DS=P1^6;
uint temp;
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
unsigned char code table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
void delay(uint count)
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void delayms(uchar x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
void delay10us(uchar t)
{
do
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
while(--t);
}
void Init_Com(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
}
bit drst(void)
{
bit ask;
EA=0;
DS=0;
delay10us(65);
DS=1;
delay10us(6);
ask=DS;
while(!DS);
return ask;
EA=1;
}
bit readbit(void)
{
bit dat;
EA=0;
DS=0;_nop_();
DS=1;_nop_();_nop_();
dat=DS;
delay10us(6);
return (dat);
EA=1;
}
uchar read(void)
{
uchar i,j,dat;
dat=0;
EA=0;
for(i=1;i<=8;i++)
{
j=readbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
EA=1;
}
void write(uchar dat)
{
uchar j;
bit testb;
EA=0;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
DS=0;
_nop_();_nop_();
DS=1;
delay10us(6);
}
else
{
DS=0; //write 0
delay10us(6);
DS=1;
_nop_();_nop_();
}
}EA=1;
}
void DSrun(void)
{
bit ask;
EA=0;
drst();
delay(1);
if(ask==0)
{
write(0xcc);
write(0x44);
}
EA=1;
}
uint tmp()
{
float tt;
uchar a,b;
EA=0;
DSrun();
a=read();
b=read();
temp=b;
temp<<=8;
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
EA=1;
}
void display(uint temp)
{
uchar A1,A2,A3,A4,A2s,A3s,ser;
ser=temp/10;
ser=SBUF;
A1=temp/1000; //这里是为了显示正负,目前没有实现。
A2s=temp%1000;
A2=A2s/100;
A3s=A2s%100;
A3=A3s/10;
A4=A3s%10;
P0=table[A1];
P2=0x01;
delayms(1);
P0=table[A2];
P2=0x02;
delayms(1);
P0=table1[A3];
P2=0x04;
delayms(1);
P0=table[A4];
P2=0x08;
delayms(1);
}
void main()
{
uchar a;
do
{
DSrun();
//delay(200);
for(a=100;a>0;a--)
{ display(tmp());
}
} while(1);
}
这个程序仿真结果是这样的:
求大神指点一下是哪里出错了。
|
|