1、在發(fā)送端proteus圖紙中, 按下按鍵K1和K2,可以改變要發(fā)送的數(shù)據(jù)num(數(shù)值范圍0~9),按下K3通過串口發(fā)送num數(shù)據(jù)到接收端。
接收端能夠接受數(shù)據(jù),并在數(shù)碼管上顯示接受的數(shù)字。
2、接收端也可以進行數(shù)據(jù)的發(fā)送,也可以發(fā)送數(shù)據(jù)到發(fā)送端,同時發(fā)送端也能顯示接收到的數(shù)據(jù)。
單片機源程序如下:
- /*
- 串口發(fā)送與接受一體的控制代碼
- */
- #include <REGX51.H>
- #include <intrins.h>
- //共陽0-F dp-a
- unsigned char code tab[16]=
- {0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,
- 0xc6,0xa1,0x86,0x8e};
- #define uchar unsigned char
- sbit K1=P1^0;//ADD 遞增
- sbit K2=P1^1;//DEC 遞減
- sbit K3=P1^2;//DEC 遞減
- void UartInit(void) //9600bps@11.0592MHz
- {
- PCON &= 0x7F; //波特率不倍速
- SCON = 0x50; //8位數(shù)據(jù),可變波特率
- TMOD = 0x20; //設(shè)定定時器1為8位自動重裝方式
- TL1 = 0xFD; //設(shè)定定時初值
- TH1 = 0xFD; //設(shè)定定時器重裝值
- ET1 = 0; //禁止定時器1中斷
- TR1 = 1; //啟動定時器1
- ES=1;//開串口中斷
- EA=1;//開總中斷
- }
- char putchar (uchar ch)//向串口發(fā)送數(shù)據(jù)
- {
- SBUF=ch;
- while(!TI);//查詢發(fā)生是否完畢
- TI=0;//發(fā)送完畢,TI由軟件清零
- return ch;
- }
- /********************************************************
- 函數(shù)功能: 延時c ms
- 參 數(shù): c
- 返回 值: 無
- *********************************************************/
- void delayms( unsigned int ms) //@11.0592MHz
- {
- unsigned char i, j;
- for(;ms>0;ms--)
- {
- _nop_();
- i = 2;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- uchar Receive_nums=0;//接收到的 字符
- void Uart1() interrupt 4
- {
- if (RI)
- {
- RI=0; //清除RI位
- Receive_nums=SBUF;
-
- }
- }
- void main(void)
- {
- uchar Transmit_nums=0;//發(fā)送給乙機的數(shù)字
- UartInit();
-
- while(1)
- {
- P0=~tab[Receive_nums];//在數(shù)碼管上顯示數(shù)字
- if(K1==0)//遞增按鍵
- {
- delayms(10);//消抖
- if(K1==0)
- {
- Transmit_nums++;
- if(Transmit_nums>9)
- Transmit_nums=9;
- }
- while(K1==0);//等待按鍵K1松開
- }
-
- if(K2==0)//遞減按鍵
- {
- delayms(10);//消抖
- if(K2==0)
- {
- Transmit_nums--;
- if(Transmit_nums<1)
- Transmit_nums=0;
- }
- while(K2==0);//等待按鍵K2松開
- }
-
- if(K3==0)//發(fā)送按鍵
- {
- delayms(10);//消抖
- if(K3==0)
- {
- putchar(Transmit_nums);
- }
- while(K3==0);//等待按鍵K3松開
- }
- }
- }
復制代碼 注意需要配合vspd軟件是用,百度即可獲取
http://www.zg4o1577.cn/bbs/dpj-108037-1.html
Keil代碼下載與Proteus仿真下載:
05_雙機通信.7z
(63.03 KB, 下載次數(shù): 79)
2021-12-5 06:06 上傳
點擊文件名下載附件
|