找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1732|回复: 3
打印 上一主题 下一主题
收起左侧

单片机多机通信仿真图中的数码管不亮,是哪里出错了,求帮助

[复制链接]
跳转到指定楼层
楼主

        单片机主机代码
  1.                 #include<reg52.h>
  2.                 #define uchar unsigned char
  3.                 #define uint unsigned int
  4.                 sbit LED1=P3^4;
  5.                 sbit LED2=P3^5;
  6.                 sbit MCU1=P2^2;
  7.                 sbit MCU2=P2^3;
  8.                 uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c};
  9.                 uchar keyscan(void);
  10.                 void  delayms(uchar t);
  11.                 void  init_serial(void);
  12.                 void  SBUFSend(uchar key);
  13.                 void  senddat(uchar addr,uchar dat);
  14.                 void  display(uchar addr,uchar dat);
  15.                 uchar keyscan()
  16.                 {
  17.                      uchar temp,key;
  18.                          P1=0xfe;
  19.                          temp=P1;
  20.                          temp=temp&0xf0;
  21.                          if(temp!=0xf0)
  22.                          {
  23.                                delayms(10);
  24.                                    if(temp!=0xf0)
  25.                                    {
  26.                                         temp=P1;
  27.                                                 switch(temp)
  28.                                                 {
  29.                                                     case 0xee:key=0;break;
  30.                                                         case 0xde:key=1;break;
  31.                                                         case 0xbe:key=2;break;
  32.                                                         case 0x7e:key=3;break;
  33.                                                 }
  34.                                                 while(temp!=0xf0)
  35.                                                 {
  36.                                                     temp=P1;
  37.                                                         temp=temp&0xf0;
  38.                                                 }
  39.                                         }
  40.                                 }
  41.                                 P1=0xfd;
  42.                                 temp=P1;
  43.                                 temp=temp&0xf0;
  44.                                 if(temp!=0xf0)
  45.                                 {
  46.                                       delayms(10);
  47.                                           if(temp!=0xf0)
  48.                                           {
  49.                                                temp=P1;
  50.                                                    switch(temp)
  51.                                                    {
  52.                                                            case 0xed:key=4;break;
  53.                                                            case 0xdd:key=5;break;
  54.                                                            case 0xbd:key=6;break;
  55.                                                            case 0x7d:key=7;break;
  56.                                                         }
  57.                                                         while(temp!=0xf0)
  58.                                                         {
  59.                                                             temp=P1;
  60.                                                 temp=temp&0xf0;
  61.                                                         }
  62.                                                 }
  63.                                         }
  64.                                         P1=0xfb;
  65.                                         temp=P1;
  66.                                     temp=temp&0xf0;
  67.                                     if(temp!=0xf0)
  68.                                         {
  69.                                              delayms(10);
  70.                                              if(temp!=0xf0)
  71.                                                  {
  72.                                                   temp=P1;
  73.                                                       switch(temp)
  74.                                                       {  
  75.                                                           case 0xeb:key=8;break;
  76.                                                           case 0xdb:key=9;break;
  77.                                                           case 0xbb:key=10;break;
  78.                                                           case 0x7b:key=11;break;
  79.                                                        }
  80.                                                        while(temp!=0xf0)
  81.                                                        {
  82.                                                            temp=P1;
  83.                                                    temp=temp&0xf0;
  84.                                                           }
  85.                                               }
  86.                                     }
  87.                                     P1=0xf7;
  88.                                     temp=P1;
  89.                                     temp=temp&0xf0;
  90.                                     if(temp!=0xf0)
  91.                                     {
  92.                                          delayms(10);
  93.                                              if(temp!=0xf0)
  94.                                              {
  95.                                                temp=P1;
  96.                                                    switch(temp)
  97.                                                    {
  98.                                                         case 0xe7:key=12;break;
  99.                                                                 case 0xd7:key=13;break;
  100.                                                                 case 0xb7:key=14;break;
  101.                                                                 case 0x77:key=15;break;
  102.                                                         }
  103.                                                         while(temp!=0xf0)
  104.                                                         {
  105.                                                              temp=P1;
  106.                                                  temp=temp&0xf0;
  107.                                                         }
  108.                                     }
  109.                                  }
  110.                                 return key;
  111.                         }
  112.                         void delayms(uchar t)
  113.                         {
  114.                             uchar i,j;
  115.                                 for(i=0;i<t;i++)
  116.                                 for(j=0;j<110;j++);
  117.                         }
  118.                         void init_serial(void)
  119.                         {
  120.                             TMOD=0X20;
  121.                                 TH1=0XFA;
  122.                                 TL1=0XFA;
  123.                                 PCON=0X80;
  124.                                 SCON=0X50;
  125.                                 TR1=1;
  126.                                 ES=1;
  127.                                 EA=1;
  128.                         }
  129.                         void SUBFSend(uchar key)
  130.                         {
  131.                             SBUF=key;
  132.                                 while(TI==0);
  133.                                 TI=0;
  134.                         }
  135.                         void senddat(uchar addr,uchar dat)
  136.                         {
  137.                              TB8=1;
  138.                                  SBUFSend(addr);
  139.                                  TB8=0;
  140.                                  SBUFSend(dat);
  141.                         }
  142.                         void display(uchar addr,uchar dat)
  143.                         {
  144.                              P0=tab[addr];
  145.                                  LED1=0;
  146.                                  LED2=1;
  147.                                  delayms(1);
  148.                                  P0=0x00;
  149.                                  P0=tab[dat];
  150.                                  LED1=1;
  151.                                  LED2=0;
  152.                                  delayms(1);
  153.                                  P0=0X00;
  154.                         }
  155.                         void main()
  156.                         {
  157.                             char addr,dat;
  158.                                 init_serial();
  159.                                 dat=keyscan();
  160.                         while(1){
  161.                              if(MCU1==1)
  162.                                  {
  163.                                      addr=1;
  164.                                          dat=keyscan();
  165.                                          senddat(1,dat);
  166.                                      display(1,dat);
  167.                                  }
  168.                                  if(MCU2==0)
  169.                                  {
  170.                                     addr=2;
  171.                                         dat=keyscan();
  172.                                         senddat(2,dat);
  173.                                         display(2,dat);
  174.                                  }
  175.                                  }
  176.                         }
  177.                         void serial_int() interrupt 4
  178.                         {
  179.                             if(RI==1)
  180.                                 {
  181.                                    RI=0;
  182.                                 }
  183.                                    else TI=0;
  184.                         }
  185. 从机1代码
  186.                 #include <reg52.h>
  187.                 #define uchar unsigned char
  188.                 sbit LED1=P2^0;
  189.                 sbit LED2=P2^1;
  190.                 uchar DispCode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  191.                 //------------
  192.                 void SerialInit()
  193.                 {
  194.                      SCON=0xf0;   //方式3 允许接收,SM2=1
  195.                          PCON=0x00;   //串口波特率不加倍
  196.                          TMOD=0x20;   //定时器1,方式2
  197.                          TH1=0xfd;    //波特率为9600
  198.                          TL1=0xfd;
  199.                          TR1=1;                  //启动定时器
  200.                          ES=1;
  201.                          EA=1;
  202.                 }
  203.                 //------------
  204.                 void SBUFSend(uchar Ch)
  205.                 {
  206.                     SBUF=Ch;
  207.                         while(TI==0)
  208.                         {
  209.                         }
  210.                         TI=0;
  211.                 }
  212.                 //------------
  213.                 void main()
  214.                 {
  215.                    SerialInit();                  
  216.                    while(1)
  217.                    {
  218.                    }
  219.                 }
  220.                 //------------
  221.                 void SerialServer() interrupt 4
  222.                 {
  223.                     uchar Ch;
  224.                         if(RI==1)
  225.                         {   uchar i;
  226.                             RI=0;
  227.                                 Ch=SBUF;
  228.                                 if(RB8==1)
  229.                                 {
  230.                                     if(Ch==1)
  231.                                         {
  232.                                            SM2=0;
  233.                                            SBUFSend(1);
  234.                                            LED1=0;
  235.                                            LED2=1;
  236.                         for(i=500;i>0;i--)
  237.                         P0=DispCode[Ch];
  238.                                    }
  239.                                    else
  240.                                    {
  241.                                       SM2=1;
  242.                                    }
  243.                                 }         
  244.                                 if(RB8==0)
  245.                                 {  P0=0x00;
  246.                                    LED1=1;
  247.                                    LED2=0;
  248.                                 for(i=500;i>0;i--)
  249.                  P0=DispCode[Ch];
  250.                               SM2=1;
  251.                                 }
  252.                         }
  253.                 }
  254. 从机2代码
  255.                 #include<reg52.h>
  256.                 #define uchar unsigned char
  257.                 sbit LED1=P2^0;
  258.                 sbit LED2=P2^1;
  259.                 uchar DispCode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  260.                 //------------
  261.                 void SerialInit()
  262.                 {
  263.                      SCON=0xf0;   //方式3 允许接收,SM2=1
  264.                          PCON=0x00;   //串口波特率不加倍
  265.                          TMOD=0x20;   //定时器1,方式2
  266.                          TH1=0xfd;    //波特率为9600
  267.                          TL1=0xfd;
  268.                          TR1=1;                  //启动定时器
  269.                          ES=1;
  270.                          EA=1;
  271.                 }
  272.                 //------------
  273.                 void SBUFSend(uchar Ch)
  274.                 {
  275.                     SBUF=Ch;
  276.                         while(TI==0)
  277.                         {
  278.                         }
  279.                         TI=0;
  280.                 }
  281.                 //------------
  282.                 void main()
  283.                 {
  284.                    SerialInit();                  
  285.                    while(1)
  286.                    {
  287.                    }
  288.                 }
  289.                 //------------
  290.                 void SerialServer() interrupt 4
  291.                 {
  292.                     uchar Ch;
  293.                         if(RI==1)
  294.                         {   uchar i;
  295.                             RI=0;
  296.                                 Ch=SBUF;
  297.                                 if(RB8==1)
  298.                                 {
  299.                                     if(Ch==2)
  300.                                         {
  301.                                            SM2=0;
  302.                                            SBUFSend(2);
  303.                                            LED1=0;
  304.                                            LED2=1;
  305.                         for(i=500;i>0;i--)
  306.                         P0=DispCode[Ch];
  307.                                    }
  308.                                    else
  309.                                    {
  310.                                       SM2=1;
  311.                                    }
  312.                                 }         
  313.                                 if(RB8==0)
  314.                                 {  P0=0x00;
  315.                                    LED1=1;
  316.                                    LED2=0;
  317.                                 for(i=500;i>0;i--)
  318.                  P0=DispCode[Ch];
  319.                               SM2=1;
  320.                                 }
  321.                         }
  322.                 }
复制代码




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:213173 发表于 2019-3-13 07:50 | 只看该作者
共阴数码管P0不接上拉电阻怎么亮?
回复

使用道具 举报

板凳
ID:414311 发表于 2019-3-13 10:24 | 只看该作者
wulin 发表于 2019-3-13 07:50
共阴数码管P0不接上拉电阻怎么亮?

谢谢,但是是接了上拉电阻,就是RP1,RP2,RP3
回复

使用道具 举报

地板
ID:257890 发表于 2019-3-13 17:00 | 只看该作者
preteus  仿真的效果 跟电脑本身的性能 也有关系,   延时过短 就是显示 不出来  你可以分步骤 验证!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表