两个51相互之间单片机如何进行串口通信

 

 

只进行发送是数据的单片机
1
#include<reg52.h> 2 #define unit unsigned int 3 #define uchar unsigned char 4 5 sbit key=P3^7; 6 7 /*void delayms(unit xms) 8 { 9 unit i,j; 10 for(i=xms;i>0;i--) 11 for(j=580;j>0;j--); 12 } */ 13 14 void send(uchar date) 15 { 16 SBUF=date; 17 while(!TI); 18 TI=0; 19 } 20 21 void chuaninit() 22 { 23 TMOD=0X20; 24 TH1=0Xfd; 25 TL1=0Xfd; 26 SM0=0; 27 SM1=1; 28 ES=1; 29 EA=1; 30 TR1=1; 31 } 32 33 void key1() 34 { 35 uchar d; 36 if(key==0) 37 { 38 d=2; 39 send(d); 40 } 41 else 42 { 43 d=4; 44 send(d); 45 } 46 } 47 48 void main() 49 { 50 chuaninit(); 51 while(1) 52 { 53 key1(); 54 } 55 56 }

 

 

只进行数据接受的单片机
1
#include<reg52.h> 2 #define unit unsigned int 3 #define uchar unsigned char 4 typedef unsigned char u8; 5 6 sbit led=P3^7; 7 sbit LSA=P2^2; 8 sbit LSB=P2^3; 9 sbit LSC=P2^4; 10 11 u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 12 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//显示0~F的值 13 14 void chuaninit() 15 { 16 TMOD=0X20; 17 TH1=0Xfd; 18 TL1=0Xfd; 19 SM0=0; 20 SM1=1; 21 REN=1; 22 ES=1; 23 EA=1; 24 TR1=1; 25 } 26 27 void display(uchar num) 28 { 29 LSA=1; 30 LSB=1; 31 LSC=1; 32 P0=smgduan[num]; 33 } 34 35 36 void main() 37 { 38 chuaninit(); 39 LSA=1; 40 LSB=1; 41 LSC=1; 42 P0=smgduan[3]; 43 while(1); 44 } 45 46 void chuan() interrupt 4 47 { 48 uchar a; 49 RI=0; 50 a=SBUF; 51 display(a); 52 }

 

猜你喜欢

转载自www.cnblogs.com/zhj868/p/12649688.html