写入I2C后,Arduino停止响应

时间:2017-06-06 09:55:07

标签: c++ arduino

我目前正在尝试通过I2C写入EEPROM。阅读很顺利,我获得了极好的吞吐量。但是当我尝试写入设备时,Arduino停止响应,我必须重置它才能让它再次工作。

I2C写入也不会出现在我的I2C调试器中。

I2c Debug trace

void i2cWrite(unsigned char device, unsigned char memory, const char *wrBuf, unsigned short len) {
  int i = 0;
  ushort bytesWritten = 0;
  ushort _memstart = memory;
  ushort blockSize = 4;
  #ifdef DEBUG_MSGS
  char serialBuf[255] = { '\0'};
  Serial.print("Writing To i2c: ");
  sprintf(serialBuf, "%p", wrBuf);
  Serial.println(serialBuf);
  #endif //DEBUG_MSGS
  while (bytesWritten < len) {
    Wire.beginTransmission((int)device);
    Wire.write((unsigned char)_memstart);
    for (int j = 0; i < blockSize; j++) {
      Wire.write(wrBuf[bytesWritten + j]);
    }
    Wire.endTransmission();
    bytesWritten += blockSize;
    _memstart += blockSize;
    delay(25);
  }
  #ifdef DEBUG_MSGS
  Serial.println("\mDone writing.");
  #endif //DEBUG_MSGS
}

我不确定我做错了什么。我通过串行连接获得以下输出:

  

收到的写请求:Andy

     

写入i2c:0xa800fd98

“写入i2c”总是给出相同的值,并且它似乎总是直接崩溃。

1 个答案:

答案 0 :(得分:0)

错误似乎位于循环中,因为输出是

Write Request Received: Andy
Writing To i2c: 0xa800fd98

I'm working here
I wrote the memory adress
I wrote a byte of data
I wrote a byte of data
I wrote a byte of data
I wrote a byte of data
I wrote a byte of data
....

这似乎无限期地继续存在。

添加了一些调试语句并更改了点之后一些程序员老兄注意到了

{
    Wire.beginTransmission((int)device);
    Serial.println("I'm working here");
    Wire.write((unsigned char)_memstart);
    Serial.println("I wrote the memory adress");
    for (int j = 0; j < blockSize; j++) {
        Wire.write(wrBuf[bytesWritten + j]);
        Serial.println("I wrote a byte of data");
        //Serial.write(wrBuf[bytesWritten + j]);
    }
    Wire.endTransmission();
    Serial.println("I ended the transmission");
    bytesWritten += blockSize;
    _memstart += blockSize;
    delay(25);
}

我注意到我正在检查我&lt; blocksize(从阅读部分复制),现在我遇到了一些其他(小)问题,但这解决了我遇到的问题。