Design and Implementation of a Multi-Machine Communication System Based on the 51 Microcontroller

This article introduces a design scheme for a multi-machine communication system based on the 51 microcontroller. The system achieves data exchange between a master device and multiple slave devices through a serial communication interface (UART), supporting address recognition and data verification functions. The article elaborates on the system’s hardware design, software programming, and communication protocol design, providing complete source code examples.

Introduction
With the increasing complexity of embedded system applications, multi-machine communication technology has become an important means of achieving distributed control and data exchange. The 51 microcontroller, due to its low cost, ease of programming, and widespread application base, has become a common choice for multi-machine communication system design.

System Design
Hardware Design
Master Device: The AT89C51 microcontroller is used as the master controller, responsible for sending commands and data, and controlling the entire communication network.
Slave Devices: Each slave device also uses the AT89C51 microcontroller to communicate with the master device through address recognition.
Serial Communication Interface: Data transmission is performed using the UART interface, with a baud rate set to 9600 bps.
Address Selection: Each slave device’s unique address (01H-03H) is set using a DIP switch.

Software Design
Communication Protocol:
Address Frame: The master sends an address frame (TB8=1) to select the target slave device.
Data Frame: The master sends a data frame (TB8=0) for data communication.
Control Commands:
0xFF: Puts all slave devices into multi-machine communication mode (SM2=1).
00H: Receive command.
01H: Send command.
Status Word:
D7 (ERR): Illegal command flag.
D1 (TRDY): Send ready flag.
D0 (RRDY): Receive ready flag.

Program Flow
Master:
Initialize the serial port (Mode 3, allow reception, TB8=1, SCON=0xD8).
Select the target slave device address using a key.
Send control command 0xFF.
Send address frame and data frame.
Slave:
Initialize the serial port (Mode 3, allow reception, TB8=1, SM2=1).
Receive control command 0xFF and enter multi-machine communication mode.
Detect address frame, match its own address, set SM2=0, and respond.
Receive data frame and process it.

Code Example

#include <reg51.h>
sbit k1 = P0^0;
sbit k2 = P0^1;
sbit k3 = P0^2;
sbit yellowLED = P2^0;
void initUART() {
SCON = 0xD8; // Mode 3, allow reception, TB8=1
TMOD = 0x20; // Timer 1 mode 2
TH1 = 0xFD; // Baud rate 9600
TR1 = 1;
EA = 1;
ES = 1;
}
void sendCommand(unsigned char cmd) {
SBUF = cmd;
while (!TI);
TI = 0;
}
void sendData(unsigned char addr, unsigned char data) {
TB8 = 1;
SBUF = addr;
while (!TI);
TI = 0;
TB8 = 0;
SBUF = data;
while (!TI);
TI = 0;
}
void main() {
initUART();
sendCommand(0xFF); // Initialize all slaves
while (1) {
if (k1 == 1) {
sendData(0x01, 0x55); // Send data to slave 1
yellowLED = 1; // Turn on LED
}
if (k2 == 1) {
sendData(0x02, 0x55); // Send data to slave 2
yellowLED = 1;
}
if (k3 == 1) {
sendData(0x03, 0x55); // Send data to slave 3
yellowLED = 1;
}
}
}

Entry Point of the Main Function:

#include <reg51.h>
sbit yellowLED = P2^0;
sbit greenLED = P1^0;
void initUART() {
SCON = 0xD8; // Mode 3, allow reception, TB8=1
TMOD = 0x20; // Timer 1 mode 2
TH1 = 0xFD; // Baud rate 9600
TR1 = 1;
EA = 1;
ES = 1;
}
void main() {
initUART();
yellowLED = 0;
greenLED = 0;
while (1) {
// Wait for receive interrupt
}
}
void serialISR() interrupt 4 {
if (RI) {
RI = 0;
unsigned char data = SBUF;
if (TB8 == 1) { // Address frame
if (data == 0x01) { // Match own address
SCON &= ~0x40; // SM2=0
yellowLED = 1;
}
} else { // Data frame
greenLED = ~greenLED; // Blink LED
SCON |= 0x40; // SM2=1
yellowLED = 0;
}
}
}

Conclusion
The multi-machine communication system designed in this article achieves reliable master-slave device communication through reasonable hardware design and software programming.

This system has advantages such as simple structure, low cost, and ease of expansion, making it suitable for industrial control, smart home, and other fields. By further optimizing the communication protocol and adding error detection mechanisms, the system’s stability and anti-interference capability can be improved.

Leave a Comment