找回密码
 立即注册

QQ登录

只需一步,快速开始

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

我的单片机双机通信,含源码

[复制链接]
ID:350999 发表于 2018-6-13 12:10 | 显示全部楼层 |阅读模式
本帖最后由 18789007389 于 2018-6-13 12:12 编辑

  1. #include <reg52.h>
  2. #define uint unsigned int
  3. #define uchar unsigned char
  4. sbit LED1 = P1^0;
  5. sbit LED2 = P1^3;
  6. sbit K1 = P1^7;
  7. uchar Operation_NO = 0;
  8. uchar code DSY_CODE[]=
  9. {
  10.          0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
  11. };

  12. void Delay(uint x)
  13. {
  14.          uchar i;
  15.         while(x--)
  16.         {
  17.                  for(i=0;i<120;i++);
  18.         }
  19. }

  20. void putc_to_SerialPort(uchar c)
  21. {
  22.          SBUF = c;
  23.         while(TI == 0);
  24.         TI = 0;
  25. }

  26. void main()
  27. {
  28.          LED1=LED2=1;
  29.         P0 = 0x00;
  30.         SCON = 0x50;
  31.         TMOD = 0x20;
  32.         PCON = 0x00;
  33.         TH1  = 0xfd;
  34.         TL1  = 0xfd;
  35.         TI   = 0;
  36.         RI   = 0;
  37.         TR1  = 1;
  38.         IE   = 0x90;
  39.         while(1)
  40.         {
  41.                  Delay(100);
  42.                 if(K1 == 0)
  43.                 {
  44.                          while(K1==0);
  45.                         Operation_NO=(Operation_NO+1)%4;
  46.                         switch(Operation_NO)
  47.                         {
  48.                                  case 0:
  49.                                                 putc_to_SerialPort('X');
  50.                                                 LED1=LED2=1; break;
  51.                                 case 1:
  52.                                                 putc_to_SerialPort('A');
  53.                                                 LED1=0;LED2=1;break;
  54.                                 case 2:
  55.                                                 putc_to_SerialPort('B');
  56.                                                 LED2=0;LED1=1;break;
  57.                                 case 3:
  58.                                                 putc_to_SerialPort('C');
  59.                                                 LED1=0;LED2=0;break;
  60.                         }
  61.                 }
  62.         }
  63. }

  64. void Serial_INT() interrupt 4
  65. {
  66.          if(RI)
  67.         {
  68.                  RI = 0;
  69.                 if(SBUF>=0&&SBUF<=9)
  70.                         P0 = DSY_CODE[SBUF];
  71.                 else
  72.                         P0 = 0x00;
  73.         }
  74. }
复制代码


回复

使用道具 举报

ID:350999 发表于 2018-6-13 12:11 | 显示全部楼层
#include <reg52.h> #define uint unsigned int #define uchar unsigned char sbit LED1 = P1^0; sbit LED2 = P1^3; sbit K1 = P1^7; uchar NumX = 0xff; void Delay(uint x) {          uchar i;         while(x--)         {                  for(i=0;i<120;i++);         } }  void main() {          LED1=LED2=1;         SCON = 0x50;         TMOD = 0x20;         PCON = 0x00;         TH1  = 0xfd;         TL1  = 0xfd;         TI   = 0;         RI   = 0;         TR1  = 1;         IE   = 0x90;         while(1)         {                  Delay(100);                 if(K1==0);                 {                          while(K1==0);                         NumX = (NumX+1)%11;                         SBUF = NumX;                         while(TI == 0);                         TI = 0;                 }         } }  void Serial_INT() interrupt 4 {          if(RI)         {                  RI = 0;                 switch(SBUF)                 {                         case 'X': LED1=1;LED2=1;break;                         case 'A': LED1=0;LED2=1;break;                         case 'B': LED2=0;LED1=1;break;                         case 'C': LED1=0;LED2=0;                 }         } }
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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