Serial communication under STM32 - HAL library


1. The way of serial port communication

1. Serial and parallel communication

  • Serial communication: A data signal line is used between devices to transmit data one by one in the form of data bits, and only one bit of data can be transmitted at the same time.
    insert image description here
  • Parallel communication: use 8, 16, 32 or more data lines for communication, and multiple data bits can be transmitted at the same time.
    insert image description here

Comparing the communication methods of the two, it is found that the parallel communication method is much faster than the serial communication method, but parallel peers need multiple data lines for transmission, which requires higher costs than serial communication, and requires synchronization of transmission clocks If it is relatively high, there will be many signal interference problems.

2. Synchronous communication and asynchronous communication

  • Asynchronous communication: No clock signal is needed for data synchronization. They directly intersperse some synchronization signal bits in the data signal, or pack the main data, and transmit data in the format of data frame. In some communications, both parties need to agree on data Transmission rate (baud rate) for better synchronization.
  • Synchronous communication: On the contrary, both sides of the synchronous communication transceiver device will use a signal line to constrain the clock, and synchronize and transmit data under the drive of the clock signal.

3. Simplex, full-duplex and half-duplex communication

  • Simplex communication: information can only be transmitted in one direction. One is fixed as the sending device and the other is fixed as the receiving device. The sending end can only send information but not receive information, and the receiving end can only receive information but not send information. The communication can be completed with only one signal line.
  • Full-duplex communication: At the same time, the sending and receiving devices can simultaneously send and receive data, but this method requires both communicating parties to have sending and receiving devices. Two data wires are required to complete the communication.
  • Half-duplex communication: Half-duplex communication can realize two-way communication, but compared with full-duplex, it cannot be carried out in two directions at the same time, and information can only be transmitted in one direction at the same time, but it The direction of transmission can be switched (only one direction of information can be transmitted at the same time).

2. Communication protocol of serial port

1. Level standard

communication standard level standard
5v TTL Logic 0: 0 ~ 0.5v Logic 1: 2.4 ~ 5v
RS-232 Logic 0: +3v ~ +15v Logic 1: -15v ~ -3v

2. Protocol layer

The data packet of serial port communication is transmitted from the sending device to the RXD interface of the receiving device through its own TXD interface. In the protocol layer of serial port communication, the content of the data packet is specified, which is composed of start bit, main data, check bit and stop bit. The data packet format of the communication parties must be agreed to send and receive data normally.

3. The basic composition of the serial data packet

  • Baud rate: The agreed communication rate between two communicating devices is the length of each symbol.
  • Start bit and stop bit: The start signal of the communication data packet is generally logic level "0", and the stop signal can generally be represented by data bits such as 0.5, 1, 1.5, as long as the communication parties agree.
  • Valid data: After the start bit is the main body of the data transmitted by yo, generally the agreed length is 5, 6, 7, 8 bits.
  • Data check digit: Immediately after the effective data digit is an optional data check digit. In the process of communication transmission, it is easy to be affected by the outside world. You can add a parity bit during transmission to solve this problem, including odd parity, even parity, 0 parity, and 1 parity.

3. STM32 serial port and USB to TTL

这里我们学习的是stm32下完成串口通信,我们就介绍一下stm32下的串口。

1. Introduction of STM32 serial port

USART-Universal Synchronous Asynchronous Receiver Receiver is a serial communication device that can flexibly perform full-duplex data exchange with external devices. Different from USART, there is also a UART, which cuts out the synchronous communication function (clock synchronization) on the basis of USART, and only has asynchronous communication. The simple distinction between synchronous and asynchronous is to see whether it is necessary to provide external clock output during communication. The serial communication we usually use is basically UART.

USART functional block diagram:
insert image description here

2. Introduction of USB to TTL

Our commonly used USB-to-serial module is CH340the module, and the USB-to-serial port is mainly used for communication between the device (stm32) and the PC.

  • Block diagram of usb to ttl:
    insert image description here

4. Use the HAL library to realize serial communication under STM32

1. File -> New Project
insert image description here
2. Clock related configuration
insert image description here
3. Select the serial port USART1
insert image description here
4. Set the clock tree
insert image description here

5. Set the project name and project storage directory

insert image description here
6. Export generated code

insert image description here
7. Open the project file with keil, and add code to the main program
insert image description here

HAL_UART_Transmit(&huart1,(uint8_t *)("hello windows!\n"),15,0xffff);

8. Debug simulation settings

insert image description here
9. Perform software simulation

insert image description here
10. The observation result serial port printing is successful!
insert image description here
11. Connect the MCU to the computer and burn the program

insert image description here
Turn on the host computer to watch the serial output is normal!

Summarize

The serial port experiment done this time is a query-type serial port transmission, that is, regardless of whether it is received or not received, the single-chip microcomputer just sends it, which is a waste of resources. However, we can understand the transmission and communication of the serial port through preliminary learning. Later, we will talk about using interrupts to implement serial ports. , realize the serial port transmission in the interrupt.

Guess you like

Origin blog.csdn.net/wer4567/article/details/127374342