高中生
最后登录1970-1-1
在线时间 小时
注册时间2023-6-22
|
用HAL库搞好了,但是标准库一直只能PA0的按键触发,PC13的按键无法触发,不知道哪里有问题,求助。
#include "main.h"
int main(void)
{
SysTick_Init(); //初始化延时函数
LED_GPIO_Config(); //LED 端口初始化
EXTI_KEY_Config(); //初始化EXTI中断,按下按键会触发中断
//等待中断,由于使用中断方式,CPU不用轮询按键
while(1)
{
}
}
//配置嵌套向量中断控制器NVIC
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = KEY1_INT_EXTI_IRQ; //配置中断源:按键1
NVIC_InitStructure.NVIC_IRQChannelPriority = 2; //配置优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断通道
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = KEY2_INT_EXTI_IRQ; //配置中断源:按键2,其他使用上面相关配置
NVIC_InitStructure.NVIC_IRQChannelPriority = 1; //配置优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断通道
NVIC_Init(&NVIC_InitStructure);
}
//配置 IO为EXTI中断口,并设置中断优先级
void EXTI_KEY_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_AHBPeriphClockCmd(KEY1_INT_GPIO_CLK,ENABLE);//开启按键端口的时钟
RCC_AHBPeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//开启按键端口的时钟
NVIC_Configuration(); //配置 NVIC 中断
/*--------------------------KEY1配置-----------------------------*/
GPIO_InitStructure.GPIO_Pin = KEY1_INT_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(KEY1_INT_GPIO_PORT,&GPIO_InitStructure);
SYSCFG_EXTILineConfig(KEY1_INT_EXTI_PORTSOURCE,KEY1_INT_EXTI_PINSOURCE);
EXTI_InitStructure.EXTI_Line = KEY1_INT_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //EXTI为中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能中断
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE); //清除中断标志位
/*--------------------------KEY2配置-----------------------------*/
RCC_AHBPeriphClockCmd(KEY2_INT_GPIO_CLK,ENABLE);//开启按键端口的时钟
RCC_AHBPeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//开启按键端口的时钟
GPIO_InitStructure.GPIO_Pin = KEY2_INT_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(KEY2_INT_GPIO_PORT,&GPIO_InitStructure);
SYSCFG_EXTILineConfig(KEY2_INT_EXTI_PORTSOURCE,KEY2_INT_EXTI_PINSOURCE);
EXTI_InitStructure.EXTI_Line = KEY2_INT_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //EXTI为中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能中断
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE); //清除中断标志位
}
#include "stm32f0xx.h"
#include "Delay.h"
//引脚定义
#define KEY1_INT_GPIO_PORT GPIOA
#define KEY1_INT_GPIO_CLK RCC_AHBPeriph_GPIOA
#define KEY1_INT_GPIO_PIN GPIO_Pin_0
#define KEY1_INT_EXTI_PORTSOURCE EXTI_PortSourceGPIOA
#define KEY1_INT_EXTI_PINSOURCE EXTI_PinSource0
#define KEY1_INT_EXTI_LINE EXTI_Line0
#define KEY1_INT_EXTI_IRQ EXTI0_1_IRQn
#define KEY1_IRQHandler EXTI0_1_IRQHandler
#define KEY2_INT_GPIO_PORT GPIOC
#define KEY2_INT_GPIO_CLK RCC_AHBPeriph_GPIOC
#define KEY2_INT_GPIO_PIN GPIO_Pin_13
#define KEY2_INT_EXTI_PORTSOURCE EXTI_PortSourceGPIOC
#define KEY2_INT_EXTI_PINSOURCE EXTI_PinSource13
#define KEY2_INT_EXTI_LINE EXTI_Line13
#define KEY2_INT_EXTI_IRQ EXTI4_15_IRQn
#define KEY2_IRQHandler EXTI4_15_IRQHandler
void EXTI_KEY_Config(void);
void KEY1_IRQHandler(void)
{
if(EXTI_GetITStatus(KEY1_INT_EXTI_LINE) != RESET) //确保是否产生了EXTI Line中断
{
LED1_TOGGLE; //LED1 取反
// LED2_TOGGLE; //LED2 取反
// EXTI_ClearFlag(KEY1_INT_EXTI_LINE); //清除中断标志位
// EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE); //清除中断标志位
}
EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE); //清除中断标志位
}
void KEY2_IRQHandler(void)
{
if(EXTI_GetITStatus(KEY2_INT_EXTI_LINE) != RESET) //确保是否产生了EXTI Line中断
{
LED2_TOGGLE;
LED3_TOGGLE; //LED3 取反
// EXTI_ClearFlag(KEY2_INT_EXTI_LINE); //清除中断标志位
// EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE); //清除中断标志位
}
EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE); //清除中断标志位
}
只触发KEY1,不触发KEY2
|
|