STM32 USART serial port configuration

Publisher:温暖微风Latest update time:2017-09-17 Source: eefocusKeywords:stm32  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Concept 
In the STM32 reference manual, the serial port is described as a universal synchronous asynchronous receiver and transmitter (USART), which provides a flexible method for full-duplex data exchange with external devices using the industry standard NRZ asynchronous serial data format. (Okay, I don't quite understand it, so I'll just post the official definition for now, and you can figure it out yourself) 
Configuration steps

  1. Turn on the clock (RCC configuration). 
    Since the TX, RX and AFIO of UART are all hung on the APB2 bridge, the firmware library function RCC_APB2PeriphClockCmd() is used for initialization. UARTx needs to be discussed separately. If it is UART1, it is hung on the APB2 bridge, so RCC_APB2PeriphClockCmd() is used for initialization. The rest of UART2~5 are hung on APB1.

  2. GPIO configuration 
    The properties of GPIO are included in the structure GPIO_InitTypeDef, where for the TX pin, the GPIO_Mode field is set to GPIO_Mode_AF_PP (multiplexed push-pull output), and the GPIO_Speed ​​switching rate is set to GPIO_Speed_50MHz; for the RX pin, the GPIO_Mode field is set to GPIO_Mode_IN_FLOATING (floating input), and the switching rate does not need to be set. Finally, enable the IO port through GPIO_Init().

  3. When NVIC configures 
    STM32, even if there is only one interrupt, it still needs to configure the priority level, which is used to enable the trigger channel of a certain interrupt. STM32 interrupts have at most two levels, namely preemption priority (main priority) and sub-priority (slave priority), and the length of the entire priority setting parameter is 4 bits, so it is necessary to first divide the preemption priority bit and sub-priority bit, which is implemented through NVIC_PriorityGroupConfig();

    The interrupt priority NVIC property of a specific device is contained in the structure NVIC_InitTypeDef, where the field NVIC_IRQChannel contains the interrupt vector of the device, which is saved in the startup code; the field NVIC_IRQChannelPreemptionPriority is the primary priority NVIC_IRQChannelSubPriority is the secondary priority, and the value range should be determined according to the bit division; the last NVIC_IRQChannelCmd field is whether it is enabled, which is generally set to ENABLE. Finally, NVIC_Init() is used to enable this interrupt vector.

  4. The USART configuration 
    is determined by the structure USART_InitTypeDef. The fields in UART mode are as follows:

    USART_BaudRate: Baud rate (data bits that can be transmitted per second), the default value is 9600.

    USART_WordLength: word length

    USART_StopBits: Stop bits

    USART_Parity: Check mode (parity check)

    USART_HardwareFlowControl: Hardware flow control

    USART_Mode: Single/duplex, i.e., transmit/receive status.

    Finally, set it up through USART_Init().

  5. Code Summary

void uart_init(u32 bound){

    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //Enable USART1 and GPIOA clocks
    USART_DeInit(USART1); //Reset serial port 1 (set all parameters to default values)

    //USART1_TX (send data) PA.9 pin
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output
    GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA.9

    //USART1_RX (receive data) PA.10 pin
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
    GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA.10

   //NVIC interrupt vector configuration 
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; //Set the preemption priority to 3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //The sub-priority is set to 3. The priority is determined according to the importance of different interrupts.
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable
    NVIC_Init(&NVIC_InitStructure); //Initialize NVIC registers according to the parameters set above

   //USART initialization settings
    USART_InitStructure.USART_BaudRate = bound; //Baud rate is 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Word length is 8 bits
    USART_InitStructure.USART_StopBits = USART_StopBits_1; //1 stop bit
    USART_InitStructure.USART_Parity = USART_Parity_No; //No parity bit
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Transmit and receive mode

    USART_Init(USART1, &USART_InitStructure); //Serial port initialization
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Interrupt enable
    USART_Cmd(USART1, ENABLE); //Serial port enable}123456789101112131415161718192021222324252627282930313233343536373839

end


Keywords:stm32  USART Reference address:STM32 USART serial port configuration

Previous article:STM32 GPIO register introduction and settings
Next article:STM32 RCC study notes

Recommended ReadingLatest update time:2024-11-16 16:03

Study notes on SD card FATFS file system based on STM32
Before porting the file system, first master some basic knowledge (must be mastered): 1. Analysis of FATFS file system format To work on the file system, you must first understand the structure and partitions in the SD card, as shown below 1. Boot sector The boot sector contains many important parameters of the dis
[Microcontroller]
STM32 HardFault_Handler problem debugging method
I believe that many people have encountered the HardFault_Handler error when debugging STM32. People who have just started to use it must be most afraid of this error, because the cause of this problem is difficult to find. I have seen many people on the Internet give better debugging methods. When I debugged again, I
[Microcontroller]
STM32 Series Part 5--Systick Tick Timer
Systick timer is a simple timer. Both CM3 and CM4 core chips have Systick timer. Systick timer is often used for delay or heartbeat clock of real-time system. This can save MCU resources and avoid wasting a timer. For example, in UCOS, time-division multiplexing requires a minimum timestamp. Generally, in STM32+UCOS s
[Microcontroller]
STM32 system learning-SysTick (system timer)
The SysTick system timer is a peripheral in the CM3 core, embedded in the NVIC (Nested Vector Interrupt Controller, which controls the interrupt-related functions of the entire chip. It is closely coupled with the core and is a peripheral in the core). The system timer is a 24-bit down-counting counter. The time for e
[Microcontroller]
STM32 USB related registers
Here I will paste the specific settings of the STM32 USB-related registers, which is very helpful for learning STM32 USB. For the code involving the register part, you can refer to the description of these registers and you can almost understand it. Since the table cannot be posted on the webpage, I took a screenshot
[Microcontroller]
STM32 USB related registers
STM32 study notes: reading and writing internal Flash
First we need to understand a memory map:    The flash address of stm32 starts at 0x0800 0000 and ends at 0x0800 0000 plus the actual flash size of the chip. Different chips have different flash sizes. The RAM start address is 0x2000 0000, and the end address is 0x2000 0000 plus the chip's RAM size. Different chips h
[Microcontroller]
STM32 study notes: reading and writing internal Flash
STM32 general timer configuration
1. Principle of STM32 general timer                             The STM32 series CPU has up to 8 timers, of which TIM1 and TIM8 are advanced timers that can generate three pairs of PWM complementary outputs, commonly used to drive three-phase motors, and their clocks are generated by the output of APB2. The other 6 ar
[Microcontroller]
STM32 NVIC
Understanding of STM32's NVIC (Green and purple are the achievements of other excellent netizens, sincere thanks. Now copied and summarized for your convenience. If it constitutes infringement, please contact us in time) 例程:   /* Configure one bit for preemption priority */   NVIC_PriorityGroupConfig(NVIC_
[Microcontroller]
STM32 NVIC
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号