STM32 HAL library [serial communication] 01

Directory

  • 1. Serial protocol
    • 1. Serial protocol
      • About RS-232
        • 1) Serial port protocol standard
        • 2) Serial port pin definition
        • 3) Disadvantages of serial port
    • 2.Level standard
    • 3. The working principle of the “USB/TTL to 232” module (CH340 chip module as an example)
  • 2. USART serial communication of STM32 (HAL library)
      • USART
      • Question requirements
    • 1 Send “Hello Windows”
      • 1) CubeMX configuration
      • 2) Open the generated keil file
    • 2 Control data sending and receiving through serial port assistant
      • Simulation waveform

1. Serial port protocol

Understand the serial port protocol and the difference between RS-232 levels and TTL levels; and the working principle of the “USB/TTL to 232” module (taking the CH340 chip module as an example).

1. Serial port protocol

Commonly used serial communication protocols include RS-232, RS-485 and TTL.

About RS-232

RS-232 (also known as EIA RS-232) is one of the commonly used serial communication interface standards. It was jointly developed by the Electronic Industries Association (EIA) in 1970 in conjunction with Bell Systems, modem manufacturers, and computer terminal manufacturers.

It has the characteristics of fewer signal lines, flexible baud rate selection, negative logic transmission and long transmission distance.

In serial communication, both communicating parties are required to use a standard interface so that different devices can be easily connected for communication. The RS-232-C interface (also known as EIARS-232-C) is currently the most commonly used serial communication interface. (The “-C” in “RS-232-C” just indicates the version of RS-232, so the abbreviation is the same as “RS-232”)

It is a standard for serial communications jointly developed by the Electronic Industries Association (EIA) in 1970 in conjunction with the Bell System, modem manufacturers and computer terminal manufacturers. Its full name is “Technical Standard for Serial Binary Data Exchange Interface between Data Terminal Equipment (DTE) and Data Communications Equipment (DCE)”. The standard specifies the use of a 25-pin DB-25 connector, with each The signal content of each pin is specified, and the levels of various signals are also specified. Later, IBM’s PC simplified RS232 into a DB-9 connector, which became the de facto standard. The RS-232 port of industrial control generally uses only three lines: RXD, TXD, and GND.
Male and female interface:

1) Serial port protocol standard

The full name of the RS-232C standard (protocol) is the EIA-RS-232C standard, where EIA (Electronic Industry Association) represents the Electronic Industry Association, RS (recommended standard) represents the recommended standard, and 232 is the identification number. This standard is for serial communication. The physical interface and logic level of the RS232 are specified, and its output level is called the RS232 level. C represents the latest modification of RS232 (1969). Before that, there were RS232B and RS232A. It specifies connecting cables and machinery, electrical characteristics, signal functions and transmission processes. Commonly used physical standards include EIARS-422A, EIA RS-423A, and EIARS-485. Only EIA RS-232C (referred to as 232, RS232) is introduced here. For example, the current COM1 and COM2 interfaces on IBM PCs are RS-232C interfaces.

2) Serial port pin definition

The simplest RS232 communication consists of three data lines, namely TXD, RXD and GND. RS232 uses negative logic levels, that is, -15V ~ -3V represents logic “1”, and +3V ~ +15V represents logic “0”. The level here is the voltage of the TxD line (or RxD line) relative to GND.

9-core Signal direction comes from Abbreviation Description Remarks
1 Modem CD Carrier Detect – –
2 Modem RXD Receive data (terminal to computer) RS-232 receive
3 PC TXD send data (Computer to terminal) RS-232 sending
4 PC DTR Data interrupt ready – –
5 GND Signal Ground RS-232 Ground
6 Modem DSR Communication equipment is ready – –
7 PC RTS Request to Send – –
8 Modem CTS Allow sending – –
9 Modem R I ring indicator – –
3) Disadvantages of serial port
  1. The signal level of the interface is relatively high, which can easily damage the chip of the interface circuit. Since it is incompatible with the TTL level, a level conversion circuit is required to connect to the TTL circuit.
  2. The transmission rate is low. During asynchronous transmission, the baud rate is 20Kbps; therefore, in the CPLD development board, the comprehensive program baud rate can only be 19200 for this reason.
  3. The interface uses a signal line and a signal return line to form a common ground transmission form. This type of common ground transmission is prone to common mode interference, so its immunity to noise interference is weak.
  4. The transmission distance is limited. The standard maximum transmission distance is 50 feet, but in fact it can only be used at about 15 meters.

2. Level standards

According to the different level standards used in communication, serial communication can be divided into TTL standard and RS-232 standard, see the table below:

Communication Standard Level Standard (Sender)
TTL Logic 1: 2.4~5v Logic 0: 0~0.5v
RS-232 Logic 1: -15~ -3v logic 0: + 3~ + 15v

We know that TTL level standards are often used in common electronic circuits. Under ideal conditions, 5V is used to represent binary logic 1, and 0V is used to represent logic 0. In order to increase the long-distance transmission and anti-interference capabilities of serial communication, RS232 uses -15V. Logic 1, +15V means logic 0.
The comparison when using RS232 and TTL level calibration to represent the same signal is shown in the figure below:

3. Working principle of the “USB/TTL to 232” module (CH340 chip module as an example)

USB to serial port converts the computer USB interface to a physical serial port. You can add a serial port to a computer or other USB host that does not have a serial port. Using a USB-to-serial port device is equivalent to turning a traditional serial port device into a plug-and-play USB device.

CH340 introduction:

CH340 is a USB bus adapter chip that realizes USB to serial port or USB to print port. In serial port mode, CH340 provides commonly used MODEM contact signals, which are used to expand asynchronous serial ports for computers, or directly upgrade ordinary serial port devices to USB bus. Because there is no serial port on our current computers, we generally use USB to serial port chips with only one purpose, mapping the computer’s USB port to a serial port. ### 1) Mutual transmission between TTL and 232 serial ports. If the microcontroller serial port (TTL level) wants to communicate with the computer serial port (232 level), it is necessary to make the level logic of the two the same before they can communicate. At this time, you need to use a chip that converts the TTL–232 level. Commonly used ones include MAX232, MAX3232, SP232, SP3232, etc. The MAX232 chip is a dedicated bidirectional conversion chip between TTL level and RS232 level. Different pins realize the function of TTL to RS-232 or RS-232 to TTL. The connection method is: microcontroller serial port-232 chip-serial port. ### 2) USB to TTL/RS-232 – USB to TTL

There are many USB to TTL chips, such as: CH340, PL2303, CP2102, FT232, etc. The development board has an onboard USB to TTL chip: CH340, so you only need to connect the computer and the development board with a USB cable to download the program and perform serial communication. Connection method: MCU TTL serial port-USB to TTL chip-USB port

CH340 actual picture:

The connection diagram is as follows:

  • USB to 232

USB is converted into a TTL serial port through CH340, and TTL is converted into RS-232 level through MAX232, and the 9-pin serial port is led out.

2. USART serial communication of STM32 (HAL library)

USART

USART – Universal Synchronous Asynchronous Receiver Transmitter is a serial communication device that can flexibly perform full-duplex data exchange with external devices. Different from USART, there is also a UART. It is based on USART but cuts out the synchronous communication function and only has asynchronous communication. The simple distinction between synchronous and asynchronous is to see whether an external clock output is required during communication. The serial communication we usually use is basically UART.

Topic requirements

Set the USART1 baud rate to 115200, 1 stop bit, no parity bit, and complete the following tasks:

  1. The STM32 system continuously sends “hello windows!” to the host computer (win10). Win11 uses the “Serial Port Assistant” tool to receive.
  2. After completing the above tasks, continue to expand the function: when the host computer sends a character ‘#’ to STM32, STM32 pauses sending “hello windows!”; after sending a character ‘*’, STM32 continues to send.

1 Send “Hello Windows”

1) CubeMX configuration

For new projects, you can view the LED running lights based on the HAL library.
RCC settings

USART1 settings

mood mode

 Asynchronous: Asynchronous communication
Synchronous: Synchronous communication
Single Wire (Half-Duplex): Single Wire/Half-Duplex
Multiprocessor Communication: Multiprocessor
Support local area interconnection network LIN
SmartCard protocol
lrDA (Infrared Data Association) SIR ENDEC specification.

Crystal oscillator configuration (72M)

Project management and document generation

2) Open the generated keil file

Add the following code in the main function while loop of the main.c file:

char data[]="hello windows!\\
";
HAL_UART_Transmit( & amp;huart1, (uint8_t *)data, 15, 0xffff);
HAL_Delay(1000);

Serial port assistant:

2 Control data sending and receiving through serial port assistant

Enable serial port interrupt

code part
Written before main function

extern UART_HandleTypeDef huart1;//Declare the structure variables of the serial port
uint8_t aRxBuffer; //Accept cache
uint8_t flag=1;//sign symbol

Written in the mani function, it is opened in advance to accept the interrupt, otherwise it cannot be accepted.

HAL_UART_Receive_IT( & amp;huart1, & amp;aRxBuffer,1);

write in while loop

 if(flag==1)
{<!-- -->
HAL_UART_Transmit( & amp;huart1, (uint8_t *)data, 15, 0xffff);
HAL_Delay(1000);
}

Serial port reception interrupt callback function

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{<!-- -->
  /* Prevent unused argument(s) compilation warning */
if(huart == & amp;huart1) // Determine the serial port number
{<!-- -->
\t\t
if(aRxBuffer == '#')//If # is received, the flag will be set to 0, and the main function will not output characters.
{<!-- -->
flag=0;
HAL_UART_Receive_IT( & amp;huart1, & amp;aRxBuffer,1);
}
\t\t
if(aRxBuffer == '*')//In the same way, * is received, set the flag to 1, and the main function can send again
{<!-- -->
flag=1;
HAL_UART_Receive_IT( & amp;huart1, & amp;aRxBuffer,1);
}
\t\t\t
}
}

Simulation waveform


It can be seen that the low level duration is about 9.641975us and the high level duration is 87.96296ns