STM32F4系列GPIO配置

文章发布时间:

最后更新时间:

文章总字数:
1.3k

预计阅读时间:
6 分钟

在stm32系列单片机的学习过程中,大部分人是从f1系列开始,今天第一次接触到f4系列的配置问题,所以写这篇博客进行简单的解释

GPIO的基本结构见STM32学习笔记,f1系列和f4系列在结构上并无区别,只是在代码配置中有所区别

首先先看看在f1系列里面对GPIO的使能:

1
2
3
4
5
6
7
8
9
10
/*开启时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟

/*GPIO初始化*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA2引脚初始化为复用推挽输出
//受外设控制的引脚,均需要配置为复用模式

这里演示的是把PA2脚使能为推挽输出

我们可以将使能分为这么几步:

  1. 打开时钟
  2. 初始化GPIO
  3. Init函数完成初始化

f4系列的初始化的大致步骤也是类似:

1
2
3
4
5
6
7
8
9
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

GPIO_InitTypeDef Serial_Structure;
Serial_Structure.GPIO_Mode = GPIO_Mode_AF;
Serial_Structure.GPIO_Pin = GPIO_Pin_9;
Serial_Structure.GPIO_Speed = GPIO_Speed_50MHz;
Serial_Structure.GPIO_OType = GPIO_OType_PP;
Serial_Structure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &Serial_Structure);

这里演示的是把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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
******************************************************************************
* @file stm32f4xx_gpio.c
* @author MCD Application Team
* @version V1.8.0
* @date 04-November-2016
* @brief This file provides firmware functions to manage the following
* functionalities of the GPIO peripheral:
* + Initialization and Configuration
* + GPIO Read and Write
* + GPIO Alternate functions configuration
*
@verbatim
===============================================================================
##### How to use this driver #####
===============================================================================
[..]
(#) Enable the GPIO AHB clock using the following function
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.

(#) Peripherals alternate function:
(++) For ADC and DAC, configure the desired pin in analog mode using
GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AN;
(+++) For other peripherals (TIM, USART...):
(+++) Connect the pin to the desired peripherals' Alternate
Function (AF) using GPIO_PinAFConfig() function
(+++) Configure the desired pin in alternate function mode using
GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
(+++) Select the type, pull-up/pull-down and output speed via
GPIO_PuPd, GPIO_OType and GPIO_Speed members
(+++) Call GPIO_Init() function

(#) To get the level of a pin configured in input mode use GPIO_ReadInputDataBit()

(#) To set/reset the level of a pin configured in output mode use
GPIO_SetBits()/GPIO_ResetBits()

(#) During and just after reset, the alternate functions are not
active and the GPIO pins are configured in input floating mode (except JTAG
pins).

(#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
(PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
priority over the GPIO function.

(#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
The HSE has priority over the GPIO function.

@endverbatim
*
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/

通过阅读这段说明我们可以知道如何使用这个文件里面的函数
当然这篇博客只涉及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. 你使用的引脚
  2. 你选择的模式
  3. 你选择的传输速度

文档里同时也说明了模式有哪些:

  1. 输入
  2. 输出
  3. 复用
  4. 模拟

其中输入模式分为

  1. 悬空
  2. 上拉
  3. 下拉

输出分为:

  1. 推挽
  2. 开漏

复用需要同时设置输入和输出的两种

具体到代码:

1
2
3
4
5
6
7
8
9
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //开启时钟

GPIO_InitTypeDef Serial_Structure; //初始化结构体
Serial_Structure.GPIO_Mode = GPIO_Mode_AF; //选择模式
Serial_Structure.GPIO_Pin = GPIO_Pin_9; //选择引脚
Serial_Structure.GPIO_Speed = GPIO_Speed_50MHz; //选择速度
Serial_Structure.GPIO_OType = GPIO_OType_PP; //选择输出模式
Serial_Structure.GPIO_PuPd = GPIO_PuPd_UP; //选择输入模式
GPIO_Init(GPIOA, &Serial_Structure); //完成初始化

总结

两个系列GPIO的初始化结构相同,只有细微区别,在使用前可以打开对应文件阅读说明,便于更好的理解和使用对应的函数

给穷逼一点钱吧!
支付宝(AliPay)
微信(WeiChat)