欢迎来到天天文库
浏览记录
ID:40585145
大小:83.50 KB
页数:5页
时间:2019-08-04
《两片单片机之间的串行通信(proteus仿真图+程序)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、两片单片机之间的串行通信(仿真图+程序)AT89C51+MAX232功能:(1)甲机P1口的开关控制乙机P1口的发光二级管,开关闭合发光二级管亮,开关断开发光二级管灭。(2)乙机P2口的开关控制甲机P2口的数码管,按下4*4矩阵键盘,显示对应的键值0~F(3)乙机P0^0口的开关控制甲机P2口的数码管,按下按键,数码管从0~9循环显示;乙机P0^2口的开关控制甲机P2口的数码管,按下按键,数码管清零。仿真图:/****************************甲机控制与接收*********************************/#include#i
2、nclude#defineucharunsignedchar#defineuintunsignedintsbitK0=P1^0;sbitK1=P1^1;sbitK2=P1^2;sbitK3=P1^3;sbitK4=P1^4;sbitK5=P1^5;sbitK6=P1^6;sbitK7=P1^7;uchari;ucharcodetab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};voiddelay(uintz){uintx,y;for(x=z
3、;x>0;x--)for(y=110;y<0;y--);}voidsend(ucharc)//向串口发送字符{SBUF=c;while(TI==0);TI=0;}voidmain(){uchari;P2=0x00;SCON=0x50;//串口模式1TMOD=0x20;//T1工作模式2PCON=0x00;//波特率不倍增TH1=0xfd;//波特率设定6900TL1=0xfd;TI=RI=0;TR1=1;//启动定时器T1IE=0x90;//允许串口中断while(1){if(K0==0)send('0');elsesend('A');if(K1==0)send('1');elses
4、end('B');if(K2==0)send('2');elsesend('C');if(K3==0)send('3');elsesend('D');if(K4==0)send('4');elsesend('E');if(K5==0)send('5');elsesend('F');if(K6==0)send('6');elsesend('G');if(K7==0)send('7');elsesend('H');}}voidserial_int()interrupt4//甲机串口接收中断函数{if(RI){RI=0;if(SBUF>=0&&SBUF<=15)P2=tab[SBUF];e
5、lseP2=0x00;if(SBUF=='x')if(i>=0&&i<9){i=i+1;P2=tab[i];}if(i==9)i=0;if(SBUF=='y'){P2=0x00;i=0;}}}/*****************************乙机控制与接收程序*****************************/#include#include#defineucharunsignedchar#defineuintunsignedintsbitL0=P1^0;sbitL1=P1^1;sbitL2=P1^2;sbitL3=P1^3;
6、sbitL4=P1^4;sbitL5=P1^5;sbitL6=P1^6;sbitL7=P1^7;sbitKEY1=P0^0;sbitKEY2=P0^2;voiddelay(uintz){uintx,y;for(x=z;x>0;x--)for(y=110;y<0;y--);}voidsend(ucharc)//向串口发送字符{SBUF=c;while(TI==0);TI=0;}ucharkey()//按键扫描{ucharkeyon,temp;P2=0x0f;delay(1);temp=P2^0x0f;switch(temp){case1:keyon=3;break;case2:keyo
7、n=2;break;case4:keyon=1;break;case8:keyon=0;break;default:keyon=16;}P2=0xf0;delay(1);temp=P2>>4^0x0f;switch(temp){case1:keyon+=0;break;case2:keyon+=4;break;case4:keyon+=8;break;case8:keyon+=12;break;}returnkeyon;}voidmain(){SCON=0
此文档下载收益归作者所有