<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: I2C on Maple Native with Arduino as slave</title>
		<link>http://forums.leaflabs.com/topic.php?id=1307</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:16:28 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=1307" rel="self" type="application/rss+xml" />

		<item>
			<title>autobot on "I2C on Maple Native with Arduino as slave"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1307#post-21203</link>
			<pubDate>Tue, 20 Nov 2012 19:45:04 +0000</pubDate>
			<dc:creator>autobot</dc:creator>
			<guid isPermaLink="false">21203@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Did this ever work? @Manitou, do you have sample code for the RET6/Arduino 328p communication?&#60;/p&#62;
&#60;p&#62;Didn't want to open a new thread as I essentially have the same problem.&#60;/p&#62;
&#60;p&#62;Thanks in advanced.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>manitou on "I2C on Maple Native with Arduino as slave"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1307#post-9864</link>
			<pubDate>Fri, 23 Mar 2012 16:00:17 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">9864@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Two Arduino's can talk I2C with direct wiring since the Wire lib utilizes the AVR's pullups.  For the Maple to talk I2C to the Arduino, you'll need  5K pullups on SDA and CLK. I have gotten an RET6 to talk to an Arduino 328p, but I had to use the Maple Wire library descirbed at&#60;br /&#62;
 &#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=1268&#38;amp;page=2&#34; rel=&#34;nofollow&#34;&#62;http://forums.leaflabs.com/topic.php?id=1268&#38;amp;page=2&#60;/a&#62;&#60;br /&#62;
To see things on the scope I modified the Wire.h to use delayMicroseconds() for the SDA/CLK delays.&#60;br /&#62;
It worked to pins 5 and 9 powering the AVR from either 3.3v or 5v.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>manitou on "I2C on Maple Native with Arduino as slave"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1307#post-9029</link>
			<pubDate>Sat, 17 Mar 2012 04:52:08 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">9029@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;If you are using 5v for mega328, then you'll want to use Maple pins that can take 5v (e.g., pins 5 and 9)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>fugalster on "I2C on Maple Native with Arduino as slave"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1307#post-7934</link>
			<pubDate>Sat, 28 Jan 2012 15:58:08 +0000</pubDate>
			<dc:creator>fugalster</dc:creator>
			<guid isPermaLink="false">7934@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I'm trying to get my Maple Native to act as master on I2C with an atMega328 as slave. I've successfully gotten I2C working with the bmp085 after some trouble (&#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=925#post-7931&#34; rel=&#34;nofollow&#34;&#62;http://forums.leaflabs.com/topic.php?id=925#post-7931&#60;/a&#62;) using software implementation with the Wire.h library, but have yet to get my atMega to respond. The same atMega with the same code works fine as slave with an Arduino Uno as master. Here's the code on the slave:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;lt;Wire.h&#38;gt;

int slaveAddress = 0x7f;
int ledPin = 13;
int inData;

void setup() {
    Wire.begin(slaveAddress);
    Wire.onReceive(receive);
    Wire.onRequest(request);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
}

void loop() {
    ///////////////////////////////////////////////////////////////
    // I2C is taken care of in event handlers defined in setup() //
    ///////////////////////////////////////////////////////////////

    for (int i=0; i&#38;lt;inData; i++) { // flash so many times
        digitalWrite(ledPin, HIGH);
        delay(200);
        digitalWrite(ledPin, LOW);
        delay(200);
    }
    delay(1000);
}

void receive(int num) { // num comes from the number of bytes being received, defined by master

    static char inString[16];
    static int i;

    for (i=0; i&#38;lt;num; i++) {
        inString[i] = Wire.read(); // read in a byte
    }

    inString[i] = &#38;#39;&#38;#39;; // complete the string
    inData = atoi(inString); // convert string to integer

}

void request() {
    char writeString[16]; // for formating a string to send over I2C
    sprintf(writeString, &#38;quot;You rang?\n%d&#38;quot;, inData);
    Wire.write(writeString);
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;And the master:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;quot;Wire.h&#38;quot;

#define ATMEGA_ADDR 0x7f

unsigned long now;
int i = 0;
char inString[16]; // string for storing incoming data

void setup() {
    Serial1.begin(115200);

    Wire.begin(0,1); // initialize bitbanged (software) i2c protocol
    Serial1.println(&#38;quot;I2C Bus 1 Initialized&#38;quot;);
    now = millis();
    pinMode(22,OUTPUT);
}

void loop() {

    if (Serial1.available() &#38;gt; 0) { // probe the serial monitor for data (not I2C)
        char inByte = Serial1.read();
        if (inByte == &#38;#39;0&#38;#39; &#124;&#124; inByte == &#38;#39;1&#38;#39; &#124;&#124; inByte == &#38;#39;2&#38;#39; &#124;&#124; inByte == &#38;#39;3&#38;#39; &#124;&#124;
            inByte == &#38;#39;4&#38;#39; &#124;&#124; inByte == &#38;#39;5&#38;#39; &#124;&#124; inByte == &#38;#39;6&#38;#39; &#124;&#124; inByte == &#38;#39;7&#38;#39; &#124;&#124;
            inByte == &#38;#39;8&#38;#39; &#124;&#124; inByte == &#38;#39;9&#38;#39;) { // check serial monitor for a digit
            inString[i] = inByte; // add the digit to the string
            i++; // get ready to read next byte
        } else if (inByte == &#38;#39;S&#38;#39; &#124;&#124; inByte == &#38;#39;s&#38;#39;) { // check for flash flag
            inString[i] = &#38;#39;&#38;#39;; // add null character to end of string
            i = 0; // reset for new data
            Serial1.print(&#38;quot;sending data: &#38;quot;);
            Serial1.println(inString);

            //////////////////////////////////
            // I2C transmission starts here //
            //////////////////////////////////
            Wire.beginTransmission(0x7f); // initiate I2C transmission
            Wire.send(inString);          // queue the string to be transmitted over I2C
            int r = Wire.endTransmission(); // send string and close transmission
            Serial1.println(r);
        } else if (inByte == &#38;#39;R&#38;#39; &#124;&#124; inByte == &#38;#39;r&#38;#39;) {
            Wire.requestFrom(0x7f, 16);   // request 16 bytes from slave
            while (Wire.available() &#38;gt; 0) {
                char received = Wire.receive();
                ///////////////////////////
                // I2C transmission ends //
                ///////////////////////////

                Serial1.print(received); // read a byte and print over serial
                i = 0;                   // reset to avoid confusion
            }
            Serial1.print(&#38;#39;\n&#38;#39;);         // new line
        }
    }

    // blink led to make sure program is running
    if (millis() - now &#38;gt; 1000) {
        toggleLED();
        now = millis();
    }

}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;The idea here is that from a serial monitor connected to the slave you will type a number followed by &#34;s&#34; (for &#34;send&#34;) and the number will be sent to the slave over I2C and a confirmation message will be sent over the serial monitor:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;Sending data: &#38;lt;num&#38;gt;&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;where &#38;lt;num&#38;gt; is the number sent. The slave will then flash an LED that many times, repeated, until a new number is sent. You can also type an &#34;r&#34; (for &#34;ring&#34; or &#34;read&#34;) and the master will request data from the slave, whereupon the slave will reply:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;You rang?
&#38;lt;num&#38;gt;&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;where &#38;lt;num&#38;gt; is the last number it received. The message is echoed on the serial monitor.&#60;/p&#62;
&#60;p&#62;Again, the code is proved, everything works as expected with the Arduino as master. The only difference in the code between the Arduino master and the Maple master is Wire.begin() vs. Wire.begin(0,1) and Serial vs. Serial1. When running on the Maple, Wire.endTransmission() will return a 2 (I think that's for ENACKADDR).&#60;/p&#62;
&#60;p&#62;If anybody has any ideas, your help would be appreciated.
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
