For multiplexed IO, we must first enable the GPIO clock, then enable the multiplexed function clock, and at the same time set the GPIO mode to the mode corresponding to the multiplexed function, and initialize the serial port parameters, including baud rate, stop bit and other parameters. After the settings are completed, the serial port is enabled. At the same time, if the serial port interrupt is enabled, of course, the NVIC must be initialized to set the interrupt priority level, and finally the interrupt service function must be written.
The general steps of serial port settings can be summarized as follows:
1) Serial port clock enable, GPIO clock enable
2) Serial port reset
3) GPIO port mode setting
4) Serial port parameter initialization
5) Enable interrupts and initialize NVIC (this step is only required if interrupts are enabled)
6) Enable the serial port
7) Write an interrupt handling function
Several firmware library functions directly related to the basic configuration of the serial port. These functions and definitions are mainly distributed in the stm32f10x_usart.h and stm32f10x_usart.c files.
1. Enable the serial port clock.
The serial port is a peripheral mounted under APB2, so the enabling function is:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1);
2. Reset the serial port.
When an abnormality occurs in a peripheral, the reset setting can be used to reset the peripheral, and then reconfigure the peripheral to make it work again. Generally, when the system starts to configure the peripheral, the reset operation of the peripheral will be performed first. The reset is completed in the function USART_DeInit():
void USART_DeInit(USART_TypeDef* USARTx); //Serial port reset
For example, to reset serial port 1, the method is:
USART_DeInit(USART1); //Reset serial port 1
3. Initialize serial port parameters.
The serial port initialization is implemented by the USART_Init() function.
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);
The first entry parameter of this function is the serial port number to be initialized, here USART1 is selected. The second entry parameter is a structure pointer of type USART_InitTypeDef, the member variables of which are used to set some parameters of the serial port. The general implementation format is:
1 USART_InitStructure.USART_BaudRate = bound; //Generally set to 9600;
2 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Word length is 8-bit data format
3 USART_InitStructure.USART_StopBits = USART_StopBits_1; //One stop bit
4 USART_InitStructure.USART_Parity = USART_Parity_No; //No parity bit
5 USART_InitStructure.USART_HardwareFlowControl
6 = USART_HardwareFlowControl_None; //No hardware data flow control
7 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Transmit and receive mode
8 USART_Init(USART1, &USART_InitStructure); //Initialize the serial port
From the above initialization format, we can see that the parameters that need to be set for initialization are: baud rate, word length, stop bit, parity bit, hardware data flow control, mode (receive, send). We can set these parameters as needed.
4. Data sending and receiving.
The transmission and reception of STM32 is realized through the data register USART_DR, which is a double register containing TDR and RDR. When data is written to this register, the serial port will automatically send it, and when data is received, it is also stored in this register.
The STM32 library function operates the USART_DR register to send data:
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
This function is used to write data to the serial port register USART_DR.
The function of the STM32 library function to operate the USART_DR register to read the data received by the serial port is:
uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
This function can be used to read the data received by the serial port.
5. Serial port status.
The status of the serial port can be read through the status register USART_SR. The description of each bit of USART_SR is shown in Figure 1:
Figure 1 Description of each bit of USART_SR register
Pay attention to two bits, the 5th and 6th bits RXNE and TC.
RXNE (read data register not empty), when this bit is set to 1, it indicates that data has been received and can be read. At this time, what we need to do is to read USART_DR as soon as possible. By reading USART_DR, this bit can be cleared to 0, or by writing 0 to this bit to clear it directly.
TC (transmission complete), when this bit is set, it means that the data in USART_DR has been sent. If the interrupt of this bit is set, an interrupt will be generated. There are also two ways to clear this bit: 1) read USART_SR and write USART_DR. 2) write 0 directly to this bit.
In our firmware library function, the function for reading the serial port status is:
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
The second entry parameter of this function is very important. It indicates which state of the serial port you want to check, such as RXNE (read data register is not empty) and TC (transmission complete) explained above. For example, to determine whether the read register is not empty (RXNE), the method of operating the library function is:
USART_GetFlagStatus(USART1, USART_FLAG_RXNE);
To determine whether the transmission is successful (T completed C), the method of operating the library function is:
USART_GetFlagStatus(USART1, USART_FLAG_TC);
The above identification numbers are defined in MDK through macro definitions:
1 #define USART_IT_PE ((uint16_t)0x0028)
2 #define USART_IT_TXE ((uint16_t)0x0727)
3 #define USART_IT_TC ((uint16_t)0x0626)
4 #define USART_IT_RXNE ((uint16_t)0x0525)
5 #define USART_IT_IDLE ((uint16_t)0x0424)
6 #define USART_IT_LBD ((uint16_t)0x0846)
7 #define USART_IT_CTS ((uint16_t)0x096A)
8 #define USART_IT_ERR ((uint16_t)0x0060)
9 #define USART_IT_ORE ((uint16_t)0x0360)
10 #define USART_IT_NE ((uint16_t)0x0260)
11 #define USART_IT_FE ((uint16_t)0x0160)
6. Enable the serial port.
The serial port is enabled through the function USART_Cmd(), which is easy to understand. The usage is:
USART_Cmd(USART1, ENABLE); //Enable the serial port
7. Enable serial port response interrupt.
Sometimes when you need to turn on the serial port interrupt, you also need to enable the serial port interrupt. The function to enable the serial port interrupt is:
void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT,
FunctionalState NewState)
The second entry parameter of this function is to indicate the type of serial port enabled, that is, which interrupt to enable, because there are many types of serial port interrupts. For example, when receiving data (RXNE read data register is not empty), an interrupt is to be generated, then the method to enable the interrupt is:
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Enable interrupt and receive data interrupt
To generate an interrupt at the end of sending data (TC, sending completed), the method is:
USART_ITConfig(USART1,USART_IT_TC,ENABLE);
8. Get the corresponding interrupt status. When we enable an interrupt, when the interrupt occurs, a flag bit in the status register will be set. Often in the interrupt handling function, we need to determine what kind of interrupt it is. The function used is:
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
For example, if the serial port transmission completion interrupt is enabled, then when an interrupt occurs, this function can be called in the interrupt handling function to determine whether it is a serial port transmission completion interrupt. The method is:
USART_GetITStatus(USART1, USART_IT_TC);
The return value is SET, indicating that a serial port send completion interrupt has occurred.
4. uart_init() function
Introduce the uart_init function, the function code is as follows:
1 //Initialize GPIO and serial port 1
2 //bound: baud rate
3 void uart_init(u32 bound)
4 {
5 GPIO_InitTypeDef GPIO_InitStructure;
6 USART_InitTypeDef USART_InitStructure;
7 NVIC_InitTypeDef NVIC_InitStructure;
8 //①Serial port clock enable, GPIO clock enable, multiplexed clock enable
9 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|
10 RCC_APB2Periph_GPIOA, ENABLE); //Enable USART1,GPIOA clock
11 //②Serial port reset
12 USART_DeInit(USART1); //Reset serial port 1
13 //③GPIO port mode setting
14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //ISART1_TX PA.9
15 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
16 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output
17 GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize GPIOA.9
18 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1_RX PA.10
19 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
20 GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize GPIOA.10
21 //④Serial port parameter initialization
22 USART_InitStructure.USART_BaudRate = bound; //Baud rate setting
23 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Word length is 8 bits
24 USART_InitStructure.USART_StopBits = USART_StopBits_1; //One stop bit
25 USART_InitStructure.USART_Parity = USART_Parity_No; //No parity bit
26 USART_InitStructure.USART_HardwareFlowControl
27 = USART_HardwareFlowControl_None; //No hardware data flow control
28 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Transmit and receive mode
29 USART_Init(USART1, &USART_InitStructure); //Initialize the serial port
30 #if EN_USART1_RX //If reception is enabled
31 //⑤Initialize NVIC
32 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
33 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; //Preemption priority 3
34 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //Subpriority 3
35 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable
36 NVIC_Init(&NVIC_InitStructure); //Interrupt priority initialization
37 //⑤Enable interrupt
38 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Enable interrupt
39 #endif
40 //⑥ Enable the serial port
41 USART_Cmd(USART1, ENABLE); //Enable the serial port
42 }
From the code, we can see that the process of initializing the serial port is the same as what we introduced earlier. We use numbers ①~⑥ to indicate the order:
① Serial port clock enable, GPIO clock enable
② Serial port reset
③ GPIO port mode setting
④ Serial port parameter initialization
⑤ Initialize NVIC and enable interrupt
⑥ Enable the serial port
Previous article:stm32 FSMC-External SRAM IS62WV51216
Next article:STM32 USART1 one-key download circuit
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- A naked scheme, is this true?
- The legendary RTOS ThreadX is now fully open source! Live pre-registration | Microsoft's new IoT solution is now officially released
- MSP430G2553 timer output PWM programming example
- Method for transferring files between mobile phone and oscilloscope directly through network
- How to Design a Successive Approximation ADC Driver Circuit
- Understand the basics of Class A, Class B, and Class AB power amplifier circuits,,,,
- [Evaluation of domestic FPGA Gaoyun GW1N-4 series development board]——4. Use of embedded logic analyzer
- Causes of PCB copper shedding
- In 2020, 5G has covered 1,336 cities around the world, with a growth rate of more than three times
- Understanding the Internals of ADCs