1. Communication interface
- The purpose of communication: to transfer data from one device to another, to expand the hardware system
- Communication protocol: formulate the rules of communication, and the communication parties send and receive data according to the rules of the agreement
- The two sides of single-ended signal communication must share the ground, because they are all the voltage difference to GND
- Synchronous signals are controlled by a clock line, and asynchronous signals need to specify the sampling frequency, etc.
- The differential level has good anti-interference and is suitable for long-distance transmission. Differential signal refers to the voltage difference between two communication lines to indicate high and low levels
- Multiple devices need to be addressed, and multiple devices mean that a master can communicate with multiple slaves.
Related terms are explained below:
2. Serial communication
- The serial port is a widely used communication interface. The serial port is low in cost, easy to use, and simple in communication lines. It can realize mutual communication between two devices.
- The serial port of the single-chip microcomputer can make the single-chip microcomputer communicate with the single-chip microcomputer, the single-chip microcomputer and the computer (via the USB to serial port module), and the single-chip microcomputer and various modules, which greatly expands the application range of the single-chip microcomputer and enhances the hardware strength of the single-chip microcomputer system.
The CH340 chip in the above figure implements the USB protocol to serial port protocol. Through this chip, the serial port data of STM32 can be transmitted to the PC through this module.
3. Hardware circuit
- Simple two-way serial communication has two communication lines (transmitter TX and receiver RX)
- Complex serial communication also has clock pins, hardware flow control pins
- TX and RX are to be cross-connected
- When only one-way data transmission is required, only one communication line can be connected (simplex communication)
- When the level standards are inconsistent, it is necessary to add a level conversion chip
4. Level standard
The level standard is the expression of data 1 and data 0. It is the correspondence between the artificially specified voltage and data in the transmission cable. There are three commonly used level standards for serial ports as follows:
TTL level: +3.3V or +5V means 1, 0V means 0
RS232 level: -3 ~ -15V means 1, +3 ~ +15V means 0
RS485 level: two-wire voltage difference +2 ~ +6V means 1, -2 ~ -6V means 0 (differential signal)
5. Serial port parameters and timing
The data frame of the data sent by the serial port
Baud rate: the rate of serial communication (baud rate 1000, means that 1000 bits of data need to be sent in 1 second)
Number of symbols transmitted per second, unit: symbol (bit)/second, bps
Start bit: mark the beginning of a data frame, fixed at low level
Data bit: the payload of the data frame, 1 is high level, 0 is low level, low bit first (the USART peripheral of stm32 will automatically flip the level)
Check bit: used for data verification, calculated according to the number of 1 in the data bit (none, odd, even check) [CRC check is better]
Stop bit: used for data frame interval, fixed at high level
Serial port timing simulation diagram
6. Introduction to STM32's USART peripherals
USART (Universal Synchronous/Asynchronous Receiver/Transmitter) Universal Synchronous/Asynchronous Receiver/Transmitter
USART is a hardware peripheral integrated in STM32. It can automatically generate data frame timing according to a byte of data in the data register, send it from the TX pin, and automatically receive the data frame timing of the RX pin, splicing it into a byte of data. , stored in the data register
Built-in baud rate generator, up to 4.5Mbits/s [Actually, it is a frequency divider, for example, the APB2 bus has a frequency of 72MHZ, after frequency division, the baud rate clock is obtained, and signals are sent and received at this frequency]
Configurable data bit length (8/9), stop bit length (0.5/1/1.5/2)
Optional parity (no parity/odd parity/even parity)
Support synchronous mode (more clock CLK output), hardware flow control (the receiver sets high level on the control line to indicate that it is not ready to receive data), DMA, smart card, IrDA, LIN
STM32F103C8T6 USART resources: USART1 (APB2 bus), USART2, USART3
Seven, USART block diagram
Data flow:
Transmit Data Register (TDR) ----- "Transmit Shift Register - "TX
RX——"Receive Shift Register—"Receive Data Register (RDR)
working principle:
When the transmit data register (TDR) sends a byte of data to the shift register, the TXE flag will be set to 1 at this time, indicating that the TDR can write new data, and then the shift register is under the action of the transmit controller One bit is sent to the TX pin. When the shift register is empty, the send data register will write a byte of data into the shift register.
RX sends data to the receiving shift register, and under the action of the receiving controller, the data is sent from the shift register to the receiving data register (RDR), at this time RXNE is set to 1
Using two registers for caching can improve work efficiency
The sending data register and the receiving data register occupy the same address, that is, there is only one register on the software, and two registers on the hardware
Introducing enhanced features
Hardware data flow control: avoid data loss due to data receiving and sending too fast
It needs to be connected to an external serial port that supports flow control (for example, the internal nRTS communicates with the external nCTS cross-connection, and if it can receive, it will send a low level)
nRTS (request, n active low)
nCTS(clear)
SCLK
Working with the sending shift register, every time a bit is sent, the clock goes for one cycle, only supports output, and the function is compatible with other protocols, such as SPI
wake up unit
Realize the serial port to mount multiple devices, connect multiple devices on one bus, each device has an address, and address it first when communicating
interrupt control
On the right is the status register SR, where TXE indicates that the transmit register is empty, and RXNE indicates that the receive register is empty
On the left is the control register CR
USART_BRR Baud Rate Generator
Transmitter clock and receiver clock
USART1 is 72MHZ, USART2/3 is 36MHZ
Eight, USART basic structure
- When the data is received, the flag bit will be set to 1, and an interrupt can be applied for at the same time. It is necessary to configure the interrupt ITConfig and NVIC
- The input adopts floating input or pull-up input. When the serial port is idle, it is high level, and the pull-down input cannot be used.
data schema
-
HEX mode/hexadecimal mode/binary mode: display in the form of raw data
-
Text Mode/Character Mode: Display in encoded form of raw data
ASCLL code list
During the data transmission process between the sender and the receiver, the sender can also send the character A, which will be converted into hexadecimal through the ASCLL code table and sent to the receiver
wiring diagram
The serial port sends data to the PC code:
serial.c
#include "stm32f10x.h" // Device header #include <stdio.h> #include <stdarg.h> void Serial_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplexed push-pull output GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //Serial port module initialization USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; //baud rate USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Hardware flow control, controlling the speed of data sending and receiving USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; //parity mode USART_InitStructure.USART_StopBits = USART_StopBits_1; //Specifies the number of bits of stop bits transmitted USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Specifies the number of bits in the data frame USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } void Serial_SendByte(uint8_t Byte) { USART_SendData(USART1, Byte); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } void Serial_SendArray(uint8_t *Array, uint16_t Length) { uint16_t i; for (i = 0; i < Length; i ++) { Serial_SendByte(Array[i]); } } void Serial_SendString(char *String) { uint8_t i; for (i = 0; String[i] != '\0'; i ++) { Serial_SendByte(String[i]); } } uint32_t Serial_Pow(uint32_t X, uint32_t Y) { uint32_t Result = 1; while (Y --) { Result *= X; } return Result; } void Serial_SendNumber(uint32_t Number, uint8_t Length) { uint8_t i; for (i = 0; i < Length; i ++) { Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0'); } } int fputc(int ch, FILE *f) { Serial_SendByte(ch); return ch; } void Serial_Printf(char *format, ...) { char String[100]; va_list arg; va_start(arg, format); vsprintf(String, format, arg); va_end(arg); Serial_SendString(String); }
main.c
#include "Serial.h" int main(void) { OLED_Init(); Serial_Init(); Serial_SendByte(0x64); //uint8_t MyArray[] = {0x42, 0x43, 0x44, 0x45}; //Serial_SendArray(MyArray, 4); //Serial_SendString("\r\nNum1="); //Serial_SendNumber(111, 3); //printf("\r\nNum2=%d", 222); //char String[100]; //sprintf(String, "\r\nNum3=%d", 333); //Serial_SendString(String); //Serial_Printf("\r\nNum4=%d", 444); //Serial_Printf("\r\n"); while (1) { } }