开发板提供了一个用户按键:
按键KEY1相关的宏定义:
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Key1 push-button
*/
#define KEY1_BUTTON_PIN GPIO_PIN_1
#define KEY1_BUTTON_GPIO_PORT GPIOA
#define KEY1_BUTTON_GPIO_CLK RCM_APB2_PERIPH_GPIOA
#define KEY1_BUTTON_EINT_LINE EINT_LINE_1
#define KEY1_BUTTON_EINT_PORT_SOURCE GPIO_PORT_SOURCE_A
#define KEY1_BUTTON_EINT_PIN_SOURCE GPIO_PIN_SOURCE_1
#define KEY1_BUTTON_EINT_IRQ_NUM EINT1_IRQn
配置为外部中断模式:
/*!
* @brief Board button configuration
*
* @param button: Specifies the button to be configured
* This parameter can be one of following parameters:
* [url=home.php?mod=space&uid=2817080]@ARG[/url] BUTTON_KEY1: Key1 Push Button
* @arg BUTTON_KEY2: Key2 Push Button
*
* @param Button_Mode: Specifies Button mode.
* This parameter can be one of following parameters:
* @arg BUTTON_MODE_GPIO: Button will be used as simple IO
* @arg BUTTON_MODE_EINT: Button will be connected to EINT line
* with interrupt generation capability
*
* @retval None
*/
void BOARD_BUTTON_Config(BOARD_BUTTON_T button, BOARD_BUTTON_MODE_T mode)
{
GPIO_Config_T GPIO_ConfigStruct = {0U};
EINT_Config_T EINT_ConfigStruct = {0U};
/* Enable the BUTTON Clock */
RCM_EnableAPB2PeriphClock(BUTTON_CLK[button] | RCM_APB2_PERIPH_AFIO);
/* Configure Button pin as input floating */
GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
GPIO_ConfigStruct.pin = BUTTON_PIN[button];
GPIO_Config(BUTTON_PORT[button], &GPIO_ConfigStruct);
if (mode == BUTTON_MODE_EINT)
{
/* Connect Button EINT Line to Button GPIO Pin */
GPIO_ConfigEINTLine(BUTTON_PORT_SOURCE[button], BUTTON_PIN_SOURCE[button]);
/* Configure Button EINT line */
EINT_ConfigStruct.line = BUTTON_EINT_LINE[button];
EINT_ConfigStruct.mode = EINT_MODE_INTERRUPT;
EINT_ConfigStruct.trigger = EINT_TRIGGER_FALLING;
EINT_ConfigStruct.lineCmd = ENABLE;
EINT_Config(&EINT_ConfigStruct);
/* Enable and set Button EINT Interrupt to the lowest priority */
NVIC_EnableIRQRequest(BUTTON_IRQn[button], 0x0F, 0x0F);
}
}
调用:BOARD_BUTTON_Config(BUTTON_KEY1, BUTTON_MODE_EINT);
中断处理函数:
/*!
* @brief This function handles External lines 1 Handler
*
* @param None
*
* @retval None
*
*/
void EINT1_IRQHandler(void)
{
if (EINT_ReadIntFlag(EINT_LINE_1))
{
BOARD_LED_Toggle(LED3);
EINT_ClearIntFlag(EINT_LINE_1);
}
}
烧录验证:
|