In the field of embedded development, the serial port usually refers to UART, which is Universal Asynchronous Receiver/Transmitter. It is a universal serial data bus used for asynchronous communication. The bus has bidirectional communication and can achieve full-duplex transmission and reception.
UART is often used for data transmission between two microcontrollers, as shown in the figure below.
The communication process is shown in the figure below.
STM32F103 provides 3 serial ports, supports synchronous single-line communication and half-duplex single-line communication, supports LIN, supports modem operation, smart card protocol and IrDA SIR ENDEC specification, has DMA, etc. There are restrictions on the pins that can be configured as serial ports, so when designing hardware circuits, you need to pay attention to which pins can be configured as serial port pins, as shown in the following table.
Serial port pin mapping
Serial port number
|
RxD
|
TXD
|
1
|
PA10
|
PA9
|
2
|
PA3
|
PA2
|
3
|
PB11
|
PB10
|
2. UART serial communication protocol
During serial communication, a frame of data includes a start bit, data bits, stop bits, and an optional parity bit. In addition to knowing these concepts, you also need to understand the concept of baud rate.
- Start bit, when no data is sent, the data line is in the logic "1" state; a logic "0" signal is sent first, indicating the start of character transmission;
- Data bit, 8 data immediately after the start bit. Transmitted from the lowest bit;
- Parity bit, generally not used;
- The stop bit is usually 1 bit (it can also be 1.5 bits or 2 bits) and is at a high level. It serves as a sign of the end;
- Baud rate, baud rate indicates the number of code symbols transmitted per second, and is an indicator of data transmission speed.
As shown in the figure below, this is the frame composition of a frame of serial port data.
3. UART serial port register
There are many registers related to the serial port of STM32F103, and only a few important registers are explained here.
- USART_SR: Status register, as shown below:
The bits that need to be understood are: TXE: transmit register is empty; TC: transmit complete; RXNE: read data register is not empty; ORE: overflow error; FE: frame error; PE: parity error.
- USART_DR: Only the lower 8 bits of this register are valid, and it has read and write operations. Whether the data in this register is data to be sent or received depends on whether the operation performed is "read" or "write". As shown in the figure below:
- USART_BRR: Baud rate register, used to set the baud rate of serial communication, as shown in the following figure:
The corresponding values will be set in the registers, DIV_Mantissa[11:0] is used to set the integer part of the division coefficient, and DIV_Fraction[3,0] is used to set the decimal part of the division coefficient.
The baud rate calculation formula of the serial port is as follows:
Here, fPCLKx (x=1, 2) is the clock for the peripherals (PCLK1 is used for USART2, 3, 4, 5, PCLK2 is used for USART1, and the serial ports that STM32F103CBT6 does not have are not explained here). USARTDIV is an unsigned floating point number. Assume that the serial port baud rate is 115200, serial port 1 is used, and the serial port clock is the PCLK2 clock (that is, the APB2 bus clock) of 72MHz, as shown in the figure below.
According to the formula:
USARTDIV=72000000/(115200*16)=39.0625
So the decimal part DIV_Fraction = 0.0625*16 = 1 = 0x01.
The integer part DIV_Mantissa = 39 = 0x27.
- Basic serial port transmission and reception experiment
The serial port receiving and sending experiment uses the serial port 1 of STM32, namely UART1, to connect the computer through the serial port module, and uses the serial port debugging assistant to communicate. After the STM32 receives the data, it sends the received data back to the computer. The serial port pins are shown in the figure below:
The two serial port pins are connected to the PA9 and PA10 pins respectively, so we configure the PA9 and PA10 pins and the serial port module, and configure the code (by calling the official library).
The idea of code writing is as follows:
Programming ideas
1
|
Pin Configuration
|
1. Define the structure;
2. Enable the clock;
3. Filling structure;
4. Load the structure.
|
2
|
Serial port configuration
|
1. Define the structure;
2. Enable the clock;
3. Filling structure;
4. Loading structure
5. Enable interrupts;
6. Enable the serial port.
|
3
|
Interrupt priority management
|
1. Define the structure;
2. Filling structure;
3. Load the structure.
|
4
|
Logical Processing
|
Waiting to receive and send data
|
The serial port initialization code is as follows:
The serial port interrupt function code is as follows:
Set the baud rate of serial port 1 to 115200. The code is as follows:
Save, compile, and download the code. Unplug the visual module from USART1 on the flight controller, and then connect the USB to serial port module, as shown in the following figure:
Then plug the USB to serial module into the USB port of the computer, open the serial debugging assistant on the computer, find the corresponding COM port (the prerequisite is that the USB to serial port driver is installed), set the baud rate to 115200, open it, enter "www.fengke.club" in the input box of the serial debugging assistant, click Send, and then you can see that the serial debugging assistant receives the reply information from STM32, which is also "www.fengke.club", as shown in the figure below.