当前位置:网站首页>STM32 serial port usart1 routine
STM32 serial port usart1 routine
2022-07-03 09:41:00 【two thousand and twenty-one point zero nine】


One 、 Enable in main function GPIOA, and USART1
void My_USART1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
because USART1 Is to reuse GPIOA So enable GPIOA
For... Under the reuse function GPIO How to determine the mode , This needs to be checked 《 Chinese Reference Manual V10》 P110 Table for “8.1.11 Peripheral GPIO To configure ”. Configure full duplex serial port 1, that TX(PA9) The pin needs to be configured as push-pull multiplex output , RX(PA10) The pins are configured as floating input or with pull-up input .

Two 、IO Mouth initialization
void My_USART1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
}
3、 ... and 、 Serial initialization
void My_USART1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART_InitStruct);
}Four 、 Enable serial port 1
Put it in the serial port initialization function .
USART_Cmd(USART1,ENABLE);5、 ... and 、 Open interrupt
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
Here we use USART_IT_RXNE, The receive buffer is not empty , As long as you receive the data , Start the interrupt service function .
6、 ... and 、 Interrupt priority grouping
Sub priority = Response priority
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;// stay stm32f10x.h Inside looking for IRQn At the end of the
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;// Whether to open the interrupt channel
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; // preemption
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;// Sub priority = Response priority
NVIC_Init(&NVIC_InitStruct);// Set the corresponding interrupt preemption priority and response priority .7、 ... and 、 Interrupt service function
void USART1_IRQHandler(void)// Interrupt service function , It's the startup file startup_stm32f10x_hd.s It's defined in
{
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
res = USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}边栏推荐
- 数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
- Installation and uninstallation of pyenv
- Construction and test of TFTP server under unbuntu (Debian)
- LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)
- Alibaba cloud notes for the first time
- Jestson nano downloads updated kernel and DTB from TFTP server
- UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding
- Design and development of biological instruments
- Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
- Learning C language from scratch -- installation and configuration of 01 MinGW
猜你喜欢

Flink learning notes (IX) status programming

端午节快乐!—— canvas写的粽子~~~~~

Flink learning notes (XI) table API and SQL

Trial of the combination of RDS and crawler

Electronic product design

UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding

Apply for domain name binding IP to open port 80 record
![[CSDN]C1训练题解析_第二部分_Web基础](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[CSDN]C1训练题解析_第二部分_Web基础

LeetCode每日一题(2212. Maximum Points in an Archery Competition)

Common software open source protocols
随机推荐
Leetcode daily question (516. long palindromic subsequence)
【男保姆式】教你打开第一个微信小程序
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
[CSDN] C1 training problem analysis_ Part IV_ Advanced web
JMX、MBean、MXBean、MBeanServer 入门
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
Installation and uninstallation of pyenv
Qt QComboBox QSS样式设置
Flink CDC practice (including practical steps and screenshots)
QT qstring:: number apply base conversion
制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
Find all possible recipes from given supplies
Difference of EOF
Learn the contents of 5g toolbox supporting NR through the NR resources provided by MATLAB
Characteristics of PUCCH formats
Leetcode daily question (745. prefix and suffix search)
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
Hudi quick experience (including detailed operation steps and screenshots)
PolyWorks script development learning notes (4) - data import and alignment using file import
CEF下载,编译工程