I. Overview
USART serial communication can be said to be a standard feature of MCU. It plays a very important role in both actual project applications and in the development process.
In project applications, we often use UART serial ports for communication. Depending on the distance and stability of communication, we also choose to add RS232, RS485, etc. to convert UART data.
During the development process, we often use it to print debugging information. The development boards we purchase basically have a UART communication interface (DB9 connector). The main function of this interface is to be used for debugging during development.
This article is mainly for STM32 beginners, using the STM32F4 standard peripheral library and Keil integrated IDE to configure and describe the STM32F4 USART. 1. Describe in detail the use of custom print data and custom interrupt to receive data; 2. Redefine print data printf;
Please read on for more details about this article (on WeChat, please click "Read original text" to view more linked content).
Ⅱ. Example Project Download
The examples provided by the author for beginners have removed many unnecessary functions and streamlined the official code, so that beginners can understand it at a glance and provide simple and clear projects for everyone to learn.
The example projects I provided were all uploaded to the 360 cloud disk after multiple tests on the board and no problems were found. You are welcome to download them for testing and reference learning.
The software project provided for download is for STM32F417, but it is also applicable to other F4 models (just change the model in the project).
360 Cloud Disk download address:
https://yunpan.cn/cBQiZXDsvacwV Access password a98e
STM32F4 information:
https://yunpan.cn/cR2pxqF5x2d9c Access password 53e7
III. USART initialization description
The author takes the F4 standard peripheral library (beginners are also advised to use the official standard peripheral library ) as an example to describeInitialization of USART.
The initialization of USART is roughly divided into three parts:
This function is located under the bsp.c file;
A. The configuration of USART clock should be before other USART configurations (USART initialization, interrupt, etc.);
B. Match the corresponding clock.
For example: RCC_APB2 peripherals should not be configured in the RCC_APB1 clock
[For example: RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); This can be compiled, but it is wrong code]
This function is located under the usart.c file;
Notice:
A.GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
In pin configuration, the configuration of pin function reuse is more important. Many friends who have switched from F1 to F4 often ignore this point (there is no such configuration option in F1).
B.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
This is configured as multiplexing mode.
3.USART parameter configuration
This function is located under the usart.c file;
A.USART baud rate:
For STM32, the baud rate can be customized by the user, that is, the value can be set by the user (but the main range and error value are required).
The key point is that it is not affected by changes in system usage (this is the benefit of using a standard peripheral library), that is, it adapts to the system clock.
B.USART mode:
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
The examples provided are for both sending and receiving modes, but I would like to remind you that if you copy code from one place to your own project, you should be careful. If you use the receiving function and the copied code is not configured for receiving, there will be a problem. [Some people have encountered this kind of problem in actual development]
C. Interrupt Configuration
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
The interrupt function is also a commonly used function in applications, especially the receive interrupt. If the USART interrupt is used, this function needs to be configured. The send interrupt is not common, so if there is no send interrupt requirement, there is no need to configure the send function.
IV. USART redefinition (print data)
I believe that friends who come to learn the knowledge of this article (all of whom have learned C language knowledge) should know the printf function for printing data. However, many beginners may not understand how to implement the printf printing method. In fact, it only takes two simple steps to implement the printf function . This section is about describing how to use USART to redefine the print data.
1. Use micro library
Project -> Options for Target -> In the Target window, check "Use MircoLIB" to use the micro library.
Since the printf function is a standard function, we will directly use the standard "micro library" here instead of designing the function ourselves.
2. Redefine the function
#include
First, include the header file "stdio.h". The example project provided is located under the usart.h file.
This function is located under the usart.c file;
We use the printf function and need to redefine the fputc function.
After the above two steps, we can implement the printf function to print out UART data.
Ⅴ. USART customization (print data)
This function is located under the usart.c file;
Our custom printing function is relatively simple and calculates the number of strings.
VI. USART interrupt reception
Data can be received by query or interrupt. The query efficiency is relatively low. Our example provides an interrupt reception method, which achieves the effect of sending out data after receiving it.
Interrupt reception requires configuration of several places:
1. Enable interrupt
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
This function is located in the USART initialization, and its main function is to enable the USART interrupt.
2. Configure NVIC
This function is located under the bsp.c file;
Configure NVIC channels and priorities.
3.USART receive interrupt
This function is located in the stm32f4xx_it.c file;
It mainly receives and processes interrupts, and the effect is: when there is an interrupt, the received data is sent.
Previous article:STM32F030 learning serial port receiving and sending program
Next article:STM32F4 study notes 13——ADC part1
- 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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Open Source Tutorial | Design of Smart Ecological Fish Tank
- Please tell me what type of inductor this is
- Feedback coefficient expression calculation
- I am a newcomer to the switching power supply industry and need to learn how to calculate the counter-transformer. Do you have any good experience or t...
- Elimination of low frequency signals
- Where can I find the German patent number?
- EK140 Burning Guide
- Two small questions about the lithium battery charging chip bq2407x
- Report Summary: Silicon Labs Development Kit Review
- ulab 1.0.0 released