一、 GPIO 介绍
GPIO(General purpose input/output)即通用型 I/O ,AFIO(Alternate-functioninput/output)即复用功能 I/O。芯片最多支持 97 个 GPIO,共被分为 7 组( A/B/C/D/E/F/G ),每组 16个端口( F 组共 10 个, G 组共 7 个)。可以通过软件来控制这些管脚的基本状态,如管脚的高低电平、输入输出状态等。但是在使用 GPIO 管脚之前需要对其进行设置,主要包括外设时钟设置、输入输出模式设置和引脚速率设置。
①外设时钟设置使用库函数RCC_EnableAPB2PeriphClk ()
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
②输入输出模式、管脚速率设置
typedef struct
{
uint16_t Pin;
GPIO_SpeedType GPIO_Speed;
GPIO_ModeType GPIO_Mode;
} GPIO_InitType;
二、 LED灯控制原理
LED灯与GPIO管脚连接如下图所示,只需要将PA8、PB4、PB5设置为高电平,就可以点亮对应的LED灯。
三、 参数的宏定义
① LED 灯引脚
#define LED_G_GPIO_PIN GPIO_PIN_0
#define LED_G_GPIO_PORT GPIOB
#define RCC_APB2_PERIPH_GPIOA ((uint32_t)0x00000004)
typedef enum
{
Bit_RESET = 0,
Bit_SET
} Bit_OperateType;
② LED灯亮灭状态
typedef struct
{
__IO uint32_t PL_CFG;
__IO uint32_t PH_CFG;
__IO uint32_t PID;
__IO uint32_t POD;
__IO uint32_t PBSC;
__IO uint32_t PBC;
__IO uint32_t PLOCK_CFG;
uint32_t RESERVED0;
__IO uint32_t DS_CFG;
__IO uint32_t SR_CFG;
} GPIO_Module;
void LedOn(GPIO_Module* GPIOx, uint16_t Pin)
{
GPIOx->PBSC = Pin;
}
void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
{
GPIOx->PBC = Pin;
}
③ LED_GPIO的初始化
/* Configure the GPIO pin */
if (Pin <= GPIO_PIN_ALL)
{
GPIO_InitStructure.Pin = Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
}
④主函数
int main(void)
{
LedInit(LED1_PORT, LED1_PIN);
KeyInputInit(KEY_INPUT_PORT, KEY_INPUT_PIN);
while (1)
{
if (GPIO_ReadInputDataBit(KEY_INPUT_PORT, KEY_INPUT_PIN) == Bit_RESET)
{
/*Turn on Led1*/
LedOn(LED1_PORT, LED1_PIN); }
else
{
/*Turn off Led1*/
LedOff(LED1_PORT, LED1_PIN);
}
}
}
⑤描述 Demo 的测试步骤和现象
1.编译后下载程序复位运行;
2.将 PA0 接入 3.3V ,D1 灭;PA0 断开,D1 亮。
四、Reference:
[1] UM_N32G45x系列用户手册V2.0.0
[2] DS_N32G455系列数据手册V2.0.0
[3] Nationstech.N32G45x_Library.2.0.0
评论