STM32串口——5个串口的使用方法

news/2024/11/22 8:13:09/

参考文档

STM32串口——5个串口的使用方法_51CTO博客_stm32串口通信的接收与发送

串口是我们常用的一个数据传输接口,STM32F103系列单片机共有5个串口,其中1-3是通用同步/异步串行接口USART(Universal Synchronous/Asynchronous Receiver/Transmitter),4,、5是通用异步串行接口UART(Universal Asynchronous Receiver/Transmitter)。

配置串口包括三部分内容:

1.  I/O口配置:TXD配置为复用推挽输出(GPIO_Mode_AF_PP),RXD配置为浮空输入(GPIO_Mode_IN_FLOATING);

2.  串口配置:波特率等;

3.  中断向量配置:一般用中断方式接收数据。

注意事项:

1.  USART1是挂在APB2,使能时钟命令为:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );

其他几个则挂在APB1上,如2口:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );

2.  配置4口和5口的时候,中断名为UART4、UART5,中断入口分别为

UART4_IRQn、UART5_IRQn

对应的中断服务函数为

void UART4_IRQHandler(void)

void UART5_IRQHandler(void)。

#include "stm32f10x.h"
#include "misc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h" void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位;
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//无硬件流控;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//收发模式;
USART_Init(USART1, &USART_InitStructure);//配置串口参数;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); //使能串口;
}void USART1_Send_Byte(u8 Data) //发送一个字节;
{
USART_SendData(USART1,Data);
while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );
}void USART1_Send_String(u8 *Data) //发送字符串;
{
while(*Data)
USART1_Send_Byte(*Data++);
}void USART1_IRQHandler(void) //中断处理函数;
{
u8 res;    
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //判断是否发生中断;
{
USART_ClearFlag(USART1, USART_IT_RXNE); //清除标志位;
res=USART_ReceiveData(USART1); //接收数据;
USART1_Send_Byte(res); //用户自定义;
}  
} void USART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART2 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //USART2 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位;
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//无硬件流控;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//收发模式;
USART_Init(USART2, &USART_InitStructure);//配置串口参数;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE); //使能串口;
}void USART2_Send_Byte(u8 Data) //发送一个字节;
{
USART_SendData(USART2,Data);
while( USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET );
}void USART2_Send_String(u8 *Data) //发送字符串;
{
while(*Data)
USART2_Send_Byte(*Data++);
}void USART2_IRQHandler(void) //中断处理函数;
{
u8 res;    
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) //判断是否发生中断;
{
USART_ClearFlag(USART2, USART_IT_RXNE); //清除标志位;
res=USART_ReceiveData(USART2); //接收数据;
USART2_Send_Byte(res); //用户自定义;
}  
} void USART3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART3 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //端口B;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //USART3 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOB, &GPIO_InitStructure); //端口B;USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位;
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//无硬件流控;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//收发模式;
USART_Init(USART3, &USART_InitStructure);//配置串口参数;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE); //使能串口;
}void USART3_Send_Byte(u8 Data) //发送一个字节;
{
USART_SendData(USART3,Data);
while( USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET );
}void USART3_Send_String(u8 *Data) //发送字符串;
{
while(*Data)
USART3_Send_Byte(*Data++);
}void USART3_IRQHandler(void) //中断处理函数;
{
u8 res;    
if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET) //判断是否发生中断;
{
USART_ClearFlag(USART3, USART_IT_RXNE); //清除标志位;
res=USART_ReceiveData(USART3); //接收数据;
USART3_Send_Byte(res); //用户自定义;
}  
} void UART4_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE );GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //UART4 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //UART4 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位;
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//无硬件流控;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//收发模式;
USART_Init(UART4, &USART_InitStructure);//配置串口参数;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE); //使能串口;
}void UART4_Send_Byte(u8 Data) //发送一个字节;
{
USART_SendData(UART4,Data);
while( USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET );
}void UART4_Send_String(u8 *Data) //发送字符串;
{
while(*Data)
UART4_Send_Byte(*Data++);
}void UART4_IRQHandler(void) //中断处理函数;
{
u8 res;    
if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET) //判断是否发生中断;
{
USART_ClearFlag(UART4, USART_IT_RXNE); //清除标志位;
res=USART_ReceiveData(UART4); //接收数据;
UART4_Send_Byte(res); //用户自定义;
}  
} void UART5_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE );GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //UART5 TX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //UART5 RX;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
GPIO_Init(GPIOD, &GPIO_InitStructure); //端口D;USART_InitStructure.USART_BaudRate = 9600; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位;
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//无硬件流控;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//收发模式;
USART_Init(UART5, &USART_InitStructure);//配置串口参数;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_Cmd(UART5, ENABLE); //使能串口;
}void UART5_Send_Byte(u8 Data) //发送一个字节;
{
USART_SendData(UART5,Data);
while( USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET );
}void UART5_Send_String(u8 *Data) //发送字符串;
{
while(*Data)
UART5_Send_Byte(*Data++);
}void UART5_IRQHandler(void) //中断处理函数;
{
u8 res;    
if(USART_GetITStatus(UART5, USART_IT_RXNE) == SET) //判断是否发生中断;
{
USART_ClearFlag(UART5, USART_IT_RXNE); //清除标志位;
res=USART_ReceiveData(UART5); //接收数据;
UART5_Send_Byte(res); //用户自定义;
}  
}


STM32串口——5个串口的使用方法
https://blog.51cto.com/u_14970037/5666911


http://www.ppmy.cn/news/1548699.html

相关文章

ubuntu固定ip

根据自己的VMware中的网关信息和ip信息设置。 然后进入到ubuntu虚拟机的网络设置目录 cd /etc/netplan nano 01-network-manager-all.yaml根据自己的ip来设置!](https://i-blog.csdnimg.cn/direct/f0be245ced5143618c059d6f0734ed81.jpeg#pic_center) 应用你的设置 sudo ne…

Spring Cache之本地缓存注解@Cacheable,@CachePut,@CacheEvict使用

*使用前需要在启动类上添加一个注解:EnableCaching 先说一下SpringCache是不支持灵活的缓存时间设置的,但可以自己实现。且不支持集群,因为是缓存到每台机器上,除非所有机器都有缓存。 所以使用场景一般是数据量较小的单机服务。…

python爬虫数据(小说)

""" 确定目标网站:https://www.wxscs.com/book/9422/ 内容页: """ #引入网页请求模块 import requests #网页主界面 url "https://www.wxscs.com/book/9422/" #伪造亲求头部 headers {"User-Agent"…

Linux高阶——1118—

1、sock.h socket常用函数 int ACCEPT(int,struct sockaddr*,socklen_t *); int SOCKET(int,int,int); ssize_t RECV(int,void*,size_t,int); int CONNECT(int, const struct sockaddr *,socklen_t); int BIND(int, const struct sockaddr *,socklen_t); int LISTEN(int, in…

配置NVIDIA Container Runtime和容器运行GPUStack教程

GPUStack 是一个设计用于运行大模型的开源 GPU 集群管理器,提供私有部署的大模型服务,支持大语言模型、Embedding 文本嵌入模型、Reranker 重排序模型、Vision 多模态模型等各种模型。它可以聚合不同平台(如 Apple Mac、Windows PC 和 Linux …

鸿蒙动画开发07——粒子动画

1、概 述 粒子动画是在一定范围内随机生成的大量粒子产生运动而组成的动画。 动画元素是一个个粒子,这些粒子可以是圆点、图片。我们可以通过对粒子在颜色、透明度、大小、速度、加速度、自旋角度等维度变化做动画,来营造一种氛围感,比如下…

【STM32】启动配置和自动串口下载

文章目录 0 前言1 启动配置2 自动下载电路3 一键下载电路只能是这样吗?4 深度思考5 总结 0 前言 最近在研究STM32时,在下载程序时遇到了一些问题,在查找相关资料并结合自己的实践后,突然发现自己之前对STM32程序下载和启动配置等理…

基于Java Springboot超市在线销售系统

一、作品包含 源码数据库设计文档万字PPT全套环境和工具资源部署教程 二、项目技术 前端技术:Html、Css、Js、Vue、Element-ui 数据库:MySQL 后端技术:Java、Spring Boot、MyBatis 三、运行环境 开发工具:IDEA/eclipse 数据…