在这个示例中,我们使用GPIOA的Pin 0作为输入引脚,并在下降沿触发时生成中断。在中断处理程序中,您可以执行任何您希望在按下按钮时发生的操作。请根据您的具体要求在EXTI0_IRQHandler函数中编写代码。
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
#define BUTTON_PIN GPIO_Pin_0
#define BUTTON_PORT GPIOA
#define BUTTON_EXTI_LINE EXTI_Line0
#define BUTTON_IRQn EXTI0_IRQn
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(BUTTON_EXTI_LINE) != RESET)
{
// 处理中断事件
// ...
EXTI_ClearITPendingBit(BUTTON_EXTI_LINE);
}
}
void init_GPIO()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUTTON_PORT, &GPIO_InitStructure);
}
void init_EXTI()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = BUTTON_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // 下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = BUTTON_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
init_GPIO();
init_EXTI();
while (1)
{
// 主循环中的其他操作
// ...
}
}