The general steps of serial port settings can be summarized as follows:
1) Enable serial port clock, enable GPIO clock
2) Reset serial port
3) Set GPIO port mode
4) Initialize serial port parameters
5) Enable interrupts and initialize NVIC (only if interrupts need to be enabled)
6) Enable serial port
7) Write an interrupt handling function
Next, we will briefly introduce these firmware library functions that are directly related to the basic configuration of the serial port. These functions and definitions are mainly distributed in the stm32f10x_usart.h and stm32f10x_usart.c files.
1. Enable the serial port clock. The serial port is mounted on APB2, so the enabling function is:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1)
2. Serial port reset. When an abnormality occurs in a peripheral, the peripheral can be reset by resetting it, and then reconfiguring the peripheral to make it work again. Generally, when the system is just beginning to be configured, the peripheral will be reset first to make it work again. The reset is completed in the function USART_DeInit():
void USART_DeInit(USART_TypeDef* USARTx)
3. Serial port parameter initialization
void USART_Init() function:
voidUSART_Init(USART_TypeDef*USARTx,USART_InitTypeDef*USART_InitStruct);
effect:
Initialize the corresponding serial port according to the specified parameters (baud rate, word length, stop bit, parity, hardware flow control, etc.)
Mainly used to initialize register BRR and CR1, CR2, CR3 control registers
Example:
USART_InitTypeDefUSART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; //Baud rate setting;
USART_InitStructure.USART_WordLength= USART_WordLength_8b; //Word length is 8-bit data format
USART_InitStructure.USART_StopBits= USART_StopBits_1; //One 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); //Initialize the serial port
4. void USART_Cmd() function:
prototype:
voidUSART_Cmd(USART_TypeDef*USARTx,FunctionalStateNewState);
effect:
Enable the corresponding serial port, which is used to set the serial port enable bit of register CR1
Example:
USART_Cmd(USART1,ENABLE); //Enable serial port 1
5. void USART_ITConfig() function:
Prototype: voidUSART_ITConfig(USART_TypeDef*USARTx,
uint16_t USART_IT, FunctionalStateNewState);
Function: Enable the corresponding interrupt of the serial port and set the serial port control register
Example:
USART_ITConfig(USART1,USART_IT_RXNE, ENABLE); //Enable the read data register non-empty interrupt
6.USART_SendData() function:
prototype:
voidUSART_SendData(USART_TypeDef* USARTx, uint16_t Data);
effect:
Send data to the serial port.
Example:
USART_SendData(USART1,0x12);
7.uint16_tUSART_ReceiveData() function:
Prototype: uint16_t USART_ReceiveData(USART_TypeDef*USARTx)
Get the value most recently received by the serial port.
Example:
USART_ReceiveData(USART1);
8. Functions related to the four status flags:
FlagStatusUSART_GetFlagStatus(USART_TypeDef*USARTx,uint16_t USART_FLAG);
void USART_ClearFlag(USART_TypeDef*USARTx,uint16_t USART_FLAG);
ITStatusUSART_GetITStatus(USART_TypeDef*USARTx,uint16_t USART_IT);
void USART_ClearITPendingBit(USART_TypeDef*USARTx,uint16_t USART_IT);
The following is a complete initialization serial port function and an interrupt service function:
//Initialize IO serial port 1
//bound: baud rate
void uart_init(u32 bound){
//GPIO port settings
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);//Enable USART1, GPIOA clock
USART_DeInit(USART1); //Reset serial port 1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//Multiplexed push-pull output
GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//Floating input
GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize PA10
//Usart1 NVIC configuration
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//Preemption priority 3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//Sub priority 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ channel enable
NVIC_Init(&NVIC_InitStructure); // Initialize VIC registers according to the specified parameters
// USART initialization settings
USART_InitStructure.USART_BaudRate = bound; // Generally set to 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // Word length is 8-bit data format
USART_InitStructure.USART_StopBits = USART_StopBits_1; // One 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); //initialize the serial port
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //enable interrupt
USART_Cmd(USART1, ENABLE); //enable the serial port
}
void USART1_IRQHandler(void) //Serial port 1 interrupt service routine
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //If the clock tick number is defined, it means that ucosII is to be used.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //Receive interrupt (the received data must end with 0x0d 0x0a)
{
Res =USART_ReceiveData(USART1);//(USART1->DR);//Read received data
if((USART_RX_STA&0x8000)==0)//Reception is not completed
{
if(USART_RX_STA&0x4000)//Received 0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//Reception error, restart
else USART_RX_STA|=0x8000;//Reception is completed
}
else //Has not received 0X0D yet
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//Receive data error, restart receiving
}
}
}
}
#ifdef OS_TICKS_PER_SEC//If the clock tick number is defined, it means that ucosII is to be used.
OSIntExit();
#endif
}
Previous article:Summary of STM32 serial port setting process
Next article:Understanding of STM32's nvic
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
- Problems with sensor acquisition circuits
- What are the RF challenges of small cells for 5G networks?
- Basic Features of DSP TMS320C6000
- Let's take a look at this power switching circuit.
- What is the reason for the Flash Timeout error message when downloading the program on STM32F4?
- EEWORLD University ---- FreeRTOS on stm32 ST
- Read the white paper on Renesas Electronics' power module series and win double gifts: energy boost gift & 100% recommendation gift!
- Understanding common mode and differential mode
- 【McQueen Trial】Unboxing and microbit
- STM32f767zi UART