当前位置:网站首页>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);
}
}边栏推荐
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
- Failed building wheel for argon2 cffi when installing Jupiter
- 一款开源的Markdown转富文本编辑器的实现原理剖析
- Development of fire evacuation system
- [CSDN] C1 training problem analysis_ Part III_ JS Foundation
- Apply for domain name binding IP to open port 80 record
- Process communication - semaphore
- Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
- [successful graduation] [1] - visit [student management information system]
- NR technology -- MIMO
猜你喜欢

Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)

Flink learning notes (XI) table API and SQL

Nr-prach:prach format and time-frequency domain

Convert IP address to int
![[graduation successful] [1] - tour [Student Management Information System]](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[graduation successful] [1] - tour [Student Management Information System]

Development of electrical fire system

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

小王叔叔的博客目录【持续更新中】

Leetcode daily question (2090. K radius subarray averages)

What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
随机推荐
Call the contents of Excel cells opened at the same time - button line feed
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
Qt QComboBox QSS样式设置
Solve editor MD uploads pictures and cannot get the picture address
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 3 --blinker_ MIOT_ Light (lighting technology app control + Xiaoai classmate control)
Quickly use markdown to edit articles
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
UCI and data multiplexing are transmitted on Pusch (Part 4) --small block lengths
Vscode Arduino installation Library
UCI and data multiplexing are transmitted on Pusch - Part I
Leetcode daily question (516. long palindromic subsequence)
LeetCode每日一题(2109. Adding Spaces to a String)
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
LeetCode每日一题(1362. Closest Divisors)
Flink learning notes (10) Flink fault tolerance mechanism
用Redis实现分布式锁
Leetcode daily question (2212. maximum points in an archery competition)
LeetCode每日一题(745. Prefix and Suffix Search)
2021-09-26
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)