[APM32F4] 【APM32F402R Micro-EVB】-4-外部中断配置

[复制链接]
 楼主| 南来之风 发表于 2025-7-27 10:50 | 显示全部楼层 |阅读模式
开发板提供了一个用户按键:
86261688592d8da21c.png

按键KEY1相关的宏定义:
  1. /**
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] Key1 push-button
  3. */
  4. #define KEY1_BUTTON_PIN                 GPIO_PIN_1
  5. #define KEY1_BUTTON_GPIO_PORT           GPIOA
  6. #define KEY1_BUTTON_GPIO_CLK            RCM_APB2_PERIPH_GPIOA
  7. #define KEY1_BUTTON_EINT_LINE           EINT_LINE_1
  8. #define KEY1_BUTTON_EINT_PORT_SOURCE    GPIO_PORT_SOURCE_A
  9. #define KEY1_BUTTON_EINT_PIN_SOURCE     GPIO_PIN_SOURCE_1
  10. #define KEY1_BUTTON_EINT_IRQ_NUM        EINT1_IRQn


配置为外部中断模式:
  1. /*!
  2. * @brief   Board button configuration
  3. *
  4. * @param   button: Specifies the button to be configured
  5. *              This parameter can be one of following parameters:
  6. *              [url=home.php?mod=space&uid=2817080]@ARG[/url] BUTTON_KEY1: Key1 Push Button
  7. *              @arg BUTTON_KEY2: Key2 Push Button
  8. *
  9. * @param       Button_Mode: Specifies Button mode.
  10. *              This parameter can be one of following parameters:
  11. *              @arg BUTTON_MODE_GPIO: Button will be used as simple IO
  12. *              @arg BUTTON_MODE_EINT: Button will be connected to EINT line
  13. *                   with interrupt generation capability
  14. *
  15. * @retval  None
  16. */
  17. void BOARD_BUTTON_Config(BOARD_BUTTON_T button, BOARD_BUTTON_MODE_T mode)
  18. {
  19.     GPIO_Config_T GPIO_ConfigStruct = {0U};
  20.     EINT_Config_T EINT_ConfigStruct = {0U};
  21.    
  22.     /* Enable the BUTTON Clock */
  23.     RCM_EnableAPB2PeriphClock(BUTTON_CLK[button] | RCM_APB2_PERIPH_AFIO);
  24.    
  25.     /* Configure Button pin as input floating */
  26.     GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
  27.     GPIO_ConfigStruct.pin = BUTTON_PIN[button];
  28.     GPIO_Config(BUTTON_PORT[button], &GPIO_ConfigStruct);
  29.    
  30.     if (mode == BUTTON_MODE_EINT)
  31.     {
  32.         /* Connect Button EINT Line to Button GPIO Pin */
  33.         GPIO_ConfigEINTLine(BUTTON_PORT_SOURCE[button], BUTTON_PIN_SOURCE[button]);

  34.         /* Configure Button EINT line */
  35.         EINT_ConfigStruct.line = BUTTON_EINT_LINE[button];
  36.         EINT_ConfigStruct.mode = EINT_MODE_INTERRUPT;
  37.         EINT_ConfigStruct.trigger = EINT_TRIGGER_FALLING;
  38.         EINT_ConfigStruct.lineCmd = ENABLE;
  39.         EINT_Config(&EINT_ConfigStruct);

  40.         /* Enable and set Button EINT Interrupt to the lowest priority */
  41.         NVIC_EnableIRQRequest(BUTTON_IRQn[button], 0x0F, 0x0F);
  42.     }
  43. }
调用:BOARD_BUTTON_Config(BUTTON_KEY1, BUTTON_MODE_EINT);

中断处理函数:
  1. /*!
  2. * @brief   This function handles External lines 1 Handler
  3. *
  4. * @param   None
  5. *
  6. * @retval  None
  7. *
  8. */
  9. void EINT1_IRQHandler(void)
  10. {
  11.     if (EINT_ReadIntFlag(EINT_LINE_1))
  12.     {
  13.         BOARD_LED_Toggle(LED3);
  14.         EINT_ClearIntFlag(EINT_LINE_1);
  15.     }
  16. }
烧录验证:

IMG_7408.GIF
您需要登录后才可以回帖 登录 | 注册

本版积分规则

69

主题

290

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部