大海 发表于 2014-5-16 10:04:42

关于iic多机通讯

一个主机通过iic协议控制两个从机上与d13引脚相连的led灯的亮灭主从机程序该如何编啊 求大神啊

i7456 发表于 2014-5-16 10:27:43

arduino的IIC通讯,可以参考官方的例子。
http://arduino.cc/en/Tutorial/MasterWriter

LINK~ 发表于 2014-5-16 10:37:29

Master Writer Code - Program for Arduino 1
// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>

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

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

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

byte x = 0;

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

x++;
delay(500);
}

Slave Receiver Code - Program for Arduino 2
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

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

// Created 29 March 2006

// This example code is in the public domain.


#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
}

lty365 发表于 2014-5-16 10:58:51

本帖最后由 lty365 于 2014-5-16 11:00 编辑

LINK~ 发表于 2014-5-16 10:37 static/image/common/back.gif


编译出错啊!!!!!


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

i7456 发表于 2014-5-16 12:59:29

lty365 发表于 2014-5-16 10:58 static/image/common/back.gif
编译出错啊!!!!!




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

大海 发表于 2014-5-16 13:27:27

i7456 发表于 2014-5-16 12:59 static/image/common/back.gif
上面分别是发送和接收的程序,你一起copy了去编译了吧?
你得看注释啊

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

caiwenping 发表于 2014-5-16 13:37:28

i2c主机编程没玩过,多从机用51调试成功。

i2c挂了两个从机,1602LCD和DS3231
http://player.youku.com/player.php/sid/XNjg2Mzg5OTgw/v.swf

大海 发表于 2014-5-16 13:51:58

i7456 发表于 2014-5-16 10:27 static/image/common/back.gif
arduino的IIC通讯,可以参考官方的例子。
http://arduino.cc/en/Tutorial/MasterWriter

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

i7456 发表于 2014-5-16 13:59:06

大海 发表于 2014-5-16 13:51 static/image/common/back.gif
就是把官方的例子里从机加一个led(d13)用主机控制从机的led的亮灭

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

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

大海 发表于 2014-5-16 14:08:08

i7456 发表于 2014-5-16 13:59 static/image/common/back.gif
我也学机械的,大学毕业设计搞的PLC编程。

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

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

i7456 发表于 2014-5-16 17:04:14

大海 发表于 2014-5-16 14:08 static/image/common/back.gif
大神帮忙编一下吧,我看着编好的程序学习下。。真的好难上手啊

花点时间看看,其实很简单的.

lty365 发表于 2014-5-16 17:29:16

其实 我是来看 一个脚 接很多传感器 怎么写的....
没有....
页: [1]
查看完整版本: 关于iic多机通讯