1. Experimental Principle
Simple serial communication example
1. The general steps of serial port settings can be summarized as follows:
1) Serial port clock enable, GPIO clock enable
2) Serial port reset
3) GPIO port mode setting
4) Serial port parameter initialization
5) Enable interrupts and initialize NVIC (this step is only required if interrupts need to be enabled)
6) Enable the serial port
7) Write an interrupt handling function
2. Specific function implementation
1) Enable serial port clock and GPIO clock: RCC_APB2PeriphClockCmd();
//①Serial port clock enable, GPIO clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //Serial port 1 clock enable
2) GPIO port mode setting: GPIO_Init(); mode is set to GPIO_Mode_AF_PP and GPIO_Mode_IN_FLOATING
//②TX
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP; //Check the Chinese manual to find the mode that the serial port needs to be configured in
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting
//②RX
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting
3) Serial port parameter initialization: USART_Init();
//③Serial port parameter initialization
USART_InitStrue.USART_BaudRate=115200; //Baud rate
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //No hardware flow
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //Both transmit and receive modes are turned on
USART_InitStrue.USART_Parity=USART_Parity_No; //No parity check
USART_InitStrue.USART_StopBits=USART_StopBits_1;//Stop bits
USART_InitStrue.USART_WordLength=USART_WordLength_8b;//Word length 8
USART_Init(USART1,&USART_InitStrue);
4) Enable interrupts and initialize NVIC (this step is only required if interrupts need to be enabled)
NVIC_Init();
USART_ITConfig();
//④
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//④ Enable receive interrupt!!!!
NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;;//Serial port 1 interrupt
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1; // The preemption priority is 1
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1; //Subpriority bit 1
NVIC_Init(&NVIC_InitStrue); //④Interrupt initialization function
5) Enable the serial port: USART_Cmd();
//⑤Enable the serial port
USART_Cmd(USART1,ENABLE);
6) Write interrupt handling function: USARTx_IRQHandler();
7) Serial port data transmission and reception:
void USART_SendData(); //Send data to the serial port, DR
uint16_t USART_ReceiveData(); //Receive data and read the received data from DR
//Interrupt processing function written by yourself (repeatedly defined in SYSTEM)⑥
void USART1_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE))//Judge whether there is an interrupt reception!
{
//Serial port data receiving and sending function⑦
res= USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}
8) Get the serial port transmission status (not used yet):
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
3. Use the Chinese manual to check the GPIO mode that needs to be configured for the serial port
2. Experimental Code
**main.c**
#include "stm32f10x.h"
void My_USART1_Init(void)//Simple serial port initialization function written by yourself, you can put a separate uart.c
{
//2, 3, 4 define structure pointers
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
//①Serial port clock enable, GPIO clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //Serial port 1 clock enable
//②TX
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP; //Check the Chinese manual to find the mode that the serial port needs to be configured in
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting
//②RX
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue); //GPIO port mode setting
//③Serial port parameter initialization
USART_InitStrue.USART_BaudRate=115200; //Baud rate
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //No hardware flow
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //Both transmit and receive modes are turned on
USART_InitStrue.USART_Parity=USART_Parity_No; //No parity check
USART_InitStrue.USART_StopBits=USART_StopBits_1;//Stop bits
USART_InitStrue.USART_WordLength=USART_WordLength_8b;//Word length 8
USART_Init(USART1,&USART_InitStrue);
//⑤Enable the serial port
USART_Cmd(USART1,ENABLE);
//④
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//④ Enable receive interrupt!!!!
NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;;//Serial port 1 interrupt
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1; // The preemption priority is 1
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1; //Subpriority bit 1
NVIC_Init(&NVIC_InitStrue); //④Interrupt initialization function
}
//Interrupt processing function written by yourself (repeatedly defined in SYSTEM)⑥
void USART1_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE))//Judge whether there is an interrupt reception!
{
//Serial port data receiving and sending function⑦
res= USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Interrupt priority grouping function
My_USART1_Init();
while(1); //wait for interrupt
}
3. Experimental Results
The configuration in the serial port assistant needs to be consistent with the initialization
//Serial port parameter initialization
USART_InitStrue.USART_BaudRate=115200; //Baud rate
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //No hardware flow
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //Both transmit and receive modes are turned on
USART_InitStrue.USART_Parity=USART_Parity_No; //No parity check
USART_InitStrue.USART_StopBits=USART_StopBits_1;//Stop bits
USART_InitStrue.USART_WordLength=USART_WordLength_8b;//Word length 8
USART_Init(USART1,&USART_InitStrue);
Previous article:STM32 Basic Experiment 4 (Key Interrupt)
Next article:stm32 basic experiment 2 (key input-query mode)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Moto 360 Wireless Charging Base Disassembly
- What is MU-MIMO and why is it important for Wi-Fi 6 and 6E?
- 【Lazy Self-Care Fish Tank Control System】Work Submission
- [Summer benefits to celebrate the Dragon Boat Festival] A large number of technical information is waiting for you to claim, and there are also gifts to help
- Please help me find which resistor in the picture is the 1.1K resistor in the formula?
- Xiaozhi Science Popularization丨How to Correctly Understand Power Supply Ripple and Noise
- Find the maximum voltage stress of the diode when it is working
- About QQ Chat at Work
- Naming method of TMS320 chip
- 【GD32L233C-START Review】Touch screen control LED based on serial communication