EyeRobot 发表于 2014-3-16 21:43:03

关于I2C的Proteus仿真错误,求指教

我直接用了example中的master_writer.ino和slave_receiver.ino这两个源代码例程,在Proteus中分别用两个Uno做I2C仿真,一个做主机循环发送,另外一个做从机接收,发送和接收分别通过串口输出仿真,代码基本没有变。



但是仿真的过程,出现两个问题
(1)第一次发送,从机能正常接收,然后就不能接收到任何信息了。
(2)通过I2C Debugger检测输出的主机发出的信号,第一次和第二次均能正确发送,但是第三次发送的信息感觉有问题,而主机通过串口显示发送的信息又没有错误。

不知道错误出在什么地方,请各位大神指教!!!谢谢!
附件为程序和仿真图

EyeRobot 发表于 2014-3-16 21:45:48

#include <Wire.h>

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600);
}

byte x = 3;

void loop()
{
Wire.beginTransmission(4); // transmit to device #4
Wire.write("x is ");      // sends five bytes
Serial.print("x is ");
Wire.write(x);            // sends one byte
Serial.println(x);
Wire.flush();
Wire.endTransmission();    // stop transmitting
x++;
delay(1500);
}

EyeRobot 发表于 2014-3-16 21:46:20

#include <Wire.h>

void setup()
{
Wire.begin(4);                // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);         // start serial for output
}

void loop()
{
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
}
int x = Wire.read();    // receive byte as an integer
Serial.println(x);         // print the integer
}
页: [1]
查看完整版本: 关于I2C的Proteus仿真错误,求指教