本帖最后由 mftang2016 于 2022-12-18 18:10 编辑
源代码和开发文档地址:
https://gitee.com/mftang/n32-g45-xvl-stb.git
6 GPIO
6.1 需求配置GPIO 控制UG_N32G45XVL-STB开发板 上的3个LED指示灯
6.2 硬件分析UG_N32G45XVL-STB开发板 上的3个LED和MCU 对应IO如下, 当IO为高电平时,点亮LED
IO特性分析
6.3 软件实现6.3.1 IO 参数配置接口- // io 初始化接口
- void gpio_Init(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIO_InitType GPIO_InitStructure;
- /* Check the parameters */
- assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
- /* Enable the GPIO Clock */
- if (GPIOx == GPIOA)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
- }
- else if (GPIOx == GPIOB)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
- }
- else if (GPIOx == GPIOC)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
- }
- else if (GPIOx == GPIOD)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
- }
- else if (GPIOx == GPIOE)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
- }
- else if (GPIOx == GPIOF)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);
- }
- else
- {
- if (GPIOx == GPIOG)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOG, ENABLE);
- }
- }
- /* 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);
- }
- }
复制代码
6.3.2 LED控制接口- /**
- * @brief Turns selected Led on.
- * @param GPIOx x can be A to G to select the GPIO port.
- * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
- */
- void LedOn(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIOx->PBSC = Pin;
- }
- /**
- * @brief Turns selected Led Off.
- * @param GPIOx x can be A to G to select the GPIO port.
- * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
- */
- void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIOx->PBC = Pin;
- }
- /**
- * @brief Turns selected Led on or off.
- * @param GPIOx x can be A to G to select the GPIO port.
- * @param Pin This parameter can be one of the following values:
- * @arg GPIO_PIN_0~GPIO_PIN_15: set related pin on
- * @arg (GPIO_PIN_0<<16)~(GPIO_PIN_15<<16): clear related pin off
- */
- void LedOnOff(GPIO_Module* GPIOx, uint32_t Pin)
- {
- GPIOx->PBSC = Pin;
- }
- /**
- * @brief Toggles the selected Led.
- * @param GPIOx x can be A to G to select the GPIO port.
- * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
- */
- void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIOx->POD ^= Pin;
- }
复制代码 6.3.3 测试代码初始化代码 - /*******************************************************************************
- Board LED IO
- *******************************************************************************/
- #define GPIO_LED1 GPIOA
- //#define GPIO_LED2 GPIOB
- #define GPIO_LED3 GPIOB
- #define PIN_LED1 GPIO_PIN_8
- //#define PIN_LED2 GPIO_PIN_4
- #define PIN_LED3 GPIO_PIN_5
- #define RCC_LED1_GPIO_CLK RCC_APB2_PERIPH_GPIOA
- #define RCC_LED3_GPIO_CLK RCC_APB2_PERIPH_GPIOB
- static void Board_GPIO_Configuration(void)
- {
- /* initial LED gpio port */
- gpio_Init( GPIO_LED1, PIN_LED1);
- gpio_Init( GPIO_LED3, PIN_LED3);
-
- // all led on
- LedOn( GPIO_LED1, PIN_LED1);
- LedOn( GPIO_LED3, PIN_LED3);
- }
复制代码在定时器中实现LED控制 ,1 hz 闪烁频率 - static int count;
- void TIM1_UP_IRQHandler(void)
- {
- if (TIM_GetIntStatus(TIM1, TIM_INT_UPDATE) != RESET)
- {
- TIM_ClrIntPendingBit(TIM1, TIM_INT_UPDATE);
- if( (count % 1000 ) == 0 )
- {
- LedBlink(GPIO_LED3, PIN_LED3);
- }
- count ++;
- }
- }
复制代码
|