c - c arduino 按钮中断(isr),使多个 LED 点亮和熄灭
问题描述
我一直在尝试通过按下相应的按钮来让我的 Arduino 上的 LED 指示灯亮起和熄灭。
我正在使用中断来实现它并且按钮按下确实被注册,但由于某种原因它不会改变全局变量的值(int button_pressed1,...);
应该发生的是,当我按下按钮 1 时,Led 1 应该会亮起和熄灭,与按钮 2 和按钮 3 相同。
我真的很感谢你看一下,中断对我来说很新,所以它可能是一个小的忽视。<3
*我省略了按钮 2 和 3 的代码。如果我可以让按钮 1 上的 LED 亮起,我就可以让其他按钮亮起。
#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 main()
{
LED_DDR |= _BV(PB2) | _BV(PB3) | _BV(PB4); //registrer pins output(bit = 1)
LED_PORT |= _BV(PB2) | _BV(PB3) | _BV(PB4);
BUTTON_DDR &= ~_BV(PC1) & ~_BV(PC2) & ~_BV(PC3); //registrer inputs(bit = 0)
BUTTON_PORT |= _BV(PC1) | _BV(PC2) | _BV(PC3); // pull up ( bit =1 )
PCICR |= _BV(PCIE1); //type pins doorgeven
PCMSK1 |= _BV(PC1) | _BV(PC2) | _BV(PC3); //pin/button doorgeven aan change mask
initUSART();
sei();
while (1)
{ //infinte loop
if (button_pressed1 == 1)
{
LED_PORT &= ~_BV(PB2); //turn led on
_delay_ms(500);
LED_PORT |= _BV(PB2); //turn led off
_delay_ms(500);
}
}
return 0;
}
解决方案
几个基本问题:
- 与 ISR 共享的所有变量都应声明
volatile
并具有针对竞争条件的保护。有关详细信息,请参阅此。 - 您不应该在 ISR 中出现忙延迟。相反,您应该设置定时器中断以在特定时间段内再次触发。通常,很难将 GPIO 中断专门用于按钮,最好从循环定时器中断轮询。您可以使用中断,但它相当复杂,详情请点击此处。
- 500us 可能不足以进行去弹跳。机械信号反弹相对较慢。等待约 10 毫秒左右更为常见。您可以很容易地用示波器测量反弹特性,方法是向按钮添加电源,然后按下它并捕获边缘。它看起来像一个正弦曲线,您可以轻松测量它会持续多长时间。
- 主循环中的 500 毫秒延迟将被用户体验为“滞后”。他们可能会开始怀疑按钮坏了。您可能希望至少跳过关闭延迟。
推荐阅读
- javascript - 这个javascript代码有效吗?它有效率吗?试图创建一个 javascript 用户 ID 和一个会话 ID。不确定我是否有有效的代码
- nlp - 在不平衡引文数据集上使用 LSTM 进行情感分析
- julia - 如何从我的模块中的子模块导出相同功能的多个方法?
- android-studio - 如何在 Android Studio 中使用 Ucanaccess 库?
- javascript - 如何在 html 中使用 jQuery 正确显示下拉菜单
- security - 站点范围的身份验证方法?
- javascript - 第一个技巧,就是errorHandler
- java - 如何在 Java 中不断提示正确的用户输入?
- python-3.x - 我应该将计数放在这个 tim 排序算法中的哪个位置,以便准确地将运行时间与其他算法进行比较
- tensorflow - 遇到 ModuleNotFoundError:当我使用 Tensorflow GPU 处理时,没有名为“tensorflow.contrib”的模块