我无法在Arduino Uno中清除外部中断

问题描述 投票:0回答:1

我正在使用arduino uno板进行项目,我正在使用与交换机绑定的外部中断我希望仅当我向开发板发送激活订单时此开关才能工作问题是,如果在我发送命令之前按下了开关,则即使我没有按下该开关,我也会在发送命令后进入按下状态,这意味着外部中断会在我之前保存该状态并在我启用后检索它它这是代码的片段

volatile boolean  EX_INT = 0, activate = 0;
const byte interruptBin = 3;
const byte ACTIVATE = 0x55;
unsigned char frame[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36};

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptBin, INPUT_PULLUP);
}

void loop() {
  if(activate == 1){
    //EIMSK =0;  EIFR = 0; I tried to clear the last interrupt but with no effect
    activate = 0; EX_INT = 0;
    attachInterrupt(digitalPinToInterrupt(interruptBin),buttonPressed,RISING);  
    while(EX_INT != 1);
    EX_INT = 0; 
    Serial.write((uint8_t*)frame, sizeof(frame)); 
    detachInterrupt(digitalPinToInterrupt(interruptBin)); 
  }
}

void serialEvent(){
  while (Serial.available()){
    value = Serial.read();
    if(value == ACTIVATE)
      activate = 1;
  }
}

void buttonPressed()      
{  
  EX_INT = 1;        
}
arduino interrupt interrupt-handling atmega32
1个回答
1
投票

您可以在中断处理程序中检查activate

void buttonPressed()      
{
  if(activate) {  
    EX_INT = 1;   
  }     
}
© www.soinside.com 2019 - 2024. All rights reserved.