The bottom-level essence of the microcontroller serial port​

Publisher:xxoke624Latest update time:2023-07-03 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In embedded development, UART serial port is the most common communication interface. Do you know why serial port is so common? This article will give you an in-depth understanding of the underlying nature of the serial port.

1. What is serial communication?

Serial communication refers to a communication method that can transmit data in bits using only one receiving line and one sending line. Although serial communication is slower than byte-by-byte parallel communication, a serial port can transmit data using only two wires.

Typical serial communication is completed using three wires, namely ground wire, sending and receiving. Because serial communication is asynchronous, the port can send data on one line while receiving data on another line. The most important parameters of serial communication are baud rate, data bits, stop bits and parity. For two ports that require serial communication, these parameters must match, which is also a prerequisite for serial communication.

Figure 1: Serial communication shows data transmission intention

2. What is the communication protocol for serial communication?

Initially, the data was a simple process quantity of analog signal output. Later, the RS232 interface appeared in the instrument interface. This interface can realize point-to-point communication, but this method cannot realize the networking function, which gave rise to RS485.

We know that the data transmission of serial communication is all 0 and 1. In single bus, I2C, and UART, the high and low levels of a line are used to determine logic 1 or logic 0. However, the GND of this signal line is not related to other devices. Forming co-ground mode communication, this co-ground mode transmission is prone to interference, and the anti-interference performance is relatively weak. Therefore, RS485, which has differential communication, supports multi-machine communication, and has strong anti-interference, is widely used.

The biggest feature of RS485 communication is that the transmission speed can reach more than 10Mb/s, and the transmission distance can reach about 3000 meters. What everyone needs to note is that although the maximum speed and maximum transmission distance of 485 are both very large, the transmission speed will slow down as the distance increases, so you cannot have both.

3. Physical layer of serial communication

There are many standards for the physical layer of serial communication. For example, as mentioned above, we mainly explain the RS-232 standard. The RS-232 standard mainly stipulates the use of signals, communication interfaces, and signal level standards.

In the above communication method, the "DB9 interface" of the two communication devices is connected through the serial port signal line, and the "RS-232 standard" is used in the serial port signal line to transmit data signals. Since the RS-232 level standard signals cannot be directly recognized by the controller, these signals will be converted through a "level conversion chip" into "TTL calibrated" level signals that the controller can recognize before communication can be achieved.

The picture below shows the DB9 standard serial communication interface:

DB9 pin description:

The table above is the standard connection method of the DB9 male connector on the computer side. Since the transceiver signals (RXD and TXD) between the two communication devices should be cross-connected, the DB9 female connector on the modem side is generally connected with the male connector. On the contrary, when connecting two devices, just use a "straight-through" serial port cable to connect them.

The RTS, CTS, DSR, DTR and DCD signals in the serial port line use logic 1 to indicate that the signal is valid, and logic 0 to indicate that the signal is invalid. For example, when the computer-side control DTR signal line is expressed as logic 1, it is to inform the remote modem that the machine is ready to receive data, and 0 means that it is not ready yet.

4. Baud rate

Baud rate refers to the modulation rate of the data signal to the carrier, which is expressed by the number of times the carrier modulation state changes per unit time;

For example, the baud rate is 9600bps; it means that 9600 bits are transmitted per second, which is equivalent to dividing each second into 9600 equal parts.

Therefore, the time per 1 bit is 1/9600 seconds = 104.1666...us. About 0.1ms. Since it is 9600 equal parts, that is, each 1bit is immediately followed by the next bit, and there is no extra interval. If two devices want to achieve serial communication, the baud rate set by the transceiver and receiver must be the same, otherwise communication cannot be achieved.

Communication can be achieved if the sending and receiving baud rates are the same:

animated cover


The transmitting and receiving baud rates are inconsistent, causing the RX end to be unable to receive normally:

animated cover


5. Data structure of serial communication

Start bit:  The start bit must be a logic 0 level that lasts for one bit time, marking the beginning of transmitting a character. The receiver can use the start bit to synchronize its receiving clock with the sender's data.

Data bits:  The data bits follow the start bit and are the real valid information in communication. The number of data bits can be agreed upon by both communicating parties. When transmitting data, the low-order bit of the character is transmitted first, and then the high-order bit of the character is transmitted.

Parity bit:  The parity bit only occupies one bit and is used for odd or even check. The parity bit is not necessary. If it is odd parity, it is necessary to ensure that the transmitted data has a total of odd number of logical high bits; if it is even parity, it is necessary to ensure that the transmitted data has a total of even number of logical high bits.

Stop bit:  The stop bit can be 1 bit, 1.5 bits or 2 bits, and can be set by software. It must be a logic 1 level, marking the end of transmitting a character.

Idle bit:  The idle bit refers to the period from the end of the stop bit of one character to the beginning of the start bit of the next character, indicating that the line is in an idle state and must be filled with a high level.

6. Single-duplex communication

Simplex:  Data transmission only supports data transmission in one direction;

Half-duplex:  allows data to be transmitted in both directions, but only allows data to be transmitted in one direction at a time. It is actually a simplex communication that switches directions and does not require independent receiving and transmitting ends. Can be combined into one port;

Full-duplex:  Allows data to be transmitted in both directions at the same time, so full-duplex communication is a combination of two simplex modes and requires independent receiving and transmitting ends.


7. Serial communication in STM32

There are two types of STM32 serial communication interfaces, namely: UART (Universal Asynchronous Receiver-Transmitter) and USART (Universal Synchronous Asynchronous Receiver-Transmitter). For the large-capacity STM32F10x series chips, there are three USARTs and two UARTs respectively.

TXD: data transmission pin; RXD: data input pin

For the connection between the two chips, the GND of the two chips is common ground, and TXD and RXD are cross-connected, so that TTL level communication can be carried out between the two chips.

But if the chip is connected to a PC, except for the common ground condition, the direct cross connection as above cannot be used. Although both have TXD and RXD pins, the PC usually uses the RS232 interface (9-pin), usually TXC and RXD are obtained through level conversion. Therefore, if you want the chip to communicate directly with the RS232 interface of the PC, you need to level-convert the input and output ports of the chip to the RS232 type and then cross-connect. The level standards of the two are different:

Microcontroller evaluation standards (TTL level): +5V means 1, 0V means 0;

RS232 level standard: +15/+13V means 0, -15/-13 means 1.

Therefore, the serial communication between the microcontroller and the PC should follow: between the serial port of the microcontroller and the RS232 port given by the host computer, the conversion between TTL level and RS232 level is achieved through a level conversion circuit.

If you use USB to serial port, you can also achieve serial communication. The USB to serial port circuit diagram is as follows

STM32 serial communication code

The corresponding library functions for serial port communication in STM32 have been built for you. When you use and configure the serial port, you can just call the library functions and configure them directly. Please refer to the code:

1. Initialization structure code


typedef struct {

 uint32_t USART_BaudRate; // baud rate

 uint16_t USART_WordLength; // word length

 uint16_t USART_StopBits; // Stop bits

 uint16_t USART_Parity; // Check digit

 uint16_t USART_Mode; // USART mode

 uint16_t USART_HardwareFlowControl; // Hardware flow control

 } USART_InitTypeDef;



2. NVIC configures interrupt priority


NVIC_Configuration(void)

{

  NVIC_InitTypeDef NVIC_InitStructure;


  /* Nested vector interrupt controller group selection*/

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


  /*Configure USART as interrupt source*/ 

  NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;

  /* Stealing priority*/

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  /* Sub-priority*/

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  /* Enable interrupts */

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  /* Initialize configuration NVIC */

  NVIC_Init(&NVIC_InitStructure);

}



3. USART configuration function


void DEBUG_USART_Config(void)

 GPIO_InitTypeDef GPIO_InitStructure;

 USART_InitTypeDef USART_InitStructure;


 /* Step 1: Initialize GPIO */

  // Turn on the clock of the serial port GPIO

 DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);

  //Configure the GPIO of USART Tx to push-pull multiplexing mode

 GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

[1] [2]
Reference address:The bottom-level essence of the microcontroller serial port​

Previous article:Common operations of EEPROM driver code
Next article:How to design a product using a microcontroller

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号