功能要求
将两个按键分别与引脚PA0、PA1相连接,通过按键按下,能够触发中断响应程序(不需明确功能)。
代码流程如下:
实现代码
#include "stm32f10x.h" // Device header
int main()
{
//开启时钟:GPIO、AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//配置GPIO初始化
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode= GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//设置EXTI引脚映射
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
//EXTI0通道初始化
EXTI_InitTypeDef EXTI_InitStruct0;
EXTI_InitStruct0.EXTI_Line=EXTI_Line0;
EXTI_InitStruct0.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct0.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStruct0.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStruct0);
//EXTI1通道初始化
EXTI_InitTypeDef EXTI_InitStruct1;
EXTI_InitStruct1.EXTI_Line=EXTI_Line1;
EXTI_InitStruct1.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct1.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStruct1.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStruct1);
//NVIC配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//EXTI0
NVIC_InitTypeDef NVIC_InitStruct0;
NVIC_InitStruct0.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStruct0.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStruct0.NVIC_IRQChannelSubPriority=0;
NVIC_InitStruct0.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct0);
//EXTI1
NVIC_InitTypeDef NVIC_InitStruct1;
NVIC_InitStruct1.NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStruct1.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStruct1.NVIC_IRQChannelSubPriority=0;
NVIC_InitStruct1.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct1);
while(1)
{
}
}
void EXTI0_IRQHandler()
{
EXTI_ClearITPendingBit(EXTI_Line0);//清除标志位
//需要执行的功能
}
void EXTI1_IRQHandler()
{
EXTI_ClearITPendingBit(EXTI_Line1);//清除标志位
//需要执行的功能
}