简体   繁体   English

如何在 stm32 中设置中断处理程序?

[英]How to setup the interrupt handler in stm32?


void EXTI4_15_IRQHandler()
{
if(EXTI->PR & EXTI_PR_PR8)
    {
    EXTI->PR |= EXTI_PR_PR8;
    // handle interrupt here
    GPIOA->BSRR |= GPIO_BSRR_BS_10;
    Delay(500);
    GPIOA->BSRR |= GPIO_BSRR_BR_10;
    Delay(500);

    }
}

the interrupt init is initialized in the main.c and the setup is correct.中断初始化在 main.c 中初始化,设置正确。 Is there anything that i am missing in the handler function?处理程序 function 中有什么我遗漏的吗?

You have posted only interrupt handler function, which is not a big help on its own.您只发布了中断处理程序 function,这本身并没有多大帮助。 Basically, your MCU either enters it and executes it entirely or not.基本上,您的 MCU 要么进入它并完全执行它,要么不执行它。 So if it (the interrupt handler) doesn't run, it means, the interrupt is not triggered.因此,如果它(中断处理程序)没有运行,则意味着没有触发中断。 It means, the problem is somewhere else, so I will go over the entire logic of getting EXTI interrupt on STM32, make sure you have all of that done.这意味着,问题出在其他地方,所以我将 go 遍历在 STM32 上获取 EXTI 中断的整个逻辑,确保您已完成所有这些。

In order to get EXTI interrupts working, we need to connect 3 internal peripherals together: GPIO, EXTI and NVIC.为了让 EXTI 中断工作,我们需要将 3 个内部外围设备连接在一起:GPIO、EXTI 和 NVIC。 You haven't indicated a specific microcontroller, but they all (STM32) do it in the same/similar way, so I will use STM32F746 as an example, since I have it here on my table.您没有指定特定的微控制器,但它们都 (STM32) 以相同/相似的方式执行此操作,因此我将以 STM32F746 为例,因为我的桌子上有它。

First, you need to set up EXTI.首先,您需要设置 EXTI。 If you want an interrupt on pin GPIOx8, you need to set bits 8 in the appropriate places in the EXTI registers depending on the event you want to trigger an interrupt.如果你想在引脚 GPIOx8 上中断,你需要根据你想触发中断的事件在 EXTI 寄存器的适当位置设置第 8 位。

Second of all, you need to connect GPIO port to EXTI.其次,您需要将 GPIO 端口连接到 EXTI。 Do you want an interrupt on PA8?你想中断 PA8 吗? Or PB8?还是PB8? Or PC8?还是PC8? EXTI only understands that it's "Pin 8". EXTI 只知道它是“Pin 8”。 It doesn't know if it's PA8 or PB8.不知道是PA8还是PB8。 That you do in SYSCFG peripheral.您在 SYSCFG 外设中执行的操作。 In the register EXTI->EXTICR3 you need to set port for EXTI8.在寄存器EXTI->EXTICR3 ,您需要为 EXTI8 设置端口。 This is where you decide that it's PB8 and not PA8 that triggers the interrupt, or whatever port you have.这是您决定触发中断的是 PB8 而不是 PA8,或者您拥有的任何端口。

Now, upon the GPIO behavior event defined in EXTI, the EXTI will report an EXTI9_5 event to NVIC.现在,根据 EXTI 中定义的 GPIO 行为事件,EXTI 将向 NVIC 报告 EXTI9_5 事件。 For now, NVIC will set pending bit if EXTI interrupt event occurs, but it will not execute the interrupt, because it's not activated.现在,如果 EXTI 中断事件发生,NVIC 将设置挂起位,但它不会执行中断,因为它没有被激活。 So we need to configure NVIC and activate EXTI9_5 interrupt there.所以我们需要配置 NVIC 并在那里激活 EXTI9_5 中断。 At this point your interrupt should work.此时你的中断应该工作。

To recap, the sequence of actions is the following:回顾一下,操作顺序如下:

  1. Configure EXTI with the number of pin, on which you want interrupt.使用要中断的引脚数配置 EXTI。 Number of pin, but not a GPIO port.引脚数,但不是 GPIO 端口。 For you, it will be pin 8.对你来说,它将是引脚 8。
  2. Configure SYSCFG to select GPIO port for that EXTI pin.将 SYSCFG 配置为该 EXTI 引脚的 select GPIO 端口。 Configure pin 8 to be pin PB8 and not PA8 (or whatever port the interrupt is on).将引脚 8 配置为引脚 PB8 而不是 PA8(或中断打开的任何端口)。
  3. Activate the corresponding NVIC interrupt.激活相应的 NVIC 中断。

From this also follows, that you can't have interrupts on PA8 and PB8 at the same time.由此也可以得出,您不能同时在 PA8 和 PB8 上进行中断。

EDIT: having delays in the ISR is highly inadvisable.编辑:在 ISR 中出现延迟是非常不可取的。 Also, make sure your input signal on the interrupt pin is debounced.另外,请确保中断引脚上的输入信号已去抖动。

EDIT2: I assumed it goes without saying that every peripheral you use requires clock to be supplied to it. EDIT2:我认为不言而喻,您使用的每个外围设备都需要向其提供时钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM