51串口通讯,单片机本该不断接收串口调试助手的指令,动态改变数码管和LED显示内容的,可是下面的程序只能接收一次指令,再发送就没反应了,该怎么修改才能实现不断自动接收指令呢?
代码如下:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar SEG7[16]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
uchar ACT[4]={0xf7,0xfb,0xfd,0xfe};
/***************************************************/
sbit D0=P2^0;
sbit D1=P2^1;
sbit D2=P2^2;
sbit D3=P2^3;
sbit D4=P2^4;
sbit D5=P2^5;
sbit D6=P2^6;
sbit D7=P2^7;
#define ON 0
#define OFF 1
uchar a[5];
bit outflag=0;
uint len=0;
/***************************************************/
void delay(uint k)
{
uint i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;j++)
{
;
}
}
}
/***************************************************/
void init(void)
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SCON=0x50;
EA=1;
ES=1;
}
/***************************************************/
void main(void)
{
init();
while(1)
{
P0=SEG7[a[1]];
P1=ACT[1];
delay(1);
P0=SEG7[a[3]];
P1=ACT[0];
delay(1);
if(outflag==1)
{
switch(a[1])
{
case 1:if(a[3]==1)D0=ON;else D0=OFF;break;
case 2:if(a[3]==1)D1=ON;else D1=OFF;break;
case 3:if(a[3]==1)D2=ON;else D2=OFF;break;
case 4:if(a[3]==1)D3=ON;else D3=OFF;break;
case 5:if(a[3]==1)D4=ON;else D4=OFF;break;
case 6:if(a[3]==1)D5=ON;else D5=OFF;break;
case 7:if(a[3]==1)D6=ON;else D6=OFF;break;
case 8:if(a[3]==1)D7=ON;else D7=OFF;break;
default:break;
}
outflag=0;
}
}
}
/***************************************************/
void serial_serve(void) interrupt 4
{
if(RI==1)
{
RI=0;
if(!outflag)
{
if((SBUF=='#')&&(len==0))
{
a[0]=SBUF;
}
else
{
len=len+1;
a[len]=SBUF;
if(len==4)
{
a[1]=a[1]-0x30;
a[3]=a[3]-0x30;
len=0;
outflag=1;
}
}
}
}
} |