寫在前面:
本文章旨在總結備份、方便以後查詢,由於是個人總結,如有不對,歡迎指正;另外,內容大部分來自網絡、書籍、和各類手冊,如若侵權請告知,馬上刪帖致歉。
目錄
一、GPIO模式
STM32的 GPIO模式有以下幾種:
- GPIO_Mode_AIN -- 模擬輸入
- GPIO_Mode_IN_FLOATING -- 輸入浮空
- GPIO_Mode_IPD -- 輸入下拉
- GPIO_Mode_IPU -- 輸入上拉
- GPIO_Mode_Out_OD -- 開漏輸出
- GPIO_Mode_Out_PP -- 推輓式輸出
- GPIO_Mode_AF_OD -- 開漏複用功能
- GPIO_Mode_AF_PP -- 推輓式複用功能
typedef enum
{
GPIO_Mode_AIN = 0x0,
GPIO_Mode_IN_FLOATING = 0x04,
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
GPIO_Mode_Out_OD = 0x14,
GPIO_Mode_Out_PP = 0x10,
GPIO_Mode_AF_OD = 0x1C,
GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;
爲了後面的代碼參考,順便在上面貼上 GPIO的模式結構體,也可以在 stm32f10x_gpio.h 文件中查看
根據上面的幾種模式,可以大致分爲 4種模式:輸入、輸出、複用、模擬
先來了解一下 I/O口總的功能結構,然後再細分
1、輸入
2、輸出
3、複用
4、模擬
二、外設 I/O配置模式選擇
三、GPIO配置代碼實現
本次只演示輸入、輸出的例子;另外的,後面也會一一應用
1、輸出之推輓輸出
/************************************************
函數名稱 : LED_Config
功 能 : LED配置
參 數 : 無
返 回 值 : 無
*************************************************/
void LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB.14 pin as output push pull */
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}
2、輸入之上拉輸入
/************************************************
函數名稱 : Key_Config
功 能 : 按鍵配置
參 數 : 無
返 回 值 : 無
*************************************************/
void Key_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Configure PB.01 pin as input pull up */
GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
/* Connect EXTI1 Line to PB.01 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
/* Configure EXTI1 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable the EXTI1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Generate software interrupt: simulate a falling edge applied on EXTI1 line */
EXTI_GenerateSWInterrupt(EXTI_Line1);
}
以上便是常用的一些 I/O配置;在輸入中,用到了 EXIT和 NVIC這兩個陌生的配置,現在你不需要弄懂他們,後面會詳細說
四、總工程實現
bsp_gpio.c 源文件
#include "bsp_gpio.h"
/************************************************
函數名稱 : LED_Config
功 能 : LED配置
參 數 : 無
返 回 值 : 無
*************************************************/
void LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB.14 pin as output push pull */
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
LED_OFF;
}
/************************************************
函數名稱 : Key_Config
功 能 : 按鍵配置
參 數 : 無
返 回 值 : 無
*************************************************/
void Key_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Enable AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Configure PB.01 pin as input pull up */
GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
/* Connect EXTI1 Line to PB.01 pin */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
/* Configure EXTI1 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable the EXTI1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Generate software interrupt: simulate a falling edge applied on EXTI1 line */
EXTI_GenerateSWInterrupt(EXTI_Line1);
}
/*---------------------------- END OF FILE ----------------------------*/
bsp_gpio.h 頭文件
#ifndef __BSP_GPIO_H
#define __BSP_GPIO_H
#include "stm32f10x.h"
#define LED_GPIO_PORT GPIOB
#define LED_GPIO_PIN GPIO_Pin_14
#define LED_ON GPIO_ResetBits(GPIOB, GPIO_Pin_14)
#define LED_OFF GPIO_SetBits(GPIOB, GPIO_Pin_14)
#define KEY_GPIO_PORT GPIOB
#define KEY_GPIO_PIN GPIO_Pin_1
void LED_Config(void);
void Key_Config(void);
#endif /* __BSP_GPIO_H */
/*---------------------------- END OF FILE ----------------------------*/
stm32f10x_it.c 源文件
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "bsp_gpio.h"
/******************************************************************************/
/* STM32F10x Peripherals Interrupt Handlers */
/******************************************************************************/
/**
* @brief This function handles External line 1 interrupt request.
* @param None
* @retval None
*/
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
if(GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN) == RESET)
{
LED_OFF;
}
/* Clear the EXTI line 1 pending bit */
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
main.c 主函數
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
/************************************************
函數名稱 : main
功 能 : 主函數入口
參 數 : 無
返 回 值 : 無
*************************************************/
int main(void)
{
/* Initial Configuration */
SystemInit();
/* -------- End -------- */
LED_ON;
/* Infinite loop */
while (1)
{
}
}