STM32F4_USART configuration and detailed description

Publisher:创新驿站Latest update time:2017-09-20 Source: eefocusKeywords:STM32F4  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:

 

1.USART clock


This function is located under the bsp.c file;

 

Important Note:

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]

 

2.USART pin configuration


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.


Keywords:STM32F4  USART Reference address:STM32F4_USART configuration and detailed description

Previous article:STM32F030 learning serial port receiving and sending program
Next article:STM32F4 study notes 13——ADC part1

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号