首页 技术 正文
技术 2022年11月21日
0 收藏 340 点赞 2,980 浏览 2704 个字

端口复用的定义

STM32有许多的内置外设(如串口、ADC、DCA等等),这些外设的外部引脚都是和GPIO复用的。也就是说,一个GPIO如果可以复用为内置外设的功能引脚,那么当这个GPIO作为内置外设使用的时候,就叫复用。详细的可以参考《STM32F103ZET6数据手册》p30的内容,表格的倒数第二栏就表示端口复用功能。

比如说,STM32的串口1的引脚对应的I/O位PA9、PA10。而PA9、PA10默认的功能都是GPIO,所以说当PA9、PA10引脚作为串口1使用的时候就是端口复用。

在配置GPIO引脚功能时需要打开对应端口(port)所在的总线时钟,例如:

#define RCC_AHB1Periph_GPIOA               ((uint32_t)0x00000001)
#define RCC_AHB1Periph_GPIOB               ((uint32_t)0x00000002)
#define RCC_AHB1Periph_GPIOC               ((uint32_t)0x00000004)
#define RCC_AHB1Periph_GPIOD               ((uint32_t)0x00000008)
#define RCC_AHB1Periph_GPIOE               ((uint32_t)0x00000010)
#define RCC_AHB1Periph_GPIOF               ((uint32_t)0x00000020)
#define RCC_AHB1Periph_GPIOG              ((uint32_t)0x00000040)
#define RCC_AHB1Periph_GPIOH               ((uint32_t)0x00000080)
#define RCC_AHB1Periph_GPIOI                ((uint32_t)0x00000100)
#define RCC_AHB1Periph_GPIOJ               ((uint32_t)0x00000200)
#define RCC_AHB1Periph_GPIOK              ((uint32_t)0x00000400)
#define RCC_AHB1Periph_CRC                 ((uint32_t)0x00001000)
#define RCC_AHB1Periph_FLITF               ((uint32_t)0x00008000)
#define RCC_AHB1Periph_SRAM1             ((uint32_t)0x00010000)
#define RCC_AHB1Periph_SRAM2             ((uint32_t)0x00020000)
#define RCC_AHB1Periph_BKPSRAM        ((uint32_t)0x00040000)
#define RCC_AHB1Periph_SRAM3            ((uint32_t)0x00080000)
#define RCC_AHB1Periph_CCMDATARAMEN      ((uint32_t)0x00100000)
#define RCC_AHB1Periph_DMA1              ((uint32_t)0x00200000)
#define RCC_AHB1Periph_DMA2              ((uint32_t)0x00400000)
#define RCC_AHB1Periph_DMA2D             ((uint32_t)0x00800000)
#define RCC_AHB1Periph_ETH_MAC           ((uint32_t)0x02000000)
#define RCC_AHB1Periph_ETH_MAC_Tx        ((uint32_t)0x04000000)
#define RCC_AHB1Periph_ETH_MAC_Rx        ((uint32_t)0x08000000)
#define RCC_AHB1Periph_ETH_MAC_PTP       ((uint32_t)0x10000000)
#define RCC_AHB1Periph_OTG_HS            ((uint32_t)0x20000000)
#define RCC_AHB1Periph_OTG_HS_ULPI       ((uint32_t)0x40000000)

端口复用初始化过程

接下来看一下端口复用初始化过程的步骤,拿串口1为例:

1、GPIO端口时钟使能。要使用到端口复用,首先是要使能端口的时钟了;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

2、复用的外设时钟使能。比如要将PA9、PA10引脚复用成串口,必须也要使能串口时钟;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

3、端口模式配置。在I/O复用位内置外设功能引脚的时候,必须设置GPIO端口的模式。至于在复用功能下,GPIO的模式怎么设置,可以查看手册《STM32中文参考手册》p110的内容。这里拿USART1为例,进行配置,要配置全双工的串口1,TX引脚需要推挽复用输出,RX引脚需要浮空输入或者上拉输入;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9//复用推挽输出

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 PA.10 浮空输入

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入

GPIO_Init(GPIOA, &GPIO_InitStructure);

总而言之,使用复用功能的时候至少要使能2时钟:GPIO时钟使能、复用的外设时钟使能。同时还要初始化GPIO以及复用外设功能(端口模式配置)。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848