標題: 單片機之間的串口通信實例 proteus仿真及源碼 [打印本頁]

作者: 51黑ff    時間: 2016-10-10 13:22
標題: 單片機之間的串口通信實例 proteus仿真及源碼
給51黑電子論壇的朋友們分享一個單片機之間的串口通信實例。下面是仿真原理圖:


單片機串口通信仿真工程文件及所有完整程序等資料下載地址(壓縮包一共有45個單片機仿真,全部下載后找到這個項目即可):
http://www.torrancerestoration.com/bbs/dpj-56298-1.html

單片機主機程序:
  1. /********************************************************************
  2.                             天馬電子
  3. *********************************************************************/
  4. #include <reg52.h>
  5. #define uchar unsigned char
  6. #define uint  unsigned  int        
  7. uchar duan[10]={0xc0,0Xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};         //所需的段的位碼
  8. //uchar wei[4]={0XEf,0XDf,0XBf,0X7f};                                 //位的控制端        (開發(fā)板)
  9. uchar wei[4]={0X80,0X40,0X20,0X10};                                 //位的控制端        (仿真)
  10. uint z,x,c,v, date;        //定義數(shù)據(jù)類型

  11. sbit P2_0=P2^0;
  12. sbit P2_1=P2^1;
  13. sbit P2_2=P2^2;
  14. sbit P2_3=P2^3;
  15. /******************************************************************

  16. 延時函數(shù)

  17. ******************************************************************/
  18. void delay(uchar t)
  19. {
  20.   uchar i,j;
  21.    for(i=0;i<t;i++)
  22.    {
  23.             for(j=13;j>0;j--);
  24.          { ;
  25.          }
  26.    }
  27. }

  28. /**********************************************************************
  29.                 數(shù)碼管動態(tài)掃描
  30. *********************************************************************/
  31. void xianshi()
  32. {
  33. /*****************數(shù)據(jù)轉(zhuǎn)換*****************************/
  34.   z=date/1000;                         //求千位
  35.   x=date%1000/100;                 //求百位
  36.   c=date%100/10;                 //求十位
  37.   v=date%10;                         //求個位

  38.       P2=wei[0];
  39.           P0=duan[z];
  40.           delay(50);  
  41.             P2=wei[1];
  42.       P0=duan[x];
  43.           delay(50);  
  44.              P2=wei[2];
  45.       P0=duan[c];
  46.           delay(50);  
  47.       P2=wei[3];
  48.       P0=duan[v];
  49.           delay(50);  
  50.                                        

  51. }
  52. /*************************************************************************
  53.                      串口初始化,波特率9600,方式1
  54. **************************************************************************/
  55. void Init_Com(void)
  56. {
  57. TMOD = 0x20;  //16位定時計數(shù)器
  58. PCON = 0x00;  //電源控制寄存器
  59. SCON = 0x50;  //串口寄存器
  60. TH1 = 0xFd;
  61. TL1 = 0xFd;
  62. TR1 = 1;
  63. }

  64. /*************************************************************************
  65.                                 主函數(shù)        
  66. **************************************************************************/
  67. void main()
  68. {
  69.    Init_Com();//串口初始化
  70.   // date=1234;
  71.   P1=0X0A;
  72.         while(1)
  73.    {
  74.    if(P2_0==0)P1=0XAA;
  75.    if(P2_1==0)P1=0X55;
  76.    if(P2_2==0)P1=0X0F;
  77.    if(P2_3==0)P1=0XF0;
  78.    SBUF=P1;
  79.    while(!TI);
  80.    TI=0;
  81.    }
  82. }
  83.                                                                                             
復(fù)制代碼



單片機從機程序:
  1. /********************************************************************
  2.                             天馬電子
  3. *********************************************************************/
  4. #include <reg52.h>
  5. #define uchar unsigned char
  6. #define uint  unsigned  int        
  7. uchar duan[10]={0xc0,0Xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};         //所需的段的位碼
  8. //uchar wei[4]={0XEf,0XDf,0XBf,0X7f};                                 //位的控制端        (開發(fā)板)
  9. uchar wei[4]={0X80,0X40,0X20,0X10};                                 //位的控制端        (仿真)
  10. uint z,x,c,v, date;        //定義數(shù)據(jù)類型
  11. /******************************************************************

  12. 延時函數(shù)

  13. ******************************************************************/
  14. void delay(uchar t)
  15. {
  16.   uchar i,j;
  17.    for(i=0;i<t;i++)
  18.    {
  19.             for(j=13;j>0;j--);
  20.          { ;
  21.          }
  22.    }
  23. }

  24. /**********************************************************************
  25.                 數(shù)碼管動態(tài)掃描
  26. *********************************************************************/
  27. void xianshi()
  28. {
  29. /*****************數(shù)據(jù)轉(zhuǎn)換*****************************/
  30.   z=date/1000;                         //求千位
  31.   x=date%1000/100;                 //求百位
  32.   c=date%100/10;                 //求十位
  33.   v=date%10;                         //求個位

  34.       P2=wei[0];
  35.           P0=duan[z];
  36.           delay(50);  
  37.             P2=wei[1];
  38.       P0=duan[x];
  39.           delay(50);  
  40.              P2=wei[2];
  41.       P0=duan[c];
  42.           delay(50);  
  43.       P2=wei[3];
  44.       P0=duan[v];
  45.           delay(50);  
  46.                                        

  47. }
  48. /*************************************************************************
  49.                      串口初始化,波特率9600,方式1
  50. **************************************************************************/
  51. void Init_Com(void)
  52. {
  53. TMOD = 0x20;
  54. PCON = 0x00;
  55. SCON = 0x50;
  56. TH1 = 0xFd;
  57. TL1 = 0xFd;
  58. TR1 = 1;
  59. }

  60. /*************************************************************************
  61.                                 主函數(shù)        
  62. **************************************************************************/
  63. void main()
  64. {
  65.    Init_Com();//串口初始化
  66.   // date=1234;
  67.         while(1)
  68.    {   
  69.    while(!RI);
  70.    P1=~SBUF;
  71.    RI=0;                  
  72.    
  73.    }
  74. }
復(fù)制代碼


作者: Mos.杏子    時間: 2018-5-9 09:14
感覺很有用
作者: ntwxd    時間: 2018-5-9 10:05
牛,學(xué)習(xí)了
作者: niuniniu    時間: 2018-6-20 11:00
牛逼  很有用
作者: chenfynh    時間: 2018-12-8 16:40
正在查這類的程序,剛好可以參考,感謝分享.

作者: Mos.杏子    時間: 2019-1-4 10:10
你好  為什么顯示器上只顯示U呢?
作者: 蕪湖    時間: 2020-4-27 08:43
你好,請問為什么我開始運行之后會很卡,并且界面找不到鼠標呢
作者: kevind    時間: 2020-5-6 22:34
感謝樓主分享,學(xué)習(xí)了
作者: 黑天科技    時間: 2021-3-18 23:13
試一下,不知道好不好




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