STM32F10x USART serial port mapping function realizes serial port communication 485 initialization

Publisher:心连心意Latest update time:2018-08-12 Source: eefocusKeywords:STM32F10x  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This article is very useful! Newbies should not be self-righteous. Do you know how to remap the STM32 serial port pins?


The STM32F10x series of microcontrollers all include the USART module, which is a universal synchronous asynchronous receiver and transmitter. The universal synchronous asynchronous receiver and transmitter (USART) provides a flexible method for full-duplex data exchange with external devices using the industrial standard NRZ asynchronous serial data format. It supports synchronous one-way communication and half-duplex single-line communication, as well as LIN (local interconnect network), smart card protocol and IrDA (infrared data organization) SIR ENDEC specification, as well as modem (CTS/RTS) operation. It also allows multi-processor communication.


From the previous introduction, we know that the USART module is very powerful. Here I will just briefly talk about how to use the USART module to implement standard EIA-232 serial communication.


Anyone who has used a single-chip microcomputer must have come into contact with a serial port. Setting a serial port is nothing more than setting the baud rate, data bit, stop bit, and parity bit. There are three basic ways to send and receive: polling, interrupt, and DMA. The USART module of STM32F10x is no different. So I will focus on the various mistakes I made when debugging the code, and I will not explain in detail those codes that are easy to get.


First, let me talk about my hardware environment. I still use the Shenzhou 4 development board, and use serial port 2, which corresponds to USART2. By default, USART2 is connected to IO port A, but I need to redirect the USART pins to IO port D. The specific relationship between the pins is shown in the table below. This table is copied from the STM32 reference manual.





The code to initialize USART is very simple. USART2 is connected to APB1 bus. First, turn on the clock of USART2, and then set parameters such as baud rate.


USART_InitTypeDef USART_InitStructure;  

  

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  

USART_InitStructure.USART_BaudRate = 9600;  

USART_InitStructure.USART_WordLength = USART_WordLength_8b;  

USART_InitStructure.USART_StopBits = USART_StopBits_1;  

USART_InitStructure.USART_Parity = USART_Parity_No;  

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  

USART_Init(USART2, &USART_InitStructure );   


This setting is not usable yet. This is because we redirected USART2. The redirection operation requires writing the multiplexing remapping and debugging I/O configuration register (AFIO_MAPR). GPIO_PinRemapConfig() can complete this task.


[cpp] view plain copy

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  

This is not enough. The STM32 reference manual says:


Before reading or writing registers AFIO_EVCR, AFIO_MAPR and AFIO_EXTICRX, the AFIO clock should be turned on first. Refer to Section 6.3.7 APB2 Peripheral Clock Enable Register (RCC_APB2ENR).


So you need to turn on the AFIO clock first. Therefore, redirection of USART2 requires two steps:


RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);  

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  

I thought it would work, but it still didn't output anything. I had no choice but to continue researching. When I was reading the chapter on GPIO, I saw the following picture, which made me suddenly realize it.


The input and output of USART2 are all borrowed from the PD port, but the clock of the PD port has not been given. The corresponding input and output states of the several IO ports used have not been set. When reading the section 8.1.9 Multiplexing Function Configuration, I found the following table.


According to the configuration given above, write the program:

GPIO_InitTypeDef GPIO_InitStructure;  

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);  

/* Configure USART Tx as alternate function push-pull */  

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;  

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;  

GPIO_Init(GPIOD, &GPIO_InitStructure);  

      

/* Configure USART Rx as input floating */  

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;  

GPIO_Init(GPIOD, &GPIO_InitStructure);  


Tested again, everything is normal.


A function that sends a character can be written like this:



void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)  

{  

    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET ) {};  

    USART_SendData (USARTx, Data);  

}  

This function can be optimized manually, and the two function calls in it can be removed, or even implemented in assembly or written as an inline function. However, this is just a sample code and does not take these into consideration.

The function that sends the string is as follows:



void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str)  

{  

    while (0 != *str)  

    {  

        UART_PutChar(USARTx, *str);  

        str++;  

    }  

}  


The above serial port initialization code can be put into a function:


void USART2_init(void)  

{  

    GPIO_InitTypeDef GPIO_InitStructure;  

    USART_InitTypeDef USART_InitStructure;  

  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);  

      

    /* Configure USART Tx as alternate function push-pull */  

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;  

    GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;  

    GPIO_Init(GPIOD, &GPIO_InitStructure);  

      

    /* Configure USART Rx as input floating */  

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;  

    GPIO_Init(GPIOD, &GPIO_InitStructure);  

      

    GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  

  

    USART_InitStructure.USART_BaudRate = 9600;  

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;  

    USART_InitStructure.USART_StopBits = USART_StopBits_1;  

    USART_InitStructure.USART_Parity = USART_Parity_No;  

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  

    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  

    USART_Init(USART2, &USART_InitStructure );   

  

    USART_Cmd(USART2, ENABLE);  

}  


That's all for today. The function for receiving characters is similar to the function for sending characters, but this polling method is very inefficient and is not recommended. Next time I will write an article about how to send and receive serial port data using interrupts, which is much more efficient. If I have time, I will write another article about sending and receiving data using DMA.


Keywords:STM32F10x  USART Reference address:STM32F10x USART serial port mapping function realizes serial port communication 485 initialization

Previous article:STM32 UART/USART initialization clock enable
Next article:STM32 485 serial data transmission and reception

Recommended ReadingLatest update time:2024-11-16 12:59

STM32 USART serial port DMA receive and send mode
Serial port DMA sending: The process of sending data: If there is data to be sent in the foreground program, you need to do the following things 1. Place the data to be sent in the data sending buffer. Note: The first address of this data buffer must be written into the DMA configuration when the DMA is initialized. 2
[Microcontroller]
STM32 USART serial port DMA receive and send mode
STM8L USART serial port usage
There are multiple serial ports on the STM8L, up to 5, namely USART1~USART5, but the number of serial ports varies depending on the model.  Taking STM8L052R8 as an example, it only has USART1~USART3.  Because the STM8 series has many functions and many pins are reused, you must check the STML reference manual befo
[Microcontroller]
A brief discussion on the SysTick system clock timer of the STM32F10X chip
As the title, the text is as follows: 1 Introduction        To implement the configuration of the Cortex-M3 system timer SysTick, you need to have the following knowledge: the default frequency of the Cortex-M3 system timer is 8 times the frequency of HCLK (as shown in the figure below), so you need to configure the
[Microcontroller]
The STM32F10x series MCU configures PB3 and PB4 as normal IO ports
For beginners, why can't they control the output when using PB3 and PB4? The following is an analysis and explanation of this issue. First, after the STM32F10x series MCU is reset, PA13/14/15 & PB3/4 are configured as JTAG ports by default. Sometimes, in order to make full use of the resources of the MCU I/O port, we
[Microcontroller]
Reasons for slow USB speed on STM32F10X
There are several possible reasons why USB speed is slow: 1. Protocol issues      The maximum speed of a full-speed USB device is 12Mb/s, but if it is running a slow device protocol such as HID, the speed will not be that fast, theoretically only 1.5Mb/s. 2. Configuration issues     The USB configuration descriptor co
[Microcontroller]
DMA working mode of USART6 of stm32F407
I debugged the DMA working mode of USART6 yesterday, and I am posting this note today. For the sake of brevity, the DMA of stm32 will not be introduced. If you don't know, you can search for it. Here we focus on how to determine the peripheral address of DMA. This is rarely mentioned on the Internet but it is very imp
[Microcontroller]
DMA working mode of USART6 of stm32F407
Basic knowledge of STM32 USART
USART, also known as Universal Synchronous Asynchronous Receiver/Transmitter, uses a fractional bit rate generator to provide a wide range of bit rate options. The STM32F10x series chips all provide a powerful USART serial port, and the basic serial port functions can be implemented through hardware. USART has the fol
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号