首页 文章

用SPI写入外部EEPROM

提问于
浏览
2

我正在尝试写一个单独的设备的EEPROM来配置行为是设备,并使用Arduino Uno控制设备 .

根据this webpage,我的SCK连接到引脚13,我的SDA连接到引脚11 .

我有两个函数, i2c_eeprom_write_bytei2c_eeprom_read_byte ,取自this example .

void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
    Wire.begin(deviceaddress); // MUST INCLUDE, otherwise Wire.endTransmission hangs
    // called once, in setup
    int rdata = data;
    Wire.beginTransmission(deviceaddress);

    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB    
    Wire.write(rdata);

    Wire.endTransmission(false);

}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    delay(10);
    Wire.requestFrom(deviceaddress,1);


    int avail = Wire.available();
    Serial.println(avail);
    if (Wire.available()) rdata = Wire.read();

    // there's a bug here with Wire.available. It's returning 0 (ie, 0 bytes to be read),
    // when it should be returning 1, since I want 1 byte.

    return rdata;
}

我的问题是 Wire.available() 总是返回0,这意味着从设备没有向主设备Arduino发送任何内容 .

如何从从设备读取?

2 回答

  • 0

    看起来你正在使用I2C库写入SPI设备 - 它们不一样!大多数外部EEPROM使用I2C(使用Uno上的引脚4和5) .

  • 2

    在没有示波器或逻辑分析仪的情况下调试硬件将是一项挑战 .

    一个简单的双重检查是确认地址引脚已连线以配置您已编码的地址 . 如果幸运的话,你只需找到错误的地址,修复它,一切都开始工作 .

    小心你的调试方式 . 这个

    delay(10);
    

    你添加的是一个以29 kbit / s(或更快)运行的设备的 huge 时间 . 延迟可能会干扰通信,因为它可以帮助您进行调试 . 您没有提及该设备,但确认它是否能够容忍那么大的暂停 . 写入存储器可能需要一段时间才能执行,但与100 kHz时钟速率相比,读取几乎是即时的 .

    同样,在事务中间添加它

    println(...
    

    可能是问题的一部分 . 如果Serial为默认的9600波特,则传输该字符将中断I2C事务 .

    如果你有一个示波器,我能提供的唯一技巧就是用一个电阻分压器对取代一个上拉电阻,将SDA和SCL拉至逻辑1的电压,但不等于Vcc(在你的情况下是5V?) . 例如,选择一对电阻将线路保持在4.8 V . 通过这种安排,您可以分辨出没有驱动总线的设备和驱动逻辑1的设备之间的区别 .

相关问题