* 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){
}
}
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【GD32E503 Review】01. Unboxing and Hardware Evaluation
- Is this kind of isolation useful for external signal interference?
- Let's talk about which one is the ceiling of the ARM core in your mind, and how it works after experience
- Building a BLE development environment
- EEWORLD University Hall----Live Replay: 5G and Edge Computing Development and Technology Application
- Raspberry Pi Pico BASIC Interpreter
- EEWORLD University Hall----Enabling JTAG Communication on UCD3138 Series
- MSP430G2755 Main Memory Bootloader UART Porting Guide
- EEWORLD University Hall - A vivid introduction: What is Fourier transform
- Playing with Zynq Serial 28——[ex50] The first Zynq system project "Hello Zynq"