当前位置:网站首页>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);
}
}边栏推荐
- Process communication - semaphore
- Leetcode daily question (2090. K radius subarray averages)
- Jetson nano custom boot icon kernel logo CBOOT logo
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)
- Nodemcu-esp8266 development board to build Arduino ide development environment
- LeetCode每日一题(2212. Maximum Points in an Archery Competition)
- Electronic product design, MCU development, circuit cloning
- 端午节快乐!—— canvas写的粽子~~~~~
- LeetCode每日一题(1162. As Far from Land as Possible)
- LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
猜你喜欢

MySQL Data Definition Language DDL common commands

全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶

Win10安装ELK

Leetcode daily question (2090. K radius subarray averages)

Flink学习笔记(八)多流转换

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

UCI and data multiplexing are transmitted on Pusch - Part I

LeetCode每日一题(1162. As Far from Land as Possible)

Characteristics of PUCCH formats

Development of electrical fire system
随机推荐
MySQL data manipulation language DML common commands
Trial of the combination of RDS and crawler
[combinatorics] Introduction to Combinatorics (combinatorial thought 2: mathematical induction | mathematical induction promotion | multiple induction thought)
一款开源的Markdown转富文本编辑器的实现原理剖析
Win10 install elk
MySQL Data Definition Language DDL common commands
QT qstring:: number apply base conversion
【顺利毕业】[1]-游览 [学生管理信息系统]
Nodemcu-esp8266 development board to build Arduino ide development environment
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
[CSDN]C1训练题解析_第三部分_JS基础
Epoll read / write mode in LT and et modes
Vscode Arduino installation Library
Long类型的相等判断
Spark cluster installation and deployment
LeetCode每日一题(2109. Adding Spaces to a String)
文件系统中的目录与切换操作
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
CEF下载,编译工程
Find all possible recipes from given supplies