Complete Analysis of MCU Serial Communication Protocol

Complete Analysis of MCU Serial Communication Protocol

Dear friends, hello everyone! Today, the topic we are going to learn about is the serial communication protocol of microcontrollers (MCUs). This content is quite important and relatively professional, but I will try to explain it in a simple and understandable way. First, let’s see why we need serial communication?

Communication is the Basis of Interconnection
In modern embedded systems, microcontrollers or MCUs often need to exchange data with other devices, such as PCs, mobile phones, and other microcontrollers. Just like how we communicate with each other using language, microcontrollers also need a “language”—that is, a communication protocol—to work together. Serial communication is one of these “languages”.

What is Serial?
We know that data transmission inside computers is parallel, meaning multiple binary bits are transmitted simultaneously. Serial, on the other hand, transmits one bit at a time. It’s like saying the four characters “老马识途” (Old Horse Knows the Way) all at once—this is parallel. Saying each character one by one is serial.

Why Use Serial Communication?

  1. From an economic perspective, parallel communication requires more data lines, making wiring and connection costs higher. Serial only requires one data line, making it easier and cheaper to wire.
  2. Additionally, parallel cable lengths are limited, generally not exceeding 10 meters. Serial can transmit much farther, sometimes even up to thousands of meters.
  3. Parallel communication is prone to interference and has poor anti-interference ability. Serial interface levels are generally lower, offering stronger anti-interference capability.

Therefore, in situations where long distances, low costs, and anti-interference are required, serial communication has clear advantages. Of course, its transmission speed is lower than that of parallel communication, but it is still sufficient for most applications.

What Are the Common Serial Protocols?
Common serial communication protocols include: UART (Asynchronous Serial), I2C, SPI, CAN, etc. Each protocol has different characteristics and uses. The most common is the asynchronous serial.

Principle of Asynchronous Serial Communication
The communication method of asynchronous serial is asynchronous, where sending and receiving are determined by pre-agreed transmission rates, data bits, stop bits, and parity bits to define the frame format. Let me explain this asynchronous communication with a real-life example.

For instance, if you and a friend agree to meet at the park gate at 10 AM, your friend will arrive at 10 AM, and you will also go to the gate at 10 AM to meet. This is an “asynchronous” method. You don’t need any beats or synchronization signals; as long as everyone follows the agreed time, you can meet perfectly.

Similarly, both parties in asynchronous serial communication need to agree on parameters like baud rate and data bit length in advance. As long as the parameters match, data can be reliably transmitted.

The microcontroller’s serial port typically has a UART module that automatically converts parallel data into serial and generates and recognizes the synchronous frame format. So, as long as we set up the UART, we can easily use it.

UART Setup Code:

#define UART_BAUD 9600   // Baud rate
void InitUART(void) {
    TMOD = 0x20;  // Timer1 Mode 2 (8-bit auto-reload)
    TH1 = (65536 - (FOSC/384/UART_BAUD))/256; // Calculate initial value
    TL1 = (65536 - (FOSC/384/UART_BAUD))%256;
    TR1 = 1;      // Start Timer1
    SCON = 0x50;  // Allow reception, 8-bit data
    ES = 1;       // Enable interrupt
}

Send Function:

void UART_SendByte(uint8_t dat) {
    while(!TI); // Wait for sending to complete
    TI = 0;     // Clear send complete flag
    SBUF = dat; // Write to send register
}

// Send string, 0 terminated
void UART_SendStr(uint8_t *str) {
    while(*str) {
        UART_SendByte(*str++);
    }
}

By now, do you have a basic understanding of serial communication? Of course, there are many aspects not covered, such as:

  • How to achieve multi-device communication? Set slave addresses.
  • How to detect error frames? Parity bits.
  • How to interrupt communication? Handshake protocols.
  • How to improve efficiency? DMA, FIFO, etc.

But these will be left for you to learn gradually in practice!

Common Issues:

  1. Serial Port Freezing
    Possible reasons: Baud rate, data bits, and other parameters do not match; microcontroller operating voltage is too low, etc.

  2. Data Reception Errors
    Possible reasons: Baud rate, frame format incorrect; severe interference, requiring hardware anti-interference; data lines too long, signal attenuation, etc.

Tips:
For debugging convenience, you can first use serial port assistant software to communicate with the microcontroller to check if the communication is normal. You can also try discrete circuits for serial communication to deepen your understanding of the principles.

Recommended Tools:
The most commonly used tools for debugging serial ports are serial cables and serial assistant software. Additionally, using an oscilloscope to view waveforms in real-time is very helpful.

Alright, that’s all for today’s discussion on serial communication! If you encounter any questions or need further technical guidance in practice, feel free to consult me anytime! Learning about microcontrollers can be a bit tedious, but as long as you persevere, it will definitely make your DIY engineer life more exciting! Let’s keep working hard and look forward to the next technical sharing!

Leave a Comment