数码管只显示一次正确距离,然后数码管显示0,再无反应。
代码在这:
/***********************************************************************************************************/
#include
#include
sbit RX = P2^1;
sbit TX = P2^2;
sbit du = P2^6;
sbit we = P2^7;
unsigned int time=0;
unsigned int timer=0;
unsigned char posit=0;
unsigned long S=0;
bit flag =0;
unsigned char const discode[] ={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x01/*-*/};
unsigned char const positon[3]={ 0xf7,0xef,0xdf};
unsigned char disbuff[4] ={ 0,0,0,0,};
/********************************************************/
void Display(void)
{
P0=discode[disbuff[posit]];
du = 1;
du = 0;
P0=positon[posit];
we = 1;
we = 0;
if(++posit>=3)
posit=0;
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100;
//S++;
if((S>=700)||flag==1)
{
flag=0;
disbuff[0]=10;
disbuff[1]=10;
disbuff[2]=10;
}
else
{
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
}
}
/********************************************************/
void zd0() interrupt 1
{
flag=1;
}
/********************************************************/
void zd3() interrupt 3
{
TH1=0xf8;
TL1=0x30;
Display();
timer++;
if(timer>=40)
{
timer=0;
TX=1;
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
TX=0;
}
}
/*********************************************************/
void main( void )
{
TMOD=0x11;
TH0=0;
TL0=0;
TH1=0xf8;
TL1=0x30;
ET0=1;
ET1=1;
TR1=1;
EA=1;
while(1)
{
while(!RX);
TR0=1;
while(RX);
TR0=0;
Conut();
//S++;
}
}