0x1 實驗要求
(1)每隔1秒鍾,A向B通過串口發送一個字節c_num(該字節按照0x00-0x09循環,例如某一時刻發送c_num=0x-3);
(2)B接收到數據后,做9-c_num的計算,並將計算結果通過串口發送給A單片機。例如B接收到0x03,則B要通過串口返回0x09-0x03=0x06給單片機A;
(3)A接收到數據后,將收到數據寫在數碼管上,例如顯示收到的數字6;
(4)A和B的串口發送不要用中斷方式,A的串口接收必須用中斷方式,B的串口接收可用/不用串口中斷。
0x2 電路原理圖
0x3 代碼部分
單片機A:
#include<reg51.h>
#define uchar unsigned char
uchar RIflag=0;
uchar RIreceive;
char code map[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(unsigned int time)
{
unsigned int j=0;
for(;time>0;time--)
for(j=0;j<125;j++);
}
void main(void)
{
uchar counter=0;
TMOD=0x20;
TH1=TL1=0xf4;
PCON=0;
SCON=0x50;
TR1=1;
EA=1;
ES=1;//打開中斷
while(1)
{
SBUF=counter;
while(TI==0);
TI=0;//數據發送完成,標志位清零
if((RIflag==1)&&(RIreceive==counter))
{
P2=map[counter];
if(++counter>9)counter=0;
delay(500);
}
}
}
void zhongduan() interrupt 4
{
if(RI)
{
RI=0;
RIflag=1;
RIreceive=SBUF;
}
}
單片機B:
#include<reg51.h>
#define uchar unsigned char
char code map[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void main(void)
{
uchar receive;
TMOD=0x20;
TH1=TL1=0xf4;
PCON=0;
SCON=0x50;
TR1=1;
while(1)
{
while(RI==1)
{
RI=0;
receive=SBUF;
SBUF=receive;
while(TI==0);
TI=0;
P2=map[9-receive];
}
}
}