|
如何编程,使串口接收4位数字,单片机将其在数码管上显示。
[复制链接]
以下是我写的程序,但每次数码管都显示不成功。麻烦大家帮帮忙,教我程序该怎么编。谢谢。
#include
#define uchar unsigned char
sbit wela=P2^7;
sbit dula=P2^6;
uchar recive[4],rec=0,flag;
char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delays(int z)
{
int i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void uart_isr() interrupt 4
{
if(RI)
{
RI=0;
recive[rec]=SBUF;
rec++;
if(rec==4)
{
rec=0;
flag=1;
}
}
}
void display()
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[recive[0]];
dula=0;
delays(5);
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=table[recive[1]];
dula=0;
delays(5);
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[recive[2]];
dula=0;
delays(5);
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[recive[3]];
dula=0;
delays(5);
}
void main()
{
TMOD=0x20;
SCON=0x50;
TH1=TL1=0xfd;
TR1=1;
EA=1;
ES=1;
while(1)
{
if(flag)
{
display();
flag=0;
}
}
}
|
|