当前位置:网站首页>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);
}
}
边栏推荐
- Uncle Wang's blog directory [constantly updating]
- [successful graduation] [1] - visit [student management information system]
- Flink learning notes (VIII) multi stream conversion
- Installation and uninstallation of pyenv
- 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
- QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
- Development of fire evacuation system
- 从0开始使用pnpm构建一个Monorepo方式管理的demo
- LeetCode每日一题(1996. The Number of Weak Characters in the Game)
- [male nanny style] teach you to open the first wechat applet
猜你喜欢
PolyWorks script development learning notes (4) - data import and alignment using file import
Convert IP address to int
【顺利毕业】[1]-游览 [学生管理信息系统]
小王叔叔的博客目录【持续更新中】
Leetcode daily question (1162. as far from land as possible)
Successful graduation [3]- blog system update...
Alibaba cloud notes for the first time
Successful graduation [2] - student health management system function development...
LeetCode每日一题(2090. K Radius Subarray Averages)
[CSDN] C1 training problem analysis_ Part III_ JS Foundation
随机推荐
[successful graduation] [1] - visit [student management information system]
LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
Epoll read / write mode in LT and et modes
顺利毕业[3]-博客系统 更新中。。。
制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
Error output redirection
Learn the contents of 5g toolbox supporting NR through the NR resources provided by MATLAB
Design and development of biological instruments
[combinatorics] Introduction to Combinatorics (context of combinatorics | skills of combinatorics | thought of combinatorics 1: one-to-one correspondence)
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Nodemcu-esp8266 development board to build Arduino ide development environment
Solve editor MD uploads pictures and cannot get the picture address
PolyWorks script development learning notes (II) -treeview basic operations
Analysis of the implementation principle of an open source markdown to rich text editor
Hudi quick experience (including detailed operation steps and screenshots)
[22 graduation season] I'm a graduate yo~
Leetcode daily question (968. binary tree cameras)
UCI and data multiplexing are transmitted on Pusch (Part 4) --small block lengths
LeetCode每日一题(1162. As Far from Land as Possible)
Jestson nano downloads updated kernel and DTB from TFTP server