• 兆易创新 GD32 系列(三) 标准库初探,点灯LED


    点灯是跑程序的第一步,犹如灯塔照亮了前进的路。无论干过多少年,你都是要点灯,MCU的新手村
    在GD32 系列(二) 中认识到 gd32f4xx_libopt.h的作用,它包含了所有的标准库头文件
    点灯要用到的就是GPIO操作 参考标准工程即可

    void  gd_eval_led_init (led_typedef_enum lednum)
    {
        /* enable the led clock */
        rcu_periph_clock_enable(GPIO_CLK[lednum]);
        /* configure led GPIO port */ 
        gpio_mode_set(GPIO_PORT[lednum], GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN[lednum]);
        gpio_output_options_set(GPIO_PORT[lednum], GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN[lednum]);
    
        GPIO_BC(GPIO_PORT[lednum]) = GPIO_PIN[lednum];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    一个外设如果要正常工作,首先要启动它对应的输入时钟
    rcu_periph_clock_enable 是所有外设初始化都需要使用到的部分

    /*!
        \brief    enable the peripherals clock
        \param[in]  periph: RCU peripherals, refer to rcu_periph_enum
                    only one parameter can be selected which is shown as below:
          \arg        RCU_GPIOx (x = A, B, C, D, E, F, G, H, I): GPIO ports clock
          \arg        RCU_CRC: CRC clock
          \arg        RCU_BKPSRAM: BKPSRAM clock
          \arg        RCU_TCMSRAM: TCMSRAM clock
          \arg        RCU_DMAx (x=0,1): DMA clock
          \arg        RCU_IPA: IPA clock
          \arg        RCU_ENET: ENET clock
          \arg        RCU_ENETTX: ENETTX clock
          \arg        RCU_ENETRX: ENETRX clock
          \arg        RCU_ENETPTP: ENETPTP clock
          \arg        RCU_USBHS: USBHS clock
          \arg        RCU_USBHSULPI: USBHSULPI clock
          \arg        RCU_DCI: DCI clock
          \arg        RCU_TRNG: TRNG clock
          \arg        RCU_USBFS: USBFS clock
          \arg        RCU_EXMC: EXMC clock
          \arg        RCU_TIMERx (x = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13): TIMER clock
          \arg        RCU_WWDGT: WWDGT clock
          \arg        RCU_SPIx (x = 0, 1, 2, 3, 4, 5): SPI clock
          \arg        RCU_USARTx (x = 0, 1, 2, 5): USART clock
          \arg        RCU_UARTx (x = 3, 4, 6, 7): UART clock
          \arg        RCU_I2Cx (x = 0, 1, 2): I2C clock
          \arg        RCU_CANx (x = 0, 1): CAN clock
          \arg        RCU_PMU: PMU clock
          \arg        RCU_DAC: DAC clock
          \arg        RCU_RTC: RTC clock
          \arg        RCU_ADCx (x = 0, 1, 2): ADC clock
          \arg        RCU_SDIO: SDIO clock
          \arg        RCU_SYSCFG: SYSCFG clock
          \arg        RCU_TLI: TLI clock
          \arg        RCU_CTC: CTC clock
          \arg        RCU_IREF: IREF clock
        \param[out] none
        \retval     none
    */
    void rcu_periph_clock_enable(rcu_periph_enum periph)
    {
        RCU_REG_VAL(periph) |= BIT(RCU_BIT_POS(periph));
    }
    
    /* peripheral clock enable */
    typedef enum
    {
        /* AHB1 peripherals */
        RCU_GPIOA     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 0U),                  /*!< GPIOA clock */
        RCU_GPIOB     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 1U),                  /*!< GPIOB clock */
        RCU_GPIOC     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 2U),                  /*!< GPIOC clock */
        RCU_GPIOD     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 3U),                  /*!< GPIOD clock */
        RCU_GPIOE     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 4U),                  /*!< GPIOE clock */
        RCU_GPIOF     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 5U),                  /*!< GPIOF clock */
        RCU_GPIOG     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 6U),                  /*!< GPIOG clock */
        RCU_GPIOH     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 7U),                  /*!< GPIOH clock */
        RCU_GPIOI     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 8U),                  /*!< GPIOI clock */
        RCU_CRC       = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 12U),                 /*!< CRC clock */
        RCU_BKPSRAM   = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 18U),                 /*!< BKPSRAM clock */
        RCU_TCMSRAM   = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 20U),                 /*!< TCMSRAM clock */
        RCU_DMA0      = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 21U),                 /*!< DMA0 clock */
        RCU_DMA1      = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 22U),                 /*!< DMA1 clock */
        RCU_IPA       = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 23U),                 /*!< IPA clock */
        RCU_ENET      = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 25U),                 /*!< ENET clock */
        RCU_ENETTX    = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 26U),                 /*!< ENETTX clock */
        RCU_ENETRX    = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 27U),                 /*!< ENETRX clock */
        RCU_ENETPTP   = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 28U),                 /*!< ENETPTP clock */
        RCU_USBHS     = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 29U),                 /*!< USBHS clock */
        RCU_USBHSULPI = RCU_REGIDX_BIT(AHB1EN_REG_OFFSET, 30U),                 /*!< USBHSULPI clock */
        /* AHB2 peripherals */
        RCU_DCI       = RCU_REGIDX_BIT(AHB2EN_REG_OFFSET, 0U),                  /*!< DCI clock */
        RCU_TRNG      = RCU_REGIDX_BIT(AHB2EN_REG_OFFSET, 6U),                  /*!< TRNG clock */
        RCU_USBFS     = RCU_REGIDX_BIT(AHB2EN_REG_OFFSET, 7U),                  /*!< USBFS clock */
        /* AHB3 peripherals */
        RCU_EXMC      = RCU_REGIDX_BIT(AHB3EN_REG_OFFSET, 0U),                  /*!< EXMC clock */
        /* APB1 peripherals */
        RCU_TIMER1    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 0U),                  /*!< TIMER1 clock */
        RCU_TIMER2    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 1U),                  /*!< TIMER2 clock */
        RCU_TIMER3    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 2U),                  /*!< TIMER3 clock */
        RCU_TIMER4    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 3U),                  /*!< TIMER4 clock */
        RCU_TIMER5    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 4U),                  /*!< TIMER5 clock */
        RCU_TIMER6    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 5U),                  /*!< TIMER6 clock */
        RCU_TIMER11   = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 6U),                  /*!< TIMER11 clock */
        RCU_TIMER12   = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 7U),                  /*!< TIMER12 clock */
        RCU_TIMER13   = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 8U),                  /*!< TIMER13 clock */   
        RCU_WWDGT     = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 11U),                 /*!< WWDGT clock */
        RCU_SPI1      = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 14U),                 /*!< SPI1 clock */
        RCU_SPI2      = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 15U),                 /*!< SPI2 clock */
        RCU_USART1    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 17U),                 /*!< USART1 clock */
        RCU_USART2    = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 18U),                 /*!< USART2 clock */
        RCU_UART3     = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 19U),                 /*!< UART3 clock */
        RCU_UART4     = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 20U),                 /*!< UART4 clock */
        RCU_I2C0      = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 21U),                 /*!< I2C0 clock */
        RCU_I2C1      = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 22U),                 /*!< I2C1 clock */
        RCU_I2C2      = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 23U),                 /*!< I2C2 clock */   
        RCU_CAN0      = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 25U),                 /*!< CAN0 clock */
        RCU_CAN1      = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 26U),                 /*!< CAN1 clock */
        RCU_PMU       = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 28U),                 /*!< PMU clock */
        RCU_DAC       = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 29U),                 /*!< DAC clock */
        RCU_UART6     = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 30U),                 /*!< UART6 clock */
        RCU_UART7     = RCU_REGIDX_BIT(APB1EN_REG_OFFSET, 31U),                 /*!< UART7 clock */
        RCU_RTC       = RCU_REGIDX_BIT(BDCTL_REG_OFFSET, 15U),                  /*!< RTC clock */
        /* APB2 peripherals */
        RCU_TIMER0    = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 0U),                  /*!< TIMER0 clock */
        RCU_TIMER7    = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 1U),                  /*!< TIMER7 clock */
        RCU_USART0    = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 4U),                  /*!< USART0 clock */
        RCU_USART5    = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 5U),                  /*!< USART5 clock */
        RCU_ADC0      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 8U),                  /*!< ADC0 clock */
        RCU_ADC1      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 9U),                  /*!< ADC1 clock */
        RCU_ADC2      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 10U),                 /*!< ADC2 clock */
        RCU_SDIO      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 11U),                 /*!< SDIO clock */
        RCU_SPI0      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 12U),                 /*!< SPI0 clock */
        RCU_SPI3      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 13U),                 /*!< SPI3 clock */
        RCU_SYSCFG    = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 14U),                 /*!< SYSCFG clock */
        RCU_TIMER8    = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 16U),                 /*!< TIMER8 clock */
        RCU_TIMER9    = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 17U),                 /*!< TIMER9 clock */
        RCU_TIMER10   = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 18U),                 /*!< TIMER10 clock */
        RCU_SPI4      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 20U),                 /*!< SPI4 clock */
        RCU_SPI5      = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 21U),                 /*!< SPI5 clock */
        RCU_TLI       = RCU_REGIDX_BIT(APB2EN_REG_OFFSET, 26U),                 /*!< TLI clock */
        /* APB1 additional peripherals */
        RCU_CTC       = RCU_REGIDX_BIT(ADD_APB1EN_REG_OFFSET, 27U),             /*!< CTC clock */
        RCU_IREF      = RCU_REGIDX_BIT(ADD_APB1EN_REG_OFFSET, 31U),             /*!< IREF clock */
    }rcu_periph_enum;
    
    • 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
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    那么毫无疑问 rcu_periph_clock_enable(RCU_GPIOC); 用来初始化GPIO的时钟

    打开gd32f4xx_gpio.h
    在这里插入图片描述
    设置GPIO的工作模式

    /*!
        \brief      set GPIO mode
        \param[in]  gpio_periph: GPIO port
                    only one parameter can be selected which is shown as below:
          \arg        GPIOx(x = A,B,C,D,E,F,G,H,I)
        \param[in]  mode: GPIO pin mode
          \arg        GPIO_MODE_INPUT: input mode
          \arg        GPIO_MODE_OUTPUT: output mode
          \arg        GPIO_MODE_AF: alternate function mode
          \arg        GPIO_MODE_ANALOG: analog mode
        \param[in]  pull_up_down: GPIO pin with pull-up or pull-down resistor
          \arg        GPIO_PUPD_NONE: floating mode, no pull-up and pull-down resistors
          \arg        GPIO_PUPD_PULLUP: with pull-up resistor
          \arg        GPIO_PUPD_PULLDOWN:with pull-down resistor
        \param[in]  pin: GPIO pin
                    one or more parameters can be selected which are shown as below:
          \arg        GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
        \param[out] none
        \retval     none
    */
    void gpio_mode_set(uint32_t gpio_periph, uint32_t mode, uint32_t pull_up_down, uint32_t pin)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    完成后如下

    #include "gd32f4xx.h"
    int main(void)
    {
        rcu_periph_clock_enable(RCU_GPIOC);
    	
    	gpio_mode_set(GPIOC,GPIO_MODE_OUTPUT,GPIO_PUPD_PULLUP,GPIO_PIN_0);
    	
    	gpio_output_options_set(GPIOC,GPIO_OTYPE_PP,GPIO_OSPEED_25MHZ,GPIO_PIN_0);
    	
    	gpio_bit_reset(GPIOC,GPIO_PIN_0);
    }![在这里插入图片描述](https://img-blog.csdnimg.cn/900d197a72b146d2b8ea69ec0844268f.png)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

  • 相关阅读:
    23种设计模式【创建型模式】详细介绍之【建造者模式】
    C++ — 程序、进程、线程
    Java 压缩PDF文档
    Kubernetes上安装Metrics-Server
    .Net6 Api Swagger配置
    基于tensorflow和NasNet的皮肤癌分类项目
    one-hot是什么
    SAP出现库存数量为0金额不为0的调整方法
    【每日一题】CF1690E. Price Maximization | 双指针 | 简单
    51单片机应用开发---定时器(定时1S,LED以1S间隔闪烁)
  • 原文地址:https://blog.csdn.net/denghuajing/article/details/128048115