当前位置:网站首页>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);
}
}边栏推荐
- Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
- PIP configuring domestic sources
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
- 【顺利毕业】[1]-游览 [学生管理信息系统]
- Qt QComboBox QSS样式设置
- Usage of pandas to obtain MySQL data
- Chromium Embedded Framework (CEF) 介绍
- MySQL environment variable configuration
- [CSDN]C1訓練題解析_第三部分_JS基礎
- Flink learning notes (10) Flink fault tolerance mechanism
猜你喜欢

【22毕业季】我是毕业生yo~

Flink学习笔记(八)多流转换
![[CSDN]C1训练题解析_第三部分_JS基础](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1训练题解析_第三部分_JS基础
![[CSDN]C1訓練題解析_第三部分_JS基礎](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1訓練題解析_第三部分_JS基礎

Nr--- Pusch I: sorting out the agreement process

Leetcode daily question (1162. as far from land as possible)

Leetcode daily question (2090. K radius subarray averages)

Electronic product design, MCU development, circuit cloning

解决Editor.md上传图片获取不到图片地址问题

Please tell me how to set vscode
随机推荐
[CSDN] C1 training problem analysis_ Part II_ Web Foundation
Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
Flink学习笔记(八)多流转换
2021-09-26
Construction and test of TFTP server under unbuntu (Debian)
Arduino handles JSON data, arduinojson assistant
【22毕业季】我是毕业生yo~
Difference of EOF
Convert IP address to int
[CSDN]C1训练题解析_第二部分_Web基础
Leetcode daily question (931. minimum falling path sum)
unbuntu(debian)下TFTP服务器搭建及测试
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
Failed building wheel for argon2 cffi when installing Jupiter
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
Process communication - semaphore
The rise and fall of mobile phones in my perspective these 10 years
PolyWorks script development learning notes (II) -treeview basic operations
Flink learning notes (XI) table API and SQL
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)