The LED light of machine B can be lit by controlling the buttons of machine A, and numbers can be sent to machine A by controlling the buttons of machine B, and displayed using a digital tube.
Circuit diagram:
Machine code:
#include #define uchar unsigned char #define uint unsigned int sbit LED1=P1^0; sbit LED2=P1^3; sbit K1=P1^7; uchar Operation_No=0; //Operation code //Digital tube code uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //Delay void DelayMS(uint ms) { flying i; while(ms--) { for(i=0;i<120;i++); } } //Send characters to the serial port void Putc_to_SerialPort(uchar c) { SBUF=c; while(TI==0); TI=0; //TI hardware set to 1, software cleared to 0 } //Main program void main() { LED1=LED2=1; P0=0x00; SCON=0x50; //Serial port mode 1, allow receiving TMOD=0x20; //T1 working mode 2 PCON=0x00; //Baud rate is not doubled TH1=0xfd; TL1=0xfd; //9600 IF=RI=0; // TR1=1; IE=0x90; //Enable serial port interrupt while(1) { DelayMS(100); if(K1==0) //Select operation code 0, 1, 2, 3 when K1 is pressed { while(K1==0); { Operation_No=(Operation_No+1)%4; } switch(Operation_No) //Send A/B/C or stop sending according to the operation code { case 0: Putc_to_SerialPort('X'); LED1=LED2=1;break; case 1: Putc_to_SerialPort('A');LED1=~LED1;LED2=1;break; case 2: Putc_to_SerialPort('B'); LED2=~LED2;LED1=1; break; case 3: Putc_to_SerialPort('C'); LED1=~LED1;LED2=LED1; break; } } } } //Serial port receiving interrupt function of machine A void Serial_INT() interrupt 4 { if(RI) { RI=0; if(SBUF>=0&&SBUF<=9) { P0=DSY_CODE[SBUF]; } else { P0=0x00; } } } Machine B code: #include #define uchar unsigned char #define uint unsigned int sbit LED1=P1^0; sbit LED2=P1^3; sbit K2=P1^7; fly NumX=-1; //Delay void DelayMS(uint ms) { flying i; while(ms--) { for(i=0;i<120;i++); } } //Main program void main() { LED1=LED2=1; SCON=0x50; //Serial port mode 1, allow receiving TMOD=0x20; //T1 working mode 2 TH1=0xfd; //Baud rate 9600 TL1=0xfd; PCON=0x00; //Baud rate is not doubled SEE=OF=0; TR1=1; IE=0x90; while(1) { DelayMS(100); if(K2==0) { while(K2==0); NumX=++NumX; //Generate a number in the range of 0~10, where 10 means off SBUF=NumX; if(NumX==10) { NumX=-1; } while(TI==0); TI=0; //TI hardware set to 1, software cleared to 0 } } } void Serial_INT() interrupt 4 { if(RI) //If received, LED will be activated { RI=0; switch(SBUF) //Complete different actions according to the different command characters received { case 'X': LED1=LED2=1;break; //all off case 'A': LED1=0;LED2=1;break; //LED1 亮 case 'B': LED2=0;LED1=1;break; //LED2 亮 case 'C': LED1=LED2=0; //all on } } } Effect screenshots: search 会员中心 collect dynamic information creation Qingye Murong 2020-05-06 09:16:53 2089 51 single chip microcomputer This column includes this content 18 articles2 Subscribe51 MCU two-way communication example
Circuit diagram:
Machine code:
Machine B code:
Effect screenshots:
Previous article:51 MCU and computer communication, 51 MCU and PC communication method summary
Next article:Serial communication of 51 single chip microcomputer (I)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Guess: What is the origin of the code name of the open source "Wanli" Raspberry Pi car that went viral yesterday?
- E103-W01-BF Unboxing and Brief Introduction
- SensorTile.box trial (unboxing and mobile APP connection)
- [Silicon Labs Development Kit Review] +Si7021 Temperature and Humidity Sensor Detection
- RISC-V RVB2601 first experience--Section 3--IO simulation serial port completed
- Infineon Development Board 618 Promotion: The hot-selling list is revealed, with great gifts!
- Are there any people using Chinese MCU or Huada MCU in the forum?
- In the if else statement in C language, there is only one line of code after if. What is the impact of adding a semicolon after it?
- How to migrate SES project from GNU toolchain to Segger toolchain?
- 【GD32450I-EVAL】+ 09 Ethernet module test