標(biāo)題: 兩個(gè)單片機(jī)之間進(jìn)行串口通信程序與Proteus仿真 [打印本頁(yè)]

作者: sfdsl1    時(shí)間: 2021-12-4 23:14
標(biāo)題: 兩個(gè)單片機(jī)之間進(jìn)行串口通信程序與Proteus仿真
1、在發(fā)送端proteus圖紙中,  按下按鍵K1和K2,可以改變要發(fā)送的數(shù)據(jù)num(數(shù)值范圍0~9),按下K3通過(guò)串口發(fā)送num數(shù)據(jù)到接收端。

接收端能夠接受數(shù)據(jù),并在數(shù)碼管上顯示接受的數(shù)字。

2、接收端也可以進(jìn)行數(shù)據(jù)的發(fā)送,也可以發(fā)送數(shù)據(jù)到發(fā)送端,同時(shí)發(fā)送端也能顯示接收到的數(shù)據(jù)。


單片機(jī)源程序如下:

  1. /*
  2.                 串口發(fā)送與接受一體的控制代碼
  3. */
  4. #include <REGX51.H>
  5. #include <intrins.h>

  6. //共陽(yáng)0-F dp-a
  7. unsigned char code tab[16]=
  8. {0xc0,0xf9,0xa4,0xb0,
  9. 0x99,0x92,0x82,0xf8,
  10. 0x80,0x90,0x88,0x83,
  11. 0xc6,0xa1,0x86,0x8e};

  12. #define uchar unsigned char
  13. sbit K1=P1^0;//ADD  遞增
  14. sbit K2=P1^1;//DEC  遞減
  15. sbit K3=P1^2;//DEC  遞減



  16. void UartInit(void)                //9600bps@11.0592MHz
  17. {
  18.         PCON &= 0x7F;                //波特率不倍速
  19.         SCON = 0x50;                //8位數(shù)據(jù),可變波特率
  20.         TMOD = 0x20;                //設(shè)定定時(shí)器1為8位自動(dòng)重裝方式
  21.         TL1 = 0xFD;                //設(shè)定定時(shí)初值
  22.         TH1 = 0xFD;                //設(shè)定定時(shí)器重裝值
  23.         ET1 = 0;                //禁止定時(shí)器1中斷
  24.         TR1 = 1;                //啟動(dòng)定時(shí)器1
  25.         ES=1;//開(kāi)串口中斷
  26.         EA=1;//開(kāi)總中斷
  27. }

  28. char putchar (uchar ch)//向串口發(fā)送數(shù)據(jù)
  29. {
  30.          SBUF=ch;
  31.          while(!TI);//查詢(xún)發(fā)生是否完畢
  32.          TI=0;//發(fā)送完畢,TI由軟件清零
  33.         return ch;
  34. }

  35. /********************************************************

  36. 函數(shù)功能: 延時(shí)c  ms
  37. 參    數(shù):  c
  38. 返回  值: 無(wú)
  39. *********************************************************/

  40. void delayms( unsigned int ms)                //@11.0592MHz
  41. {
  42.          unsigned char i, j;
  43.         for(;ms>0;ms--)
  44.                 {
  45.                                 _nop_();
  46.                                 i = 2;
  47.                                 j = 199;
  48.                                 do
  49.                                 {
  50.                                         while (--j);
  51.                                 } while (--i);
  52.                 }
  53. }

  54. uchar Receive_nums=0;//接收到的  字符
  55. void Uart1() interrupt 4
  56. {        
  57.     if (RI)
  58.    {
  59.                         RI=0;        //清除RI位
  60.                         Receive_nums=SBUF;        
  61.                  
  62.    }
  63. }

  64. void main(void)
  65. {
  66.         uchar Transmit_nums=0;//發(fā)送給乙機(jī)的數(shù)字
  67.         UartInit();
  68.         
  69.         while(1)
  70.         {
  71.                 P0=~tab[Receive_nums];//在數(shù)碼管上顯示數(shù)字
  72.                 if(K1==0)//遞增按鍵
  73.                 {
  74.                         delayms(10);//消抖
  75.                          if(K1==0)
  76.                          {
  77.                                 Transmit_nums++;
  78.                                  if(Transmit_nums>9)
  79.                                          Transmit_nums=9;
  80.                          }
  81.                          while(K1==0);//等待按鍵K1松開(kāi)
  82.                 }
  83.                
  84.                 if(K2==0)//遞減按鍵
  85.                 {
  86.                         delayms(10);//消抖
  87.                          if(K2==0)
  88.                          {
  89.                                 Transmit_nums--;
  90.                                  if(Transmit_nums<1)
  91.                                          Transmit_nums=0;         
  92.                          }
  93.                  while(K2==0);//等待按鍵K2松開(kāi)
  94.                 }
  95.                
  96.                 if(K3==0)//發(fā)送按鍵
  97.                 {
  98.                         delayms(10);//消抖
  99.                          if(K3==0)
  100.                          {
  101.                                 putchar(Transmit_nums);
  102.                          }
  103.                          while(K3==0);//等待按鍵K3松開(kāi)
  104.                 }
  105.         }
  106. }
復(fù)制代碼
注意需要配合vspd軟件是用,百度即可獲取
http://www.torrancerestoration.com/bbs/dpj-108037-1.html

Keil代碼下載與Proteus仿真下載:
05_雙機(jī)通信.7z (63.03 KB, 下載次數(shù): 79)




作者: sfdsl1    時(shí)間: 2021-12-4 23:15
VSPD是為windows系統(tǒng)添加虛擬串口的軟件




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1