1

所以,我试图根据按钮的中断来切换 LED。

理想情况下,当按下按钮时,LED 应该切换,即如果它关闭则打开,反之亦然。但是当我执行此代码时,它会切换并返回其原始状态。

预期结果: LED 关闭 » 按钮按下 » LED 开启

实际结果: LED 关闭 » 按钮按下 » LED 开启 » LED 关闭

我已经添加了去抖动的延迟,因此弹跳不在图片中。按下按钮时,GPIO 的 ODR 也会在 ISR 中设置,那么在退出 ISR 时如何清除它?

我将衷心感谢您的帮助!谢谢你。

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/cm3/nvic.h>

#define LEDPIN (GPIO13)

static void exti_setup(void)
{
    /* Enable GPIOA and AFIO clock. */
    rcc_periph_clock_enable(RCC_GPIOB);
    rcc_periph_clock_enable(RCC_AFIO);

    /* Enable EXTI0 interrupt. */
    nvic_enable_irq(NVIC_EXTI15_10_IRQ);

    /* Set GPIO12 (in GPIO port B) to input  */
    gpio_set_mode(GPIOB, GPIO_MODE_INPUT,GPIO_CNF_INPUT_FLOAT, GPIO12);

    /* Configure the EXTI subsystem. */
    exti_select_source(EXTI12,GPIOB);
    exti_set_trigger(EXTI12, EXTI_TRIGGER_BOTH);
    exti_enable_request(EXTI12);
}


static void gpio_setup(void)
{
    /* Enable clock for GPIO port C */
    rcc_periph_clock_enable(RCC_GPIOC);

    /* Set LEDPIN (in GPIO port C) as opendrain output  */
    gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, LEDPIN);
}

void delay(){
    int i;
    for (i = 0; i < 1000000; i++) 
    {
        __asm__("nop");
    } 
}

void handler(){
    delay();
    gpio_toggle(GPIOC, GPIO13);
}

int main(void)
{
    gpio_setup();
    exti_setup();

    while (1) {
    __asm__("nop");
    }
    return 0;
}

void exti15_10_isr()
{
    exti_reset_request(EXTI12);
    handler();
}
4

2 回答 2

2
  1. 不是开漏而是推挽
  2. Butttons should not use EXTI as it makes debouncing more complicated, often floods the uC with interrupts, Use timer interrupt instead to read the key and debounce.
于 2020-05-08T00:27:07.740 回答
0

As @dev_eng rightly pointed out the issue was the interrupt being configured as both RISING/FALLING edge.

Configuring it with single EDGE that is either RISING or FALLING solved my problem.

于 2020-05-08T10:11:14.117 回答