Introduction In embedded system development, serial communication is one of the fundamental technologies for data exchange between devices. UART (Universal Asynchronous Receiver/Transmitter) is the most commonly used serial communication interface, widely applied in 51 microcontroller systems due to its simplicity, reliability, and low resource consumption. This article will detail the implementation method of a dual-machine communication system based on the 51 microcontroller using UART, including hardware connection design, software code implementation, and system testing process, providing a clear practical reference for embedded beginners.
System Design Principles The core of the dual-machine communication system lies in data transmission between two devices via UART. In this system, we use two AT89C51 microcontrollers, one as the transmitter (Master) and the other as the receiver (Slave). The transmitter sends data through the serial port, and the receiver processes the received data and can send data back. The system adopts a standard communication format of 8 data bits, 1 stop bit, and no parity bit, with a baud rate set to 115200bps.
Hardware Connection Design The hardware connection for dual-machine communication is very simple; it only requires connecting the TXD (transmit pin) of the transmitter to the RXD (receive pin) of the receiver, and connecting the TXD of the receiver to the RXD of the transmitter, forming a loop. For the 51 microcontroller, TXD corresponds to pin P3.1, and RXD corresponds to pin P3.0. The specific connections are as follows:
Transmitter P3.1 (TXD) → Receiver P3.0 (RXD)
Receiver P3.1 (TXD) → Transmitter P3.0 (RXD)
Since the 51 microcontroller uses TTL levels, it can be connected directly without additional level conversion chips. The entire system can achieve communication with simple jumpers.
Software Design Implementation The software design is divided into two parts: the transmitter and the receiver, with the core being the implementation of UART initialization, data sending, and receiving functions.Transmitter Code
#include<reg52.h>
// Include 51 microcontroller register definitions// Define baud rate, using 115200bps#define BAUD_RATE 115200// Function declarationsvoid UART_Init(void);void UART_SendChar(char data);void Delay1ms(unsigned int ms);void main(){ // Initialize UART UART_Init(); // Main loop, periodically send characters while(1) { // Send character ‘A’ UART_SendChar(‘A’); // Delay 1 second Delay1ms(1000); // Send character ‘B’ UART_SendChar(‘B’); // Delay 1 second Delay1ms(1000); // Send character ‘C’ UART_SendChar(‘C’); // Delay 1 second Delay1ms(1000); }}// UART initialization functionvoid UART_Init(void){ // Set serial port working mode to mode 1 (8 data bits, 1 stop bit, variable baud rate) TMOD &= 0x0F; // Clear T1 mode bits TMOD |= 0x20; // Set T1 to mode 2 (8-bit auto-reload) // Set baud rate, TH1 value for 115200bps at 12MHz crystal // Calculation formula: TH1 = 256 – (crystal frequency/(12*baud rate)) // 12MHz/(12*115200) = 8.68 ≈ 9, 256-9=247=0xF7 // But the commonly used value is 0xE7 (231), corresponding to 115200bps TH1 = 0xE7; TL1 = 0xE7; // Enable serial port reception, SCON=0x50 indicates 8 data bits, 1 stop bit, allow reception SCON = 0x50; // Start timer 1 TR1 = 1; // Enable serial port interrupt ES = 1;}// Function to send a single charactervoid UART_SendChar(char data){ // Write data to send buffer SBUF SBUF = data; // Wait for sending to complete, TI flag is 1 indicates sending complete while(TI == 0);// Clear TI flag TI = 0;}// Delay function (1ms)void Delay1ms(unsigned int ms){ unsigned int i, j; for(i = 0; i < ms; i++) for(j = 0; j < 110; j++);}// Serial port interrupt service routinevoid UART_ISR(void) interrupt 4{// Receive interrupt if(RI == 1) { // Data received, can be processed here RI = 0; // Clear RI flag } // Send interrupt (not processed here, as it has already waited for TI during sending)}
Receiver Code
#include <reg52.h>
// Include 51 microcontroller register definitions// Define baud rate, consistent with the transmitter#define BAUD_RATE 115200// Function declarationsvoid UART_Init(void);void UART_SendChar(char data);void Delay1ms(unsigned int ms);void main(){ // Initialize UART UART_Init(); // Main loop, receive and echo data while(1) { // Wait for reception to complete, RI flag is 1 indicates reception complete while(RI == 0); // Read received data char data = SBUF; // Clear RI flag RI = 0; // Echo the received character back to the transmitter UART_SendChar(data); // Delay 100ms Delay1ms(100); }}// UART initialization function (same as transmitter)void UART_Init(void){ // Set serial port working mode to mode 1 (8 data bits, 1 stop bit, variable baud rate) TMOD &= 0x0F; // Clear T1 mode bits TMOD |= 0x20; // Set T1 to mode 2 (8-bit auto-reload) // Set baud rate, TH1 value for 115200bps at 12MHz crystal TH1 = 0xE7; TL1 = 0xE7; // Enable serial port reception SCON = 0x50; // 0101 0000 -> 8 data bits, 1 stop bit, allow reception // Start timer 1 TR1 = 1; // Enable serial port interrupt ES = 1;}// Function to send a single character (same as transmitter)void UART_SendChar(char data){ SBUF = data; while(TI == 0); TI = 0;}// Delay function (1ms)void Delay1ms(unsigned int ms){ unsigned int i, j; for(i = 0; i < ms; i++) for(j = 0; j < 110; j++);}
System Testing and Results
After completing the code writing and hardware connections, use a serial port debugging tool (such as SecureCRT, Putty, etc.) for system testing:
1. Program the transmitter and receiver with the above code
2. Connect the receiver to the PC’s USB to serial module
3. Open the serial port debugging tool, set the baud rate to 115200, 8 data bits, 1 stop bit, no parity
4. Run the system and observe the serial port output test results:
• The transmitter sends ‘A’, ‘B’, ‘C’ every 1 second
• The receiver immediately echoes the received data
• The serial port debugging tool displays: A A B B C C …
Testing shows that the system operates stably, data transmission is accurate, and communication is normal.
Conclusion and Application Prospects This article has detailed the implementation process of a dual-machine communication system based on the 51 microcontroller using UART. Through simple hardware connections and clear software design, reliable communication between two 51 microcontrollers has been successfully achieved. This system has the following characteristics:
1. Simple structure: only two microcontrollers and basic connecting wires are needed
2. Low cost: hardware costs are extremely low, suitable for teaching and small projects
3. Easy to implement: the code is concise, allowing beginners to quickly grasp it
4. High reliability: UART communication has high reliability over short distances
This system can be widely applied in the following scenarios:
• Sensor data acquisition systems
• Multi-device collaborative control systems
• Teaching experiment platforms
• Low-speed data transmission applications
Future improvements may consider:
• Adding data verification mechanisms to enhance communication reliability
• Expanding to a multi-machine communication network
• Optimizing baud rate settings to support higher transmission rates
• Adding hardware flow control to implement more complex communication protocols

Summary The UART serial communication system based on the 51 microcontroller is a fundamental and important practical case in embedded system development. This article provides readers with a complete implementation plan through detailed principle explanation, code implementation, and system testing. This system not only helps to understand the basic principles of serial communication but also lays the foundation for designing more complex communication systems. For embedded system beginners, this is an ideal introductory practical project that can quickly enhance their understanding and application capabilities of hardware interfaces and communication protocols. Through the study and practice of this system, readers can master the core technologies of UART communication, laying a solid foundation for further learning of more complex communication protocols (such as SPI, I2C, CAN, etc.). In practical applications, such serial communication systems remain the preferred solution for data exchange between many embedded devices, with their simplicity, reliability, and low cost ensuring their lasting viability in various application scenarios.