I2C Slave device. Mimics the 24LC256 in a very limited way (no addressing)

Dependencies:   mbed

Fork of I2CSlave by phil dani

Revision:
2:42d461c96301
Parent:
0:fb6bbc10ffa0
Child:
3:1bb80997f316
--- a/main.cpp	Sun Jan 01 20:57:57 2012 +0000
+++ b/main.cpp	Thu Aug 29 12:35:44 2013 +0000
@@ -1,12 +1,41 @@
-#include "mbed.h"
-
-DigitalOut myled(LED1);
-
-int main() {
-    while(1) {
-        myled = 1;
-        wait(0.2);
-        myled = 0;
-        wait(0.2);
-    }
-}
+/*Program Example 7.6: I2C Slave, when called transfers switch state to mbed acting as Master, and
+displays state of Master’ s switches on its leds.
+*/
+#include <mbed.h>
+I2CSlave slave(p9, p10); //Configure I2C slave
+DigitalOut red_led(p14); //red led
+DigitalOut green_led(p12); //green led
+DigitalIn switch_ip1(p5);
+DigitalIn switch_ip2(p6);
+char switch_word ; //word we will send
+char recd_val; //value received from master
+
+int main() {
+    slave.address(0x52);
+        while (1) {
+            //set up switch_word from switches that are pressed
+            switch_word=0xa0; //set up a recognizable output pattern
+            if (switch_ip1==1)
+            switch_word=switch_word|0x01;
+            if (switch_ip2==1)
+            switch_word=switch_word|0x02;
+            slave.write(switch_word); //load up word to send
+            //test for I2C, and act accordingly
+            int i = slave.receive();
+            if (i == 3){ //slave is addressed, Master will write
+            recd_val= slave.read();
+            //now set leds according to received word
+            red_led=0; //preset both to 0
+            green_led=0;
+            recd_val=recd_val&0x03; //AND out unwanted bits
+            if (recd_val==1)
+                red_led=1;
+            if (recd_val==2)
+                green_led=1;
+            if (recd_val==3){
+                red_led=1;
+                green_led=1;
+            }
+            }
+        }
+}
\ No newline at end of file