#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar Receive_Buffer[101]; //接收缓冲
uchar Buf_Index=0; //缓冲空间索引
//数码管编码
uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
//延时
void DelayMS(uint ms)
{
uchar i;
while(ms--) for(i=0;i<120;i++);
}
//主程序
void main()
{
uchar i;
P0=0x00;
Receive_Buffer[0]=-1;
SCON=0x50; //串口模式1，允许接收师范大学信息与机电工程学院—倪继锋
TMOD=0x20; //T1 工作模式2
TH1=0xfd; //波特率9600
TL1=0xfd;
PCON=0x00; //波特率不倍增
EA=1;EX0=1;IT0=1;
ES=1;IP=0x01;
TR1=1;
while(1)
{
for(i=0;i<100;i++)
{ //收到-1 为一次显示结束
if(Receive_Buffer[i]==-1) break;
P0=DSY_CODE[Receive_Buffer[i]];
DelayMS(200);
}
DelayMS(200);
}
}
//串口接收中断函数
void Serial_INT() interrupt 4
{
uchar c;
if(RI==0) return;
ES=0; //关闭串口中断
RI=0; //清接收中断标志
c=SBUF;
if(c>='0'&&c<='9')
{ //缓存新接收的每个字符，并在其后放-1 为结束标志
Receive_Buffer[Buf_Index]=c-'0';
Receive_Buffer[Buf_Index+1]=-1;
Buf_Index=(Buf_Index+1)%100;
}
ES=1;
}
void EX_INT0() interrupt 0 //外部中断0
{
uchar *s="这是由8051 发送的字符串！\r\n";
uchar i=0;
while(s[i]!='\0')
{
SBUF=s[i];
while(TI==0);
TI=0;
i++;
}}