STM32 Learning Notes-USART Interrupt

Publisher:捡漏来了Latest update time:2012-08-31 Source: 51heiKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/************************************
* File name: main.c
* Description: This example demonstrates how to operate the USART receiving program (interrupt mode).
* Use the serial port debugging software to send a byte to the development board, and the development board will send the byte back to the PC
*/
#include "stm32f10x_lib.h"
/***************************************************
* Function name: void RCC_Configuration()
* Function description: Reset and clock control configuration
* Parameter: None
* Return value: None
* Global variables: None
* Global static variables: None
* Local static variables: None
***********************************************************/
void RCC_Configuration()
{
ErrorStatus HSEStartUpStatus; //Define the external high-speed crystal oscillator startup status enumeration variable
RCC_DeInit(); //Reset the RCC external register to the default value
RCC_HSEConfig(RCC_HSE_ON); //Turn on the external high-speed crystal oscillator
HSEStartUpStatus=RCC_WaitForHSEStartUp(); //Wait for the external high-speed clock to be readyif

(HSEStartUpStatus==SUCCESS){ //External high-speed clock is ready

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable the FLASH pre-read buffer function to speed up the reading of FLASH. Required usage in all programs, location: in the RCC initialization subfunction, after the clock starts
FLASH_SetLatency(FLASH_Latency_2); //FLASH timing delays for several cycles, waiting for bus synchronization operation. It is recommended to follow the operating frequency of the microcontroller system, when 0-24MHz, take Latency=0; when 24-48MHz, take Latency=1; when 48~72MHz, take Latency=2.

RCC_HCLKConfig(RCC_SYSCLK_Div1); //Configure AHB (HCLK) == system clock / 1
RCC_PCLK2Config(RCC_HCLK_Div1); //Configure APB2 (high speed) (PCLK2) == system clock / 1
RCC_PCLK1Config(RCC_HCLK_Div2); //Configure APB1 (low speed) (PCLK1) == system clock / 2
//Note: AHB is mainly responsible for external memory clock. APB2 is responsible for AD, I/O, advanced TIM, serial port 1. APB1 is responsible for DA, USB, SPI, I2C, CAN, serial port 2345, ordinary TIM.


RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //Configure PLL clock == (external high-speed crystal clock/1) * 9 ==72MHz
RCC_PLLCmd(ENABLE); //Enable PLL clock
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); //Wait for PLL clock to be ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Configure system clock == PLL clock
while(RCC_GetSYSCLKSource()!=0x08); //Wait for system clock source to start
}

//------------------------The following is the operation to enable peripheral clock-----------------------//
// RCC_AHBPeriphClockCmd (ABP2 device 1 | ABP2 device 2, ENABLE); //Start AHB device
// RCC_APB2PeriphClockCmd(ABP2 device 1 | ABP2 device 2, ENABLE); //Start ABP2 device
// RCC_APB1PeriphClockCmd(ABP2Device1 | ABP2Device2, ENABLE); //Start ABP1 device

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE); //Open APB2 peripherals
}

/*************************************************
* Function name: NVIC_Configuration(void)
* Function description: NVIC (Nested Interrupt Controller) configuration
* Parameters: None
* Return value: None
* Global variables: None
* Global static variables: None
* Local static variables: None
*************************************************/
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure; //Define an interrupt structure

// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //Set the starting address of the interrupt vector table to 0x08000000
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //Set the NVIC priority grouping method.
//Note: There are 16 priorities in total, divided into preemptive and responsive. The number of the two priorities is determined by this code. NVIC_PriorityGroup_x can be 0, 1, 2, 3, 4,
//representing the preemptive priority of 1, 2, 4, 8, 16 and the responsive priority of 16, 8, 4, 2, 1. After specifying the number of two priorities, all interrupt levels must be selected from them.
//The high preemptive level will interrupt other interrupts and give priority to execution, while the high responsive level will give priority to execution after other interrupts are executed.

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; //Channel is set to serial port 1 interrupt
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Interrupt response priority 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Turn on interrupt
NVIC_Init(&NVIC_InitStructure); //Initialization
}

/********************************************
* Function name: GPIO_Configuration()
* Function description: GPIO configuration
* Parameters: None
* Return value: None
* Global variables: None
* Global static variables: None
* Local static variables: None
******************************************/
void GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure; //Define GPIO initialization structure

//--------Set USART1's TX Configured as multiplexed push-pull output AF_PP---------------------//
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //Pin position definition, the label can be NONE, ALL, 0 to 15.
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //Output speed 2MHz
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //Push-pull output mode Out_PP
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO initialization of group E

//--------Configure the RX of USART1 as multiplexed floating input IN_FLOATING---------------------//
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //Pin position definition
//It is meaningless to configure the output speed in input mode
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //Output speed 2MHz
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //Floating input IN_FLOATING
GPIO_Init(GPIOA,&GPIO_InitStructure); //C group GPIO initialization


}

/********************************************************
* Function name: USART1_Configuration()
* Function description: Configure USART1 data format, baud rate and other parameters
* Parameters: None
* Return value: None
* Global variables: None
* Global static variables: None
* Local static variables: None
*******************************************************/
void USART1_Configuration()
{
USART_InitTypeDef USART_InitStructure; //Serial port settings restore default parameters

USART_InitStructure.USART_BaudRate = 115200; //Baud rate 115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Word length 8 bits
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1-bit stop byte
USART_InitStructure.USART_Parity = USART_Parity_No; //No parity check
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Turn on Rx receiving and Tx sending functions
USART_Init(USART1, &USART_InitStructure); //Initialize
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //If the receive data register is full, an interrupt is generated
USART_Cmd(USART1, ENABLE); //Start the serial port

//-----The following statement solves the problem that the first byte cannot be sent correctly-----//
USART_ClearFlag(USART1, USART_FLAG_TC); // Clear flag
}
/********This is the interrupt service subroutine, in stm32f10x_it.c****************************/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //If the receive data register is full
{
USART_SendData(USART1, USART_ReceiveData(USART1)); //Send back to PC
while(USART_GetFlagStatus(USART1, USART_IT_TXE)==RESET);//Wait for sending to be completed
}
}

/******************************************************
* Function name: main()
* Function description: Main function
* Parameter: None
* Return value: None
* Global variables: None
* Global static variables: None
* Local static variables: None
************************************************/
int main()
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration() );
USART1_Configuration();

while(1){
}
}
Keywords:STM32 Reference address:STM32 Learning Notes-USART Interrupt

Previous article:STM32 Learning Notes-TIM3 Overflow Timing
Next article:STM32 Learning Notes ①-GPIO input, output, and detection

Recommended ReadingLatest update time:2024-11-16 21:25

Remapping port configuration of stm32
The AFIO clock external interrupt (EXTI) needs to be enabled only when the peripheral remapping function is needed. The registers related to AFIO are AFIO-EXTICR1, 2, and 3, which are used to select the input pin of the EXTIx external interrupt. Example: Remapping USART2 USART2 TX/RX is on PA.2/3 PA.2 is already used
[Microcontroller]
STM32 uses JTMS (PA13) and JTCK (PA14) as common I/O ports
When STM32 uses JTMS (PA13) and JTCK (PA14) as normal I/O ports, add the following code before initialization (the order cannot be reversed):   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable , ENABLE); like: void LED_Init(void) {    GPIO_InitTypeDef  GPIO_InitStructu
[Microcontroller]
STM32 input capture measurement frequency PWM duty cycle
I have read some information about STM32 input capture on the Internet. Some of the introductions are very good, but there are still some problems in the content. I have modified them slightly and you can refer to them. Understanding of important concepts (very important for understanding the input capture function, e
[Microcontroller]
[STM32] Input capture of general timer (Example: Input capture)
General Purpose Timer Input Capture Overview How Input Capture Works In the general timer block diagram, the top part (count clock selection), the middle part (time base unit), and the lower left part (input capture) are mainly involved. Here we mainly explain the lower left part (input capture), and the other
[Microcontroller]
[STM32] Input capture of general timer (Example: Input capture)
STM32 RCC principle
1. Clock source In STM32, there are 5 clock sources, namely HSI, HSE, LSI, LSE, and PLL.  ①HSI is a high-speed internal clock, RC oscillator, with a frequency of 8MHz;  ②HSE is a high-speed external clock, which can be connected to a quartz/ceramic resonator or an external clock source, with a frequency range of
[Microcontroller]
STM32 RCC principle
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 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]
Create a new project with STM32, KEIL, MDK
At present, I feel that this file structure arrangement is the most reasonable. First, create the following files in the selected folder: Then put the relevant files into the corresponding folder according to the chip. Now take STM32F103RCT6 as an example. Copy stm32f10x.h, system_stm32f10x.h, system_stm32f10x.copy
[Microcontroller]
Create a new project with STM32, KEIL, MDK
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号