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;        
}
4

1 回答 1

2

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

void buttonPressed()      
{
  if(activate) {  
    EX_INT = 1;   
  }     
}
于 2019-10-29T12:24:59.763 回答