当前位置: 首页 > news >正文

STM32F4XX - uart设置

初始化一个波特率为115200的串口。下面函数参数为115200.
代码如下:

void uart1_init(u32 bound)
{GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);    //Enable the gpio clock //使能GPIO时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Enable the Usart clock //使能USART时钟GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10 ,GPIO_AF_USART1);        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;            //输出模式GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;          //推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;       //高速50MHZGPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;            //上拉GPIO_Init(GPIOA, &GPIO_InitStructure);                            //初始化//UsartNVIC configuration //UsartNVIC配置NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//Preempt priority //抢占优先级NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//Subpriority //子优先级NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;              //Enable the IRQ channel //IRQ通道使能NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Initialize the VIC register with the specified parameters //根据指定的参数初始化VIC寄存器 NVIC_Init(&NVIC_InitStructure); //USART Initialization Settings 初始化设置USART_InitStructure.USART_BaudRate = bound; //Port rate //串口波特率USART_InitStructure.USART_WordLength = USART_WordLength_8b; //The word length is 8 bit data format //字长为8位数据格式USART_InitStructure.USART_StopBits = USART_StopBits_1; //A stop bit //一个停止位USART_InitStructure.USART_Parity = USART_Parity_No; //Prosaic parity bits //无奇偶校验位USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control //无硬件数>据流控制USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Sending and receiving mode //收发模式USART_Init(USART1, &USART_InitStructure); //Initialize serial port 1 //初始化串口1USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Open the serial port to accept interrupts //开启串口接受中断USART_Cmd(USART1, ENABLE);                     //Enable serial port 1 //使能串口1
}

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable the gpio clock //使能GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Enable the Usart clock //使能USART时钟
设置GPIO和uart时钟(我的理解是GPIO直接挂到AHB上,所以需要基于AHB总线使能对应GPIO时钟。uart控制器是挂到APB上,所以需要基于APB总线使能uart时钟)

GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10 ,GPIO_AF_USART1);
将GPIOA的pin9、pin10配置成功能uart1。配置的寄存器为复用功能寄存器AFR

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //高速50MHZ
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化
对GPIO引脚进行配置,这个在GPIO端口配置部分有说明。

//UsartNVIC configuration //UsartNVIC配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //Preempt priority //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ; //Subpriority //子优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Enable the IRQ channel //IRQ通道使能
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Initialize the VIC register with the specified parameters
//根据指定的参数初始化VIC寄存器
NVIC_Init(&NVIC_InitStructure);
1. 配置中断通道为USART1_IRQn。
2. 配置抢占中断优先级为1,这个值越小,中断优先级越高。
3. 配置响应中断优先级为0。
4. 使能uart1中断
说明:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);优先级分组设置为group 4即4位抢占优先级即2^4种优先级(0-16),0位响应优先级。所以在设置时只有抢占优先级有效。

//USART Initialization Settings 初始化设置
USART_InitStructure.USART_BaudRate = bound; //Port rate //串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //The word length is 8 bit data format //字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //A stop bit //一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //Prosaic parity bits //无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control //无硬件数>据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Sending and receiving mode //收发模式
USART_Init(USART1, &USART_InitStructure); //Initialize serial port 1 //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Open the serial port to accept interrupts //开启串口接受中断
USART_Cmd(USART1, ENABLE); //Enable serial port 1 //使能串口1
这个比较简单了,就是初始化串口,根据串口协议进行配置,打开中断,使能串口。

相关文章:

  • 2024年漳州本地有正规等保测评机构吗?在哪里?
  • vue2+element医院安全(不良)事件报告管理系统源代码
  • Stable Diffusion 模型的概念、类型、下载、安装、使用
  • C# OpenCvSharp 颜色反转
  • 论文精读--GPT1
  • 【操作系统】磁盘存储空间的管理
  • List集合之UML、特点、遍历方式、迭代器原理、泛型、装拆箱及ArrayList、LinkedList和Vector的区别
  • 在Linux操作系统的ECS实例上安装Hive
  • mysql 输出所在月份的最后一天
  • xrpc: 一个基于消息队列的的Go语言RPC框架
  • 第九届大数据与计算国际会议 (ICBDC 2024) 即将召开!
  • HTTP 与 HTTPS-HTTP 解决了 HTTP 哪些问题?
  • 基于SVM的功率分类,基于支持向量机SVM的功率分类识别,Libsvm工具箱详解
  • 在 Windows 上使用 VC++ 编译 OpenSSL 源码的步骤
  • MySQL多实例部署:从概念到实操的全面指南
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 【Amaple教程】5. 插件
  • 【刷算法】从上往下打印二叉树
  • CentOS7简单部署NFS
  • Elasticsearch 参考指南(升级前重新索引)
  • Facebook AccountKit 接入的坑点
  • interface和setter,getter
  • java 多线程基础, 我觉得还是有必要看看的
  • JavaScript函数式编程(一)
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • PV统计优化设计
  • Python十分钟制作属于你自己的个性logo
  • Redis字符串类型内部编码剖析
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • uni-app项目数字滚动
  • 前嗅ForeSpider采集配置界面介绍
  • 微信公众号开发小记——5.python微信红包
  • 新书推荐|Windows黑客编程技术详解
  • 做一名精致的JavaScripter 01:JavaScript简介
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • #Z0458. 树的中心2
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (论文阅读30/100)Convolutional Pose Machines
  • (一)SpringBoot3---尚硅谷总结
  • (译)2019年前端性能优化清单 — 下篇
  • (转)为C# Windows服务添加安装程序
  • ***检测工具之RKHunter AIDE
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • .NET高级面试指南专题十一【 设计模式介绍,为什么要用设计模式】
  • .NET序列化 serializable,反序列化
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题