找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1653|回复: 1
收起左侧

自学51单片机,关于串口通信中断程序问题

[复制链接]
ID:602249 发表于 2019-8-21 10:05 来自手机 | 显示全部楼层 |阅读模式
波特率4800,smod置1翻倍,串口中断里的switch 语局直接跳过,也能在 pc上看到收发的数子。

单片机源程序如下:
  1. #include<reg51.h>
  2. #include<intrins.h>
  3. #define led P2
  4. typedef unsigned char u8;
  5. typedef unsigned int u16;
  6. sbit LLa=P2^2;
  7. sbit LLb=P2^3
  8. ;sbit LLc=P2^4;
  9. sbit beep=P1^5;
  10. u8 code a[]={ 0x3f  , 0x06 , 0x5b , 0x4f , 0x66 , 0x6d , 0x7d , 0x07 ,             0x7f  ,  0x6f ,  0x77 ,  0x7c,    0x39 , 0x5e , 0x79 , 0x71 };


  11. void delay (u16  i)
  12. {  while (i--);}        


  13.   void  fengmingqi ()
  14. {  u16  i=1000;
  15. while (i)  
  16. {    beep=~beep;        delay(100) ;i--;   }}


  17. void shuomaguan ()
  18. {    u8 i;     
  19. LLa=1;   
  20. LLb=1;
  21.    LLc=1;  
  22. for (i=0;i<16;i++)  
  23.   {          P0=a[i];          delay(50000) ;        }}
  24. void ledshanguang()        
  25. {  u8 i;  
  26. led=0xfe;
  27. delay(50000);  
  28. while (1)  {   
  29. for (i=0;i<7;i++)   
  30. {       led=_crol_(led,1);           delay(50000);    }  
  31. for (i=0;i<7;i++)   
  32. {       led=_cror_(led,1);              delay(50000);    }  }}                                   


  33. void chuankouint()
  34. {   TMOD=0X20;   TH1=0XF3;   TL1=0XF3;   SCON=0X50;   PCON=0X80;   EA=1        ;   ES=1         ;   TR1=1  ;    }


  35. void main()                                                                              /***********主函数**********/
  36. {   
  37. chuankouint();
  38.           while(1) ;         }


  39. chuankouzhongduan() interrupt 4                                                 {                                                                        
  40.       u8 b;   
  41. b=SBUF;
  42.         RI=0;   
  43. switch (B)
  44. {    case 1:ledshanguang();break;
  45.    case 2:shuomaguan();break;   
  46.      case 3:fengmingqi();break;       }         
  47.     SBUF=b;   
  48.    while(!TI);   
  49.    TI=0;  
  50. }
复制代码
回复

使用道具 举报

ID:235200 发表于 2019-8-26 02:07 | 显示全部楼层
switch (B)中括号里的B应为小写,不知你程序编译通过没有,直接跳过此语句也能收到是因为串行中断里有发送程序:SBUF=b;   
   while(!TI);   
   TI=0;  每进入一次串行中断,均会把当前的b中内容发送回去,所以能看到数据。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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