The use of UART in STM32 microcontroller

Publisher:大伊山人Latest update time:2013-02-22 Source: 21IC Keywords:STM32  UART  MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Before using UART, the corresponding peripheral clock must be started, which mainly uses the RCC_APBnPeriphClockCmd function of the firmware library.

Enable UART1: Use RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)

Enable UART2: use RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE)

2. To use interrupts for UART operations, you need to configure NVIC and set the interrupt priority. For example:

/* Configure the NVIC Preemption Priority Bits */

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

/* Enable the USART1 Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

3. Configure the corresponding GPIO port.

If the system's UART needs to be remapped, the GPIO_PinRemapConfig function needs to be used for remapping, such as: GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Note: Rx is floating, Tx is the second function pull-up.

Configure Rx to floating input mode and Tx to the second power mode with pull-up. And initialize it with GPIO_Init() function. For example:

/* Configure USART2 Rx PA3 input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Tx (PA.09) as alternate push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

4. Configure UART

After configuring the correct external crystal oscillator in the conf file, directly write the UART baud rate, communication frame length, mode, hardware communication control, and transceiver mode into the structure defined by USART_InitTypeDef. Then use USART_Init() to initialize. For example:

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;

/* Configure USART1 */

USART_Init(USART1, &USART_InitStructure);[page]

Then enable the transmit and receive interrupts. For example:

/* Enable USART1 Receive and Transmit interrupts */

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

Note: Generally, the TXE interrupt is not enabled. Once this interrupt is enabled, if the UART send buffer is empty, the UART interrupt will be entered immediately. Therefore, the TXE interrupt can be enabled in the program where data needs to be sent. Use USART_SendData() to send data in the UART interrupt.

After enabling the interrupt, you also need to enable the UART port:

like:

/* Enable the USART1 */

USART_Cmd(USART1, ENABLE);

/* Enable the USART2 */

USART_Cmd(USART2, ENABLE);

The interrupt program (stm32f10x_it.c) can complete the sending as follows: Note that all serial port interrupts need to determine the interrupt source in the interrupt service program for separate processing.

void USART1_IRQHandler(void)

{

if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

{

/* Read one byte from the receive data register */

RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);

if(RxCounter1 == NbrOfDataToRead1)

{

USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //After sending, disable RXNE.

}

}

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)

{

USART_SendData(USART1, TxBuffer1[TxCounter1++]);

if(TxCounter1 == NbrOfDataToTransfer1)

{

USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

}

}

}

Keywords:STM32  UART  MCU Reference address:The use of UART in STM32 microcontroller

Previous article:How to set the timer in STM32
Next article:Interrupt mechanism of STM32 microcontroller

Recommended ReadingLatest update time:2024-11-16 22:33

Universal USB interface module design AVR microcontroller
1 Introduction The USB interface has become one of the main ways for peripherals to communicate with PCs due to its fast data transmission, simple connection, easy expansion, and hot-swap support. With the development of embedded systems, embedded microprocessors need to add a universal USB interface to achieve commun
[Microcontroller]
Universal USB interface module design AVR microcontroller
Microcontroller assembly program coding standards
Software design is more of an engineering project than a personal art. If programming standards are not unified, the final program will be less readable, which will not only hinder the understanding of the code and increase the workload of the maintenance phase, but also the possibility of hidden errors in non-standar
[Microcontroller]
Research on SPWM variable frequency speed regulation system based on 8098 single chip microcomputer
At present, the research and development of high-performance AC speed regulation systems has attracted great attention from scholars from all over the world and has been studied more and more deeply. The selected microprocessor, power device and method of generating PWM wave are the direct factors affecting the perf
[Power Management]
Research on SPWM variable frequency speed regulation system based on 8098 single chip microcomputer
Hetai MCU PWM output program
;Content: PWM output controls the LED to gradually dim from bright to off ;Full source code download: http://www.51hei.com/f/htpwm.rar #INCLUDE HT66F50.INC ORG 00H MOV A,08H MOV CP0C,A ;Set the pin not as comparator pin MOV CP1C,A ;Set the pin not as comparator pin CLR ACERL ;Set the AD pin of the housekeeping
[Microcontroller]
51 MCU Learning Notes 3 -- Key Input Detection
1. Drawing of key schematic diagram The schematic diagram of the 51 development board is as follows Draw the circuit diagram according to the schematic diagram 2.Key input detection 1. Key software debounce Buttons generally utilize the closing and opening functions of mechanical contacts. Due to the elastic effe
[Microcontroller]
51 MCU Learning Notes 3 -- Key Input Detection
Hardware Design of LED Display Screen Based on AT89S52 Single Chip Microcomputer and ATF1508AS Programmable Logic Device
0 Introduction The LED display screen is mainly composed of three parts: current drive circuit and LED dot matrix array, control system and PC management software (Figure 1). The control system is responsible for receiving, converting and processing various external signals, and realizing scanning control, and t
[Microcontroller]
Hardware Design of LED Display Screen Based on AT89S52 Single Chip Microcomputer and ATF1508AS Programmable Logic Device
STM8 MCU ADC analog watchdog Chinese data error
  When debugging the ADC analog watchdog function of the stm8 microcontroller, no matter how you set the values ​​of the ADC_HTR and ADC_LTR registers, the values ​​of these two registers are incorrect when you observe them during single-step debugging using the IAR software.   According to the Chinese manual, t
[Microcontroller]
STM8 MCU ADC analog watchdog Chinese data error
Detailed introduction to the naming rules of PIC microcontrollers
PIC microcontroller is one of the commonly used devices. Every friend who studies PIC microcontroller will always encounter various difficulties in the learning process, such as the selection of PIC microcontroller. The editor has brought a brief introduction to the selection of PIC microcontroller. In this article, t
[Microcontroller]
Detailed introduction to the naming rules of PIC microcontrollers
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号