极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9329|回复: 2

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

[复制链接]
发表于 2014-3-16 21:43:03 | 显示全部楼层 |阅读模式
我直接用了example中的master_writer.ino和slave_receiver.ino这两个源代码例程,在Proteus中分别用两个Uno做I2C仿真,一个做主机循环发送,另外一个做从机接收,发送和接收分别通过串口输出仿真,代码基本没有变。



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

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2014-3-16 21:45:48 | 显示全部楼层
  1. #include <Wire.h>

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

  7. byte x = 3;

  8. void loop()
  9. {
  10.   Wire.beginTransmission(4); // transmit to device #4
  11.   Wire.write("x is ");        // sends five bytes
  12.   Serial.print("x is ");
  13.   Wire.write(x);              // sends one byte
  14.   Serial.println(x);  
  15.   Wire.flush();
  16.   Wire.endTransmission();    // stop transmitting  
  17.   x++;
  18.   delay(1500);
  19. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-16 21:46:20 | 显示全部楼层
  1. #include <Wire.h>

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

  8. void loop()
  9. {
  10.   delay(100);
  11. }

  12. // function that executes whenever data is received from master
  13. // this function is registered as an event, see setup()
  14. void receiveEvent(int howMany)
  15. {
  16.   while(1 < Wire.available()) // loop through all but the last
  17.   {
  18.     char c = Wire.read(); // receive byte as a character
  19.     Serial.print(c);         // print the character
  20.   }
  21.   int x = Wire.read();    // receive byte as an integer
  22.   Serial.println(x);         // print the integer
  23. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 06:20 , Processed in 0.062285 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表