极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 21415|回复: 11

关于iic多机通讯

[复制链接]
发表于 2014-5-16 10:04:42 | 显示全部楼层 |阅读模式
一个主机通过iic协议控制两个从机上与d13引脚相连的led灯的亮灭  主从机程序该如何编啊 求大神啊  
回复

使用道具 举报

发表于 2014-5-16 10:27:43 | 显示全部楼层
arduino的IIC通讯,可以参考官方的例子。
http://arduino.cc/en/Tutorial/MasterWriter
回复 支持 反对

使用道具 举报

发表于 2014-5-16 10:37:29 | 显示全部楼层
  1. Master Writer Code - Program for Arduino 1
  2. // Wire Master Writer
  3. // by Nicholas Zambetti <http://www.zambetti.com>

  4. // Demonstrates use of the Wire library
  5. // Writes data to an I2C/TWI slave device
  6. // Refer to the "Wire Slave Receiver" example for use with this

  7. // Created 29 March 2006

  8. // This example code is in the public domain.


  9. #include <Wire.h>

  10. void setup()
  11. {
  12.   Wire.begin(); // join i2c bus (address optional for master)
  13. }

  14. byte x = 0;

  15. void loop()
  16. {
  17.   Wire.beginTransmission(4); // transmit to device #4
  18.   Wire.write("x is ");        // sends five bytes
  19.   Wire.write(x);              // sends one byte  
  20.   Wire.endTransmission();    // stop transmitting

  21.   x++;
  22.   delay(500);
  23. }
  24. [Get Code]
  25. Slave Receiver Code - Program for Arduino 2
  26. // Wire Slave Receiver
  27. // by Nicholas Zambetti <http://www.zambetti.com>

  28. // Demonstrates use of the Wire library
  29. // Receives data as an I2C/TWI slave device
  30. // Refer to the "Wire Master Writer" example for use with this

  31. // Created 29 March 2006

  32. // This example code is in the public domain.


  33. #include <Wire.h>

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

  40. void loop()
  41. {
  42.   delay(100);
  43. }

  44. // function that executes whenever data is received from master
  45. // this function is registered as an event, see setup()
  46. void receiveEvent(int howMany)
  47. {
  48.   while(1 < Wire.available()) // loop through all but the last
  49.   {
  50.     char c = Wire.read(); // receive byte as a character
  51.     Serial.print(c);         // print the character
  52.   }
  53.   int x = Wire.read();    // receive byte as an integer
  54.   Serial.println(x);         // print the integer
  55. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2014-5-16 10:58:51 | 显示全部楼层
本帖最后由 lty365 于 2014-5-16 11:00 编辑
LINK~ 发表于 2014-5-16 10:37


编译出错啊!!!!!


sketch_may17a:7: error: expected constructor, destructor, or type conversion before 'for'
sketch_may17a:21: error: expected unqualified-id before '[' token
sketch_may17a.ino: In function 'void loop()':
sketch_may17a:44: error: redefinition of 'void loop()'
sketch_may17a:11: error: 'void loop()' previously defined here
回复 支持 反对

使用道具 举报

发表于 2014-5-16 12:59:29 | 显示全部楼层
lty365 发表于 2014-5-16 10:58
编译出错啊!!!!!

上面分别是发送和接收的程序,你一起copy了去编译了吧?
你得看注释啊
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-16 13:27:27 | 显示全部楼层
i7456 发表于 2014-5-16 12:59
上面分别是发送和接收的程序,你一起copy了去编译了吧?
你得看注释啊

你好。。在吗 能请教问题吗。。我学机械的 但是毕业设计选的却是这个电子的 一窍不通啊。。能不能帮我遍下程序啊。。。
回复 支持 反对

使用道具 举报

发表于 2014-5-16 13:37:28 | 显示全部楼层
i2c主机编程没玩过,多从机用51调试成功。

i2c挂了两个从机,1602LCD和DS3231

回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-16 13:51:58 | 显示全部楼层
i7456 发表于 2014-5-16 10:27
arduino的IIC通讯,可以参考官方的例子。
http://arduino.cc/en/Tutorial/MasterWriter

就是把官方的例子里从机加一个led(d13)用主机控制从机的led的亮灭
回复 支持 反对

使用道具 举报

发表于 2014-5-16 13:59:06 | 显示全部楼层
大海 发表于 2014-5-16 13:51
就是把官方的例子里从机加一个led(d13)用主机控制从机的led的亮灭

我也学机械的,大学毕业设计搞的PLC编程。

毕业设计还是自己学点东西吧。arduino的东西本来就不难。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-16 14:08:08 | 显示全部楼层
i7456 发表于 2014-5-16 13:59
我也学机械的,大学毕业设计搞的PLC编程。

毕业设计还是自己学点东西吧。arduino的东西本来就不难。

大神帮忙编一下吧,我看着编好的程序学习下。。真的好难上手啊
回复 支持 反对

使用道具 举报

发表于 2014-5-16 17:04:14 | 显示全部楼层
大海 发表于 2014-5-16 14:08
大神帮忙编一下吧,我看着编好的程序学习下。。真的好难上手啊

花点时间看看,其实很简单的.
回复 支持 反对

使用道具 举报

发表于 2014-5-16 17:29:16 | 显示全部楼层
其实 我是来看 一个脚 接很多传感器 怎么写的....
没有....
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 16:58 , Processed in 0.081880 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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