小编lil*_*ess的帖子

c arduino 按钮中断 (isr),使多个 LED 亮起和熄灭

我一直在尝试通过按下相应的按钮来让我的 Arduino 上的 LED 亮起和熄灭。

我正在使用中断来实现它并且按钮按下确实被注册,但由于某种原因它没有改变全局变量的值(int button_pressed1,...);

应该发生的是,当我按下按钮 1 时,LED 1 应该亮起和熄灭,与按钮 2 和按钮 3 相同。

我真的很感谢你看一看,中断对我来说很新,所以这可能是一个小问题。<3

*我省略了按钮 2 和 3 的代码。如果我能让 LED 点亮按钮 1,我就能让它们点亮其他按钮。

#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "usart.h"

#define LED_DDR DDRB
#define LED_PORT PORTB

#define BUTTON_DDR DDRC
#define BUTTON_PORT PORTC
#define BUTTON_PIN PINC

int button_pressed1 = 0; //globale variabele to turn on functions

ISR(PCINT1_vect)
{
    if (bit_is_clear(BUTTON_PIN, PC1))
    {                
        _delay_us(500); //debounce
        if (bit_is_clear(BUTTON_PIN, PC1))
        {

            button_pressed1 = 1;
            printf("button 1 pressed\n");
        }
    }
}

int …
Run Code Online (Sandbox Code Playgroud)

c atmega arduino interrupt led

0
推荐指数
1
解决办法
102
查看次数

标签 统计

arduino ×1

atmega ×1

c ×1

interrupt ×1

led ×1