上图为双机通信示意图,下面分别给出双机通信的程序
//用于接收方
#include
#define uchar unsigned char;
#define uint unsigneed int;
uchar a;//用于保存发来的数据
uchar b[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //数码管显示0-9;
void init(void)
{
SCON = 0x50;
PCON = 0x00;
TMOD = 0x20;
TH1 = 0xf4;
TL1 =0xf4;
TR1 = 1;
EA=1;
ES=1;
}
void main()
{
init();
while(1)
{
P2=b[a];
}
}
void zd() interrupt 4//当进入中断,要么RI=1,要么TI=1
{
if(RI)
{
RI=0;
a=SBUF;
SBUF=a+1;
}
else
T1=0;
}
//用于发送方
#include
#define uchar unsigned char;
#define uint unsigneed int;
unsigned char a;//用于保存发来的数据
unsigned char b[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //数码管显示0-9;
void init(void)
{
SCON = 0x50;
PCON = 0x00;
TMOD = 0x20;
TH1 = 0xf4;
TL1 =0xf4;
TR1 = 1;
EA=1;
ES=1;
}
void main()
{
init();
P1=0xff;
a=P1&0x0f;
SBUF=a;
}
void zd() interrupt 4//当进入中断,要么RI=1,要么TI=1
{
if(RI)
{
RI=0;
a=SBUF;
P2=b[a];
}
else T1=0;
}
|