3.6.Serial communication

1.learning target

1.Learn the basic use of the serial port of the STM32 motherboard.

2.Understand the basics of serial communication.

USART introduction of STM32F1:

Serial communication (Serial Communication) refers to a communication method that transmits data bit by bit between peripherals and computers through data signal lines, ground lines, etc., and belongs to the serial communication method.

USART is the Universal Synchronous Asynchronous Transceiver, which can flexibly perform full-duplex data exchange with external devices and meet the requirements of external devices for the industry standard NRZ asynchronous serial data format. UART is the Universal Asynchronous Transceiver, which cuts out the synchronous communication function on the basis of USART. Synchronization and asynchrony mainly depend on whether the clock needs to be provided externally. This chapter mainly uses the USART, and the STM32F103C8T6 chip contains 3 USART peripherals. The USART supports synchronous one-way communication and half-duplex single-wire communication, and also supports LIN (Interconnect Domain Network), smart card protocol and IrDA (Infrared Data Association) SIR ENDEC specification, and modem operation (CTS/RTS). Furthermore, it supports multiprocessor communication and DMA function, which enables high-speed data communication using DMA. The USART provides a variety of baud rates through a fractional baud rate generator. The most widely used USART in STM32 is printf to output debugging information. When we need to know some variable data information in the program, we can use the printf output function to print this information to the serial port and display it on the assistant, so that we can debug the program Bring great convenience.

When in use, the corresponding pins of each serial port can be found in the STM32 data sheet.

2.Hardware Wiring Introduction

Since the core board comes with a ch340 serial port circuit, the serial port communication can be realized directly through the usb cable, without the need for an external usb to ttl module.

Partial circuit diagram of CH340

3.program analysis

Serial port initialization code:

In the USART1_Init() function, first enable the USART1 serial port and port clock, and initialize the GPIO as an alternate function. Secondly, configure the serial port structure USART_InitTypeDef, enable the serial port and enable the receiving interrupt. In order to prevent the influence of the serial port sending status flag, we clear the serial port status flag (TC), and finally configure the corresponding NVIC and enable the corresponding interrupt channel. We will The preemption priority of USART1 is set to 3, and the response priority is set to 3.

serial interrupt code:

In order to confirm whether USART1 has a receiving interrupt, the function USART_GetITStatus, which reads the serial port interrupt status flag bit, is called. If a receiving interrupt event is indeed generated, then the statement in the if will be executed, and the data received by the serial port will be saved in the variable r, and then there will be Send out through the serial port, read the serial port status flag through the USART_GetFlagStatus function, if the data transmission is completed, exit the while loop statement, and clear the sending completion status flag USART_FLAG_TC.

main function code:

The function implemented by the main function is very simple. First, it calls the previously written hardware initialization function, including SysTick system clock, interrupt grouping, LED initialization, etc. Then call the USART1 initialization function we wrote earlier, here we set the serial communication baud rate to 9600. Finally, enter the while loop statement, and let the D1 indicator flash at intervals of 200ms. If a receiving interrupt event occurs, it will enter the interrupt execution, and return to the main function to continue running after execution

4.Experimental phenomena

After the program download is complete, the led light will flicker, and after configuring the serial port assistant as shown in the figure below, you can view the returned sending content in the serial port assistant receiving window

Note: The baud rate must be 9600, otherwise the sent content cannot be received.