如何解决“因中断而中断的2个微控制器之间的I2C通信”

时间:2019-05-10 11:45:49

标签: arduino interrupt i2c

我正在用Arduino Nano调光器,通过I2C从ESP8266控制器获得0-128之间的值。在nano上运行良好,直到中断(零交叉检测)将其中断为止。

我已经尝试过用电位计进行调光,并且效果很好,我尝试了在没有干扰的情况下(在串行监视器中)进行操作,这确实可以工作。我也尝试用sei()和cli()替换中断和noInterrupts,但没有结果。有时候我确实可以使它工作一会儿,但是看起来像这样。

63
64
65
66
67
-1
69
-1
72
-1
74

此后它停止工作。

下面是我的ESP8266设备代码(这是临时测试代码)。这只会将值发送到纳米。

#include <Wire.h>

void setup() {
  Wire.begin();
}

void loop() {
  for (int dimValue = 0; dimValue <= 128; dimValue++)
  {
    delay(50);
    Wire.beginTransmission(8);
    Wire.write(dimValue);
    Wire.endTransmission();
  }
}

下面的Nano代码负责通过I2C进行调光和接收命令。

#include <Wire.h>

int AC_LOAD = 8;    // Output to Opto Triac pin
int dimming = 128;  // Dimming level (0-128)  0 = ON, 128 = OFF
int zeroCross = 3; // zerocross pin

void setup()
{
  pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  attachInterrupt(digitalPinToInterrupt(3), zero_crosss_int, RISING);
  Serial.begin(115200);
  Wire.begin(8);
  Wire.onReceive(receiveEvent);
}

void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
{
  int dimtime = (75 * dimming);  // For 60Hz =>65
  delayMicroseconds(dimtime);    // Wait till firing the TRIAC
  digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  delayMicroseconds(10);         // triac On propogation delay
  // (for 60Hz use 8.33) Some Triacs need a longer period
  digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

void loop()
{
}

void receiveEvent() {
  noInterrupts();
  int x = Wire.read();
  dimming = x;
  Serial.println(x);
  interrupts();
}

我应该从纳米中得到的结果应该是这样

63
64
65
66
67
68
69
70
71
72
73
74

1 个答案:

答案 0 :(得分:0)

问题是中断处理程序忙了太长时间,直到被中断,I2C接收处理才溢出。现在,将来自中断的代码放入loop()中即可正常工作。

#include <Wire.h>

int AC_LOAD = 9;    // Output to Opto Triac pin
int dimming = 128;  // Dimming level (0-128)  0 = ON, 128 = OFF
int zeroCross = 3; // zerocross pin
boolean triacFire = false;

void setup()
{
  pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  attachInterrupt(digitalPinToInterrupt(zeroCross), zero_crosss_int, RISING);
  Serial.begin(115200);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
}

void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
{
  triacFire = true;
}

void loop()
{
  if (triacFire == true)
  {
    int dimtime = (75 * dimming);  // For 60Hz =>65
    delayMicroseconds(dimtime);    // Wait till firing the TRIAC
    digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
    delayMicroseconds(10);         // triac On propogation delay
    // (for 60Hz use 8.33) Some Triacs need a longer period
    digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
    triacFire = false;
  }
}

void receiveEvent() {
  noInterrupts();
  byte x = Wire.read();    // receive byte as an byte
  dimming = x;
  Serial.println(x);
  interrupts();
}

谢谢大家,帮助我解决了这个问题!