以16进制发送一个0-65536之间的任一数,当单片机收到后在数码管上动态显示出来,波特率自定。
//第一个是输入abcd四个显示
*#include
#define uint unsigned int //宏定义
#define uchar unsigned char //宏定义
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00,0x76,0x38,0x00};
uchar flag,temp,a,b,c,d;
void display(uchar aa,uchar bb,uchar cc,uchar dd);
void delay(uint z);
void main()
{
TMOD=0x21;//设置定时器1为工作方式2
TH1=0xf4;
TL1=0xf4;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
while(1)
{
display(a,b,c,d);
}
}
void ser() interrupt 4
{
RI=0;
temp=SBUF;
flag++;
if(flag==1)
{
a=temp/16;
b=temp%16;
}
if(flag==2)
{
c=temp/16;
d=temp%16;
flag=0;
}
}
void display(uchar aa,uchar bb,uchar cc,uchar dd)
{
P1=0x00;
P0=0xfe;
P1=table[aa];
delay(1);
P1=0x00;
P0=0xfd;
P1=table[bb];
delay(1);
P1=0x00;
P0=0xfb;
P1=table[cc];
delay(1);
P1=0x00;
P0=0xf7;
P1=table[dd];
delay(1);
P1=0x00;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}*/
//输入12345显示
#include
#define uint unsigned int //宏定义
#define uchar unsigned char //宏定义
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00,0x76,0x38,0x00};
uchar flag,temp,a,b,c,d,e;
void display(uchar aa,uchar bb,uchar cc,uchar dd,uchar ee);
void delay(uint z);
void main()
{
TMOD=0x21;//设置定时器1为工作方式2
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
while(1)
{
display(a,b,c,d,e);
}
}
void ser() interrupt 4
{
RI=0;
temp=SBUF;
flag++;
if(flag==1)
{
a=temp/16;
b=temp%16;
}
if(flag==2)
{
c=temp/16;
d=temp%16;
}
if(flag==3)
{
e=temp%16;
flag=0;
}
}
void display(uchar aa,uchar bb,uchar cc,uchar dd,uchar ee)
{
P1=0x00;
P0=0xfe;
P1=table[aa];
delay(1);
P1=0x00;
P0=0xfd;
P1=table[bb];
delay(1);
P1=0x00;
P0=0xfb;
P1=table[cc];
delay(1);
P1=0x00;
P0=0xf7;
P1=table[dd];
delay(1);
P1=0x00;
P0=0xef;
P1=table[ee];
delay(1);
P1=0x00;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
|