5.8 GPIO 使用说明

本章说明 N32G033 标准外设库中 GPIO 与 AFIO 相关的文件组成、25 个公开函数,以及 LedBlink 1 个示例。引脚模式、复用功能、重映射与 EXTI 线的分配请配合用户手册的“GPIO 和 AFIO”章节阅读,芯片已知问题以勘误手册为准。

5.8.1 相关文件

相对路径 作用
firmware/n32g033_std_periph_driver/inc/n32g033_gpio.h 定义 GPIO 初始化结构体 GPIO_InitType 与输入输出模式、上下拉、复用功能等宏,并声明 25 个 GPIO 与 AFIO 配置、读写函数
firmware/n32g033_std_periph_driver/src/n32g033_gpio.c 实现上述 25 个函数:复位与初始化、输入输出读写、位操作与翻转、引脚锁定、EXTI 线选择与清除、复用与重映射配置、GPIOB 驱动能力与压摆率设置
projects/n32g033_EVAL/examples/GPIO/ GPIO 示例根目录,含 LedBlink 1 个示例
projects/n32g033_EVAL/examples/GPIO/LedBlink/ 该示例的工程目录组成:MDK-ARM/、EWARM/、inc/、src/、readme.txt
projects/n32g033_EVAL/bsp/ 示例共用的板级支持文件:delay.h 的 SysTick 延时接口

5.8.2 API 参考

搜索 API共 25 个函数

以下 25 个公开函数按 n32g033_gpio.h 的声明顺序分为 6 组;每个条目给出功能摘要与 n32g033_gpio.c 中该函数正上方的注释原文。

复位与初始化

GPIO_DeInit、GPIO_DeInitPin、GPIO_InitPeripheral、GPIO_InitStruct

输入输出读取

GPIO_ReadInputDataBit、GPIO_ReadInputData、GPIO_ReadOutputDataBit、GPIO_ReadOutputData

位操作与写入

GPIO_SetBits、GPIO_WritePBSC、GPIO_ResetBits、GPIO_WriteBit、GPIO_Write、GPIO_TogglePin

引脚锁定与 EXTI 线配置

GPIO_ConfigPinLock、GPIO_ConfigEXTILine、GPIO_ClearEXTILine

复用功能与重映射配置

AFIO_ConfigEXTIFlitEN、GPIO_ConfigPinRemap、AFIO_ConfigADCExternalTrigRemap、AFIO_ConfigSPINSSMode、AFIO_ConfigIOFlitNum、AFIO_DIGEFTEnable

GPIOB 驱动能力与压摆率

GPIOB_Driver_Set、GPIOB_SlewRate_Set

void GPIO_DeInit(void)把 GPIOx 外设寄存器复位为默认值。
源码注释
name   GPIO_DeInit.
fun    Reset the GPIOx peripheral registers to their default reset values.
param  none
return none
void GPIO_DeInitPin(GPIO_Module* GPIOx, uint32_t GPIO_Pin)把 GPIOx 外设寄存器复位为默认值。
源码注释
name   GPIO_DeInitPin.
fun    Deinitializes the GPIOx peripheral registers to their default reset values.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
          - GPIO_PIN_ALL
return none
note   If IO involves EXTI configuration, the corresponding EXTI line needs to be reset.
void GPIO_InitPeripheral(GPIO_Module* GPIOx, GPIO_InitType* GPIO_InitStruct)用 GPIO_InitStruct 结构体中的值初始化 GPIOx 外设。
源码注释
name   GPIO_InitPeripheral.
fun    Initialize the GPIOx peripheral with the value of the GPIO_InitStruct structure.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  GPIO_InitStructure :
            - pin
               - GPIO_PIN_0
               - GPIO_PIN_1
               - GPIO_PIN_2
               - GPIO_PIN_3
               - GPIO_PIN_4
               - GPIO_PIN_5
               - GPIO_PIN_6
               - GPIO_PIN_7
               - GPIO_PIN_8
               - GPIO_PIN_9
               - GPIO_PIN_10
               - GPIO_PIN_11
               - GPIO_PIN_12
               - GPIO_PIN_13
               - GPIO_PIN_14
               - GPIO_PIN_15
               - GPIO_PIN_ALL
            - GPIO_Mode
               - GPIO_MODE_INPUT
               - GPIO_MODE_OUTPUT_PP
               - GPIO_MODE_AF_PP
               - GPIO_MODE_AF_OD
               - GPIO_MODE_ANALOG
            - GPIO_Pull
               - GPIO_NO_PULL
               - GPIO_PULL_UP
               - GPIO_PULL_DOWN
            - GPIO_Alternate
               - GPIO_AF0
               - GPIO_AF1
               - GPIO_AF2
               - GPIO_AF3
               - GPIO_AF4
               - GPIO_AF5
               - GPIO_AF6
               - GPIO_AF7
               - GPIO_AF8
               - GPIO_AF9
               - GPIO_AF10
               - GPIO_AF11
               - GPIO_AF12
               - GPIO_NO_AF            NO alternate Function mapping
return none
note   PB3/PB4/PB5 can be configured with IO toggle rate and drive capability,
       requiring the call of GPIOB_Driver_Set and GPIOB_SlewRate_Set function.
void GPIO_InitStruct(GPIO_InitType* GPIO_InitStruct)为 GPIO_InitType 的各个成员赋默认值。
源码注释
name   GPIO_InitStruct.
fun    Assign default values to each GPIO_InitType member.
param  GPIO_InitStructure :
           pointer to GPIO_InitType structure.
return none
uint8_t GPIO_ReadInputDataBit(GPIO_Module* GPIOx, uint16_t Pin)获取指定输入端口上引脚的状态。
源码注释
name   GPIO_ReadInputDataBit.
fun    Get the pin status on the specified input port.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
return the pin state on the input port.
uint16_t GPIO_ReadInputData(GPIO_Module* GPIOx)获取指定 GPIO 端口的输入数据。
源码注释
name   GPIO_ReadInputData.
fun    Get the input data on the designated GPIO port.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
return the data value on the GPIO input port.
uint8_t GPIO_ReadOutputDataBit(GPIO_Module* GPIOx, uint16_t Pin)获取指定输出端口上引脚的状态。
源码注释
name   GPIO_ReadOutputDataBit.
fun    Get the pin status on the specified output port.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
return the pin state on the output port.
uint16_t GPIO_ReadOutputData(GPIO_Module* GPIOx)获取指定 GPIO 端口的输出数据。
源码注释
name   GPIO_ReadOutputData.
fun    Get the output data on the designated GPIO port.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
return the data value on the GPIO output port.
void GPIO_SetBits(GPIO_Module* GPIOx, uint16_t Pin)置位所选数据端口位。
源码注释
name   GPIO_SetBits.
fun    Sets the selected data port bits.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
return none
void GPIO_WritePBSC(GPIO_Module* GPIOx, uint16_t Pin,uint8_t bitstatus)通过 PBSC 置位所选数据端口位。
源码注释
name   GPIO_WritePBSC.
fun    Sets the selected data port bits using PBSC.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
param  bitstatus :
             - Bit_SET
             - Bit_RESET
return none
void GPIO_ResetBits(GPIO_Module* GPIOx, uint16_t Pin)复位所选数据端口位。
源码注释
name   GPIO_ResetBits.
fun    Reset the selected data port bits.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
return none
void GPIO_WriteBit(GPIO_Module* GPIOx, uint16_t Pin, Bit_OperateType BitCmd)复位所选数据端口位。
源码注释
name   GPIO_WriteBit.
fun    Reset the selected data port bits.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
param  BitCmd :
          - Bit_RESET
          - Bit_SET
return none

使用提示: 注释 fun 写作 Reset the selected data port bits.,只描述了复位行为,与实现不符:BitCmd 为 Bit_SET 时置位所选引脚、为 Bit_RESET 时复位,声明见 n32g033_gpio.h 第 235 行。

void GPIO_Write(GPIO_Module* GPIOx, uint16_t PortVal)在指定 GPIO 数据端口上写入数据。
源码注释
name   GPIO_Write.
fun    Write data on the designated GPIO data port.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  data_value :
          the value to be written to the port output data register.
          - 0~0xFFFF
return none
void GPIO_TogglePin(GPIO_Module* GPIOx, uint16_t Pin)翻转指定端口引脚电平。
源码注释
name   GPIO_TogglePin.
fun    Toggle the specified port pin level.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
return none
void GPIO_ConfigPinLock(GPIO_Module* GPIOx, uint16_t Pin)GPIO 端口锁定寄存器配置。
源码注释
name   GPIO_ConfigPinLock.
fun    GPIO port lock register configuration.
param  GPIOx :
          - GPIOA
          - GPIOB
          - GPIOF
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
return none
void GPIO_ConfigEXTILine(uint8_t PortSource, uint8_t PinSource)选择用作 EXTI 线的 GPIO 引脚,一条 EXTI 线只能配置一个引脚。
源码注释
name   GPIO_ConfigEXTILine.
fun    Selects the GPIO pin used as EXTI Line. A single EXTI line can only be configured with a single pin.
param  PortSource :
          - GPIOA_PORT_SOURCE
          - GPIOB_PORT_SOURCE
          - GPIOF_PORT_SOURCE
param  PinSource :
          - GPIO_PIN_SOURCE0
          - GPIO_PIN_SOURCE1
          - GPIO_PIN_SOURCE2
          - GPIO_PIN_SOURCE3
          - GPIO_PIN_SOURCE4
          - GPIO_PIN_SOURCE5
          - GPIO_PIN_SOURCE6
          - GPIO_PIN_SOURCE7
          - GPIO_PIN_SOURCE8
          - GPIO_PIN_SOURCE9
          - GPIO_PIN_SOURCE10
          - GPIO_PIN_SOURCE11
          - GPIO_PIN_SOURCE12
          - GPIO_PIN_SOURCE13
          - GPIO_PIN_SOURCE14
          - GPIO_PIN_SOURCE15
return none
void GPIO_ClearEXTILine( uint8_t LineSource)清除用作 EXTI 线的 GPIO 引脚。
源码注释
name   GPIO_ClearEXTILine.
fun    Clear the GPIO pin used as EXTI Line.
param  LineSource :
          - GPIO_EXTILINE_SOURCE0
          - GPIO_EXTILINE_SOURCE1
          - GPIO_EXTILINE_SOURCE2
          - GPIO_EXTILINE_SOURCE3
          - GPIO_EXTILINE_SOURCE4
          - GPIO_EXTILINE_SOURCE5
          - GPIO_EXTILINE_SOURCE6
return none
void AFIO_ConfigEXTIFlitEN(FunctionalState Cmd)配置 APB 时钟域下的 IO 滤波次数。
源码注释
name   AFIO_ConfigIOFlitNum.
fun    Configure IO filter number in APB clock.
param  Cmd :
          - ENABLE
          - DISABLE
return none

使用提示: 本条注释的 name 写成 AFIO_ConfigIOFlitNum.,与函数名 AFIO_ConfigEXTIFlitEN 不一致,声明见 n32g033_gpio.h 第 241 行。

void GPIO_ConfigPinRemap(uint8_t PortSource, uint8_t PinSource, uint32_t AlternateFunction)引脚重映射配置。
源码注释
name   GPIO_ConfigPinRemap.
fun    Pin remapping configuration.
param  PortSource :
          - GPIOA_PORT_SOURCE
          - GPIOB_PORT_SOURCE
          - GPIOF_PORT_SOURCE
param  PinSource :
          - GPIO_PIN_SOURCE0
          - GPIO_PIN_SOURCE1
          - GPIO_PIN_SOURCE2
          - GPIO_PIN_SOURCE3
          - GPIO_PIN_SOURCE4
          - GPIO_PIN_SOURCE5
          - GPIO_PIN_SOURCE6
          - GPIO_PIN_SOURCE7
          - GPIO_PIN_SOURCE8
          - GPIO_PIN_SOURCE9
          - GPIO_PIN_SOURCE10
          - GPIO_PIN_SOURCE11
          - GPIO_PIN_SOURCE12
          - GPIO_PIN_SOURCE13
          - GPIO_PIN_SOURCE14
          - GPIO_PIN_SOURCE15
param  AlternateFunction :
          - GPIO_AF0
          - GPIO_AF1
          - GPIO_AF2
          - GPIO_AF3
          - GPIO_AF4
          - GPIO_AF5
          - GPIO_AF6
          - GPIO_AF7
          - GPIO_AF8
          - GPIO_AF9
          - GPIO_AF10
          - GPIO_AF11
          - GPIO_AF12
          - GPIO_NO_AF            NO alternate Function mapping
return none
void AFIO_ConfigADCExternalTrigRemap(AFIO_ADC_Trig_RemapType ADC_trigger)配置 ADC 外部触发。
源码注释
name   AFIO_ConfigADCExternalTrigRemap.
fun    Configurate ADC external trigger.
param  ADC_trigger :
          specifies the external trigger line be configured.
          - AFIO_ADC_TRIG_EXTI_0
          - AFIO_ADC_TRIG_EXTI_1
          - AFIO_ADC_TRIG_EXTI_2
          - AFIO_ADC_TRIG_EXTI_3
          - AFIO_ADC_TRIG_EXTI_4
          - AFIO_ADC_TRIG_EXTI_5
          - AFIO_ADC_TRIG_EXTI_6
return none
void AFIO_ConfigSPINSSMode(uint32_t AFIO_SPIx_NSS, uint32_t SpiNssMode)选择 SPIx 复用功能的 NSS 模式。
源码注释
name   AFIO_ConfigSPINSSMode.
fun    Selects the alternate function SPIx NSS mode.
param  AFIO_SPIx_NSS :
          choose which SPI configuration.
          - AFIO_SPI1_NSS
          - AFIO_SPI2_NSS
param  SpiNssMode :
          specifies the SPI_NSS mode to be configured.
          - AFIO_SPI_NSS_HIGH_IMPEDANCE
          - AFIO_SPI_NSS_High_LEVEL
return none
void AFIO_ConfigIOFlitNum(uint8_t Flit_Num)配置 APB 时钟域下的 IO 滤波次数。
源码注释
name   AFIO_ConfigIOFlitNum.
fun    Configure IO filter number in APB clock.
param  Flit_Num :
        - 0~7
return none
void AFIO_DIGEFTEnable(uint8_t PortSource, uint16_t Pin, FunctionalState Cmd)使能或关闭端口引脚的数字 EFT。
源码注释
name   AFIO_DIGEFTEnable.
fun    Enables or disable digital EFT of port pins.
param  PortSource :
          - GPIOA_PORT_SOURCE
          - GPIOB_PORT_SOURCE
          - GPIOF_PORT_SOURCE
param  Pin :
          - GPIO_PIN_0
          - GPIO_PIN_1
          - GPIO_PIN_2
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
          - GPIO_PIN_6
          - GPIO_PIN_7
          - GPIO_PIN_8
          - GPIO_PIN_9
          - GPIO_PIN_10
          - GPIO_PIN_11
          - GPIO_PIN_12
          - GPIO_PIN_13
          - GPIO_PIN_14
          - GPIO_PIN_15
          - GPIO_PIN_ALL
param  Cmd :
          - ENABLE
          - DISABLE
return none
void GPIOB_Driver_Set(uint32_t current, uint32_t GPIO_Pin)GPIOB 驱动能力配置。
源码注释
name   GPIOB_Driver_Set.
fun    GPIOB driver configuration.
param  current
          - GPIOB_HIGH_DREIVE
          - GPIOB_LOW_DREIVE
param  GPIO_Pin
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
return none
void GPIOB_SlewRate_Set(uint32_t slew_rate, uint32_t GPIO_Pin)GPIOB 压摆率配置。
源码注释
name   GPIOB_SlewRate_Set.
fun    GPIOB slew rate configuration.
param  slew_rate
          - GPIOB_SLEW_RATE_FAST
          - GPIOB_SLEW_RATE_SLOW
param  GPIO_Pin
          - GPIO_PIN_3
          - GPIO_PIN_4
          - GPIO_PIN_5
return none

5.8.3 Demo 示例说明

本节说明 GPIO 的 1 个示例:LedBlink 用三个 GPIO 引脚驱动 LED,按固定节奏翻转与切换,展示初始化、位操作与端口写。

工程目录: projects/n32g033_EVAL/examples/GPIO/LedBlink/

使能 GPIO 时钟→把三个 LED 引脚配成推挽输出→点亮三个 LED→主循环翻转与切换各 LED 并延时

项目 源码解读
功能 让 PA6、PA7、PB1 三个 GPIO 引脚驱动 LED,按固定节奏翻转与切换,展示位操作与端口写。
源码组成 src/main.c 含 LedInit、LedOn、LedOff、LedBlink 四个封装函数与主循环;
inc/main.h 定义三个 LED 的端口与引脚宏;
工程文件在 MDK-ARM/ 与 EWARM/,readme.txt 给出配置与测试步骤。
关键配置 LED1 PA6、LED2 PA7、LED3 PB1 均配为推挽输出;
系统时钟 64 MHz;
主循环每步延时 1000 ms。
程序与数据路径 main → LedInit 使能 GPIO 时钟并按 GPIO_MODE_OUTPUT_PP 初始化三个引脚 → LedOn 点亮三个 LED → 主循环用 LedBlink、LedOff、LedOn 切换各 LED,并在每步用 SysTick_Delay_Ms 延时。
主要 API GPIO_InitStruct、GPIO_InitPeripheral、GPIO_SetBits、GPIO_ResetBits、GPIO_TogglePin
验证方法 编译烧录并复位,板上 D1、D2、D3 三个 LED 交替闪烁。
使用提示 开发环境为 KEIL MDK-ARM V5.34.0.0 或 IAR EWARM 8.50.1,硬件基于评估板 N32G033K8Q7-1_STB V1.0;
系统时钟 64 MHz。
核心代码解读与移植

示例把 LED 初始化与点亮、熄灭、翻转封装成四个辅助函数,再在主循环里组合调用完成闪烁。

数据路径: LedInit 使能 GPIO 时钟并用 GPIO_InitStruct、GPIO_InitPeripheral 把引脚配成推挽输出 → LedOn 经 GPIO_SetBits 点亮 → 主循环的 LedBlink、LedOff、LedOn 分别经 GPIO_TogglePin、GPIO_ResetBits、GPIO_SetBits 改变引脚状态 → SysTick_Delay_Ms 控制节奏

1. LED 初始化封装了结构体初始化与外设配置。 先给结构体赋默认值,再指定引脚与推挽输出模式后下发配置。

src/main.csrc/main.c · LedInit
void LedInit(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_InitType GPIO_InitStructure;

    /* Enable the GPIO Clock */
    RCC_EnableAPBPeriphClk(RCC_APB_PERIPH_GPIO, ENABLE);

    /* Configure the GPIO pin */
    if (Pin <= GPIO_PIN_ALL)
    {
        GPIO_InitStruct(&GPIO_InitStructure);
        GPIO_InitStructure.Pin = Pin;
        GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
    }
}

移植时先用 GPIO_InitStruct 给结构体赋默认值,再按需改 GPIO_Mode 与 GPIO_Pull,否则残留值可能把引脚配成其他模式。

2. 位操作封装把点亮、熄灭与翻转分别映射到库函数。 三个函数各自独立作用于指定引脚,不改变同端口其他引脚。

src/main.csrc/main.c · LedOn
void LedOn(GPIO_Module *GPIOx, uint16_t Pin)
{
    GPIO_SetBits(GPIOx, Pin);
}
src/main.csrc/main.c · LedOff
void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_ResetBits(GPIOx, Pin);
}
src/main.csrc/main.c · LedBlink
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_TogglePin(GPIOx, Pin);
}

LED1 与 LED2 同处 GPIOA,位操作按引脚独立生效,移植到多 LED 场景时可直接复用这几个封装。

3. 主循环用延时切换各 LED 的状态。 每一步延时 1000 ms,改变延时即可调整闪烁节奏。

src/main.csrc/main.c · main
    while (1)
    {
        /*LED1_PORT, LED2_PORT and LED3_PORT are the same port group.  
         *Enable Led2 blink and not effect Led1 and Led2 by Exclusive-OR operation.*/
        LedBlink(LED2_PORT, LED2_PIN);

        /*LED1_PORT, LED2_PORT and LED3_PORT are the same port group.
         *Turn Led3 off and not effect other ports by PBC register, correspond to PORT_GROUP->POD &= ~LED3_PIN*/
        LedOff(LED3_PORT, LED3_PIN);
        /* Insert delay */
        SysTick_Delay_Ms(1000);

        /*Turn Led1 off and not effect other ports by PBC register, correspond to PORT_GROUP->POD &= ~LED1_PIN*/
        LedOff(LED1_PORT, LED1_PIN);
        /* Insert delay */
        SysTick_Delay_Ms(1000);

        /*Turn on Led1 and Led3*/
        LedOn(LED1_PORT, LED1_PIN);
        LedOn(LED3_PORT, LED3_PIN);
        /* Insert delay */
        SysTick_Delay_Ms(1000);
    }

循环里对 LED2 只翻转、对 LED1 与 LED3 做熄灭与点亮切换,移植时可据此拆分不同的指示逻辑。

对应源码: src/main.c、inc/main.h、firmware/n32g033_std_periph_driver/inc/n32g033_gpio.h

5.8.4 注意与排障

场景 检查与建议 来源
寄存器访问宽度 用户手册 GPIO 和 AFIO 章写明 GPIO 相关寄存器必须以 32 位字访问,不支持 16 位半字与 8 位字节访问;
移植时避免按字节或半字读写 GPIO 寄存器。
用户手册
复位后的默认模式 用户手册 GPIO 和 AFIO 章写明除 NRST、SWD 引脚与 PF2 外,复位后各 GPIO 默认处于模拟模式;
用作数字输入输出前必须显式配置为对应模式。
用户手册
引脚锁定后无法再改 用户手册 GPIO 和 AFIO 章写明锁定序列为写 1、写 0、写 1、读 0,锁定后对应引脚配置固定直到复位;
需要保留可重配能力的引脚不要锁定。
用户手册
EXTI 线的引脚分组 用户手册 GPIO 和 AFIO 章写明每条 EXTI 线对应一组引脚,同一时刻只能选择其中一个引脚接入;
选择前确认目标引脚与 EXTI 线编号一致。
用户手册
用作外部中断的端口方向 用户手册中断和事件章写明端口必须工作于输入模式才能产生中断;
用 EXTI 触发时不要漏配输入模式。
用户手册

勘误手册 V1.0.0(2025.9.25)针对 A 版芯片,未列出 GPIO 相关条目。

国民技术开发套件文档中心

按芯片系列在线查阅国民技术 MCU 开发套件的芯片资料、SDK、驱动 API 与 Demo 示例说明。

© 2026 国民技术股份有限公司 版权所有粤ICP备11069485号本站使用不含 Cookie 的匿名访问统计