STM32 外部中斷僅在調試模式下響應 (STM32 external interrupt responds only in debug mode)


問題描述

STM32 外部中斷僅在調試模式下響應 (STM32 external interrupt responds only in debug mode)

我的 STM32F103C8T6 微控制器有問題。我正在使用(作為練習)外部中斷來打開/關閉 LED,方法是按下一個外部開關,該開關又連接到 PC13。我正在使用 StdPeriph 庫。

芯片編程後,什麼也沒有發生。相反,當我使用調試器(在 Coocox 中調試)時,芯片工作正常。我不知道問題出在哪裡。

你能幫幫我嗎?這是我的代碼。

#include<stm32f10x.h>
#include<stm32f10x_rcc.h>
#include<stm32f10x_gpio.h>

#include<stm32f10x_exti.h>
#include<misc.h>

typedef enum{
    on,
    off
}state;
state led=on;

int main(void){

    // enable clocks
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

    // uncomment to disable/remap JTAG pins
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE);
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

    // configure PC13 as input
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
    GPIO_Init(GPIOC,&GPIO_InitStructure);

    // configure PB8 as led output
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);

    // connect PC13 to EXTI controller
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);

    // enable and configure EXTI controller
    EXTI_InitTypeDef EXTI_InitStructure;
    EXTI_InitStructure.EXTI_Line=EXTI_Line13;
    EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd=ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    // enable IRQ
    NVIC_EnableIRQ(EXTI15_10_IRQn);
    NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);

    // Configure NVIC
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
    NVIC_Init(&NVIC_InitStructure);

    // switch on led
    GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET);

    while(1);

    return 0;
}

void EXTI15_10_IRQHandler(void){

    // clear pending bit
    if(EXTI_GetITStatus(EXTI_Line13)!=RESET){
        EXTI_ClearITPendingBit(EXTI_Line13);
    }

    if(led==off){
        GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET);
        led=on;
    }else{
        GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_RESET);
        led=off;
    }
}

#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t * file,uint32_t line){
    /* Infinite loop */
    while (1);
}
#endif

參考解法

方法 1:

I had this issue as well. I'm using a STM32F030. The problem for me was not having the SYSCFG clock enabled, which is bit 0 of RCC APB2ENR register. I'm guessing this setting is enabled in debug, so that the software can debug? Otherwise the clock is disabled!

I finally found this by looking into the STM32F1 reference manual, which is slightly more comprehensive.

方法 2:

I don't have knowledge about the mentioned controller, but I worked on STM32L4 series controllers. In STM32L4 push button is connected to the PC13 pin. I observed gpio debouncing when pressing the push button. In your EXTI15_10_IRQHandler() implement debouncing logic. Make sure the interrupt is reaching this function for only once per button press. May be the debugger is slowing down the processor(cpu running in lower frequency compared to free run) and you are getting the interrupts properly.

方法 3:

It is generally a very bad idea to use external interrupts for the buttons and keys. You should use the timer interrupt instead.

You can see simple implementation of the key in the timer interrupt (click, double click, long click events supported) here : https://www.diymat.co.uk/arm‑three‑function‑click‑double‑and‑long‑click‑button‑library‑timer‑interrupt‑driven/

(by jojosthegreatBrooksNithin P0___)

參考文件

  1. STM32 external interrupt responds only in debug mode (CC BY‑SA 2.5/3.0/4.0)

#interrupt #STM32 #microcontroller #C #debugging






相關問題

無法在 java 命令行程序中捕獲中斷 (Unable to catch interrupt in java command line program)

不連續的 BitBlt 捕獲 (discontinuous BitBlt capture)

GPIO 更改狀態時如何更新 sysfs? (How is sysfs updated when a GPIO changes state?)

Linux IRQ 處理程序中的固有競爭條件 (Inherent race condition in Linux IRQ handlers)

STM32 外部中斷僅在調試模式下響應 (STM32 external interrupt responds only in debug mode)

Java中的線程 (Threading in Java)

如何在被 Python 殺死之前運行最後一個函數? (How to run one last function before getting killed in Python?)

在 C++ 中處理中斷 (Handling Interrupt in C++)

應用程序中斷瘋狂 (Application interrupts like crazy)

SetPriorityClass(REALTIME_PRIORITY_CLASS) 實際上做了什麼? (What does SetPriorityClass(REALTIME_PRIORITY_CLASS) actually do?)

如何找出軟件掛起的原因?(qemu + zephyr + tfm 的問題) (How to find out the cause of hanging software? (problem with qemu + zephyr + tfm))

將參數傳遞給系統調用 (Passing arguments to system calls)







留言討論