STM32F4系列GPIO配置
最后更新时间:
文章总字数:
预计阅读时间:
在stm32系列单片机的学习过程中,大部分人是从f1系列开始,今天第一次接触到f4系列的配置问题,所以写这篇博客进行简单的解释
GPIO的基本结构见STM32学习笔记,f1系列和f4系列在结构上并无区别,只是在代码配置中有所区别
首先先看看在f1系列里面对GPIO的使能:
1 |
|
这里演示的是把PA2脚使能为推挽输出
我们可以将使能分为这么几步:
- 打开时钟
- 初始化GPIO
- Init函数完成初始化
f4系列的初始化的大致步骤也是类似:
1 |
|
这里演示的是把PA9使能为推挽输出
可以注意到的是,两个GPIO使能的代码有细微的差别,比如:
f1中的时钟开启函数是:RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
,而f4是:RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
f4的初始化结构体比f1多OType和PuPd需要配置
这些区别可以在各自的固件库文件中的stm32f4xx_gpio.c/h和stm32f1xx_gpio.c/h文件中找到
这边只给出f4系列配置的过程
在固件库每一个.c文件的开头都会有一段解释对应用法的说明
f4 GPIO文件讲解
stm32f4xx_gpio.c前面给出的说明:
1 |
|
通过阅读这段说明我们可以知道如何使用这个文件里面的函数
当然这篇博客只涉及GPIO的初始化过程,所以我们只会讲解其中一部分,剩余可以自行去了解
从 ##### How to use this driver ##### 后面开始就是正式的讲解
这里我们只说明前面两段,这两段的内容足够我们完成GPIO的见到那配置
第一段:
Enable the GPIO AHB clock using the following function
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
即使用RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
语句来进行时钟使能
第二段:
Configure the GPIO pin(s) using GPIO_Init()
Four possible configuration are available for each pin:
(++) Input: Floating, Pull-up, Pull-down.
(++) Output: Push-Pull (Pull-up, Pull-down or no Pull)
Open Drain (Pull-up, Pull-down or no Pull). In output mode, the speed
is configurable: 2 MHz, 25 MHz, 50 MHz or 100 MHz.
(++) Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull) Open
Drain (Pull-up, Pull-down or no Pull).
(++) Analog: required mode when a pin is to be used as ADC channel or DAC
output.
这段讲解了结构体的配置,你需要确定的有:
- 你使用的引脚
- 你选择的模式
- 你选择的传输速度
文档里同时也说明了模式有哪些:
- 输入
- 输出
- 复用
- 模拟
其中输入模式分为
- 悬空
- 上拉
- 下拉
输出分为:
- 推挽
- 开漏
复用需要同时设置输入和输出的两种
具体到代码:
1 |
|
总结
两个系列GPIO的初始化结构相同,只有细微区别,在使用前可以打开对应文件阅读说明,便于更好的理解和使用对应的函数