我已经彻底检查了所有段,我无法弄清楚为什么EXTI0_1_IRQHandler不会触发,而EXTI4_15_IRQHandler会触发。请参阅下面我用于库的两个文件。我正在使用SPL和OpenSTM32 IDE。 “main.c”只调用
ENCODER_STM32_configureInterface();
所以真的没有其他事情发生。
// ENCODER_STM32 library: This library shall enable interfacing an encoder on an STM32F0 chip.
// GPIO Definitions
#define ENCODER_GPIO_PORT GPIOA
#define ENCODER_GPIO_CLK_PIN GPIO_Pin_1
#define ENCODER_GPIO_DT_PIN GPIO_Pin_2
#define ENCODER_GPIO_SW_PIN GPIO_Pin_4
#define ENCODER_GPIO_PERIPH RCC_AHBPeriph_GPIOA
// EXTI Definitions
#define ENCODER_EXTI_PORTSRC EXTI_PortSourceGPIOA
#define ENCODER_EXTI_CLK_PINSRC EXTI_PinSource1
#define ENCODER_EXTI_CLK_LINE EXTI_Line1
#define ENCODER_EXTI_DT_PINSRC EXTI_PinSource2
#define ENCODER_EXTI_DT_LINE EXTI_Line2
#define ENCODER_EXTI_SW_PINSRC EXTI_PinSource4
#define ENCODER_EXTI_SW_LINE EXTI_Line4
#define ENCODER_EXTI_PERIPH RCC_APB2Periph_SYSCFG
#define ENCODER_EXTI_MODE EXTI_Mode_Interrupt
#define ENCODER_EXTI_TRIGGER EXTI_Trigger_Falling
// NVIC Definitions
#define ENCODER_NVIC_CLK_CHA EXTI0_1_IRQn
#define ENCODER_NVIC_DT_CHA EXTI2_3_IRQn
#define ENCODER_NVIC_SW_CHA EXTI4_15_IRQn
#define ENCODER_NVIC_PRIORITY 0x03
// Variable Definitions
// Parameter Definitions
// Function Definitions
// Function Declarations
void ENCODER_STM32_configureInterface(void);
和...
#include "stm32f0xx.h"
#include "usart_stm32_console.h"
#include "encoder_stm32.h"
void ENCODER_STM32_configureInterface(void) {
// Enabling Clocks
RCC_AHBPeriphClockCmd(ENCODER_GPIO_PERIPH, ENABLE);
RCC_APB2PeriphClockCmd(ENCODER_EXTI_PERIPH, ENABLE);
// Configuring GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Pin = ENCODER_GPIO_CLK_PIN | ENCODER_GPIO_DT_PIN | ENCODER_GPIO_SW_PIN;
GPIO_Init(ENCODER_GPIO_PORT, &GPIO_InitStructure);
// Configuring EXTI
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = ENCODER_EXTI_CLK_LINE | ENCODER_EXTI_DT_LINE | ENCODER_EXTI_SW_LINE;
EXTI_InitStructure.EXTI_Mode = ENCODER_EXTI_MODE;
EXTI_InitStructure.EXTI_Trigger = ENCODER_EXTI_TRIGGER;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
SYSCFG_EXTILineConfig(ENCODER_EXTI_PORTSRC, ENCODER_EXTI_CLK_PINSRC);
SYSCFG_EXTILineConfig(ENCODER_EXTI_PORTSRC, ENCODER_EXTI_DT_PINSRC);
SYSCFG_EXTILineConfig(ENCODER_EXTI_PORTSRC, ENCODER_EXTI_SW_PINSRC);
EXTI_Init(&EXTI_InitStructure);
// Configuring NVIC
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_CLK_CHA | ENCODER_NVIC_DT_CHA | ENCODER_NVIC_SW_CHA;
NVIC_InitStructure.NVIC_IRQChannelPriority = ENCODER_NVIC_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_1_IRQHandler(void) {
USART_STM32_sendToUSART("IRQ0_1 triggered");
if(EXTI_GetITStatus(ENCODER_EXTI_CLK_LINE) != RESET) {
USART_STM32_sendToUSART("CLK triggered");
EXTI_ClearITPendingBit(ENCODER_EXTI_CLK_LINE);
}
}
void EXTI2_3_IRQHandler(void) {
USART_STM32_sendToUSART("IRQ2_3 triggered");
if(EXTI_GetITStatus(ENCODER_EXTI_DT_LINE) != RESET) {
USART_STM32_sendToUSART("DT triggered");
EXTI_ClearITPendingBit(ENCODER_EXTI_DT_LINE);
}
}
void EXTI4_15_IRQHandler(void) {
USART_STM32_sendToUSART("IRQ4_15 triggered");
if(EXTI_GetITStatus(ENCODER_EXTI_SW_LINE) != RESET) {
USART_STM32_sendToUSART("SW triggered");
EXTI_ClearITPendingBit(ENCODER_EXTI_SW_LINE);
}
}
答案 0 :(得分:4)
NVIC_Init()
一次只能处理一个中断,因为NVIC_InitStructure.NVIC_IRQChannel
不是位掩码,而是一个简单的标识符。
查看标题中IRQ频道的定义:
EXTI0_1_IRQn = 5, /*!< EXTI Line 0 and 1 Interrupts */
EXTI2_3_IRQn = 6, /*!< EXTI Line 2 and 3 Interrupts */
EXTI4_15_IRQn = 7, /*!< EXTI Line 4 to 15 Interrupts */
或者像掩码位一样将它们放在一起毫无意义,但在这种特殊情况下,5 | 6 | 7会产生7,这意外地等于EXTI4中断的数量,这就是为什么这个按钮有效的原因
只需拨打NVIC_Init()
三次:
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_CLK_CHA;
NVIC_InitStructure.NVIC_IRQChannelPriority = ENCODER_NVIC_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_DT_CHA;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = ENCODER_NVIC_SW_CHA;
NVIC_Init(&NVIC_InitStructure);