slave

Dependencies:   mbed

Revision:
0:516fb708ec42
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Dec 17 18:04:08 2018 +0000
@@ -0,0 +1,55 @@
+#include <mbed.h>
+
+I2CSlave slave(D14, D15);
+Serial pc(USBTX, USBRX);
+DigitalIn mybutton(USER_BUTTON);
+#define ADDRESS 0x50
+int main() {
+   pc.baud(57600);
+   pc.printf("Starting slave device at address:0x%02x\r\n", ADDRESS);
+   slave.frequency(400000); //Set the clock frequency
+   slave.address(ADDRESS);
+   char buf[6];
+   //char msg[1];
+   //msg[0] = 0x00;
+   int j = 1;
+   
+   while (1) {
+       
+       int i = slave.receive();
+       if(i > 0)pc.printf("rcv:%d\r\n", i);
+         
+       switch (i) {
+           case I2CSlave::ReadAddressed:
+               pc.printf("I2CSlave::ReadAddressed\r\n");
+               //slave.write(msg, strlen(msg) + 1); // Includes null char
+               break;
+           case I2CSlave::WriteAddressed:
+               pc.printf("I2CSlave::WriteAddressed\r\n");
+               
+               slave.read(buf, 2);
+               /*for(int ik=0;ik<7;ik++)
+               {
+                   buf[ik]=0x00;
+                }*/
+                printf("Read A: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x \r\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
+                break;
+       
+               
+       }
+       if(mybutton == 0) {
+           
+        
+       buf[0] = 0;    // Clear buffer
+       buf[1] = 0;
+       buf[2] = 0;
+       buf[3] = 0;
+       buf[4] = 0;
+       buf[5] = 0;
+       
+       }
+       //msg[0] = j;
+       j++;
+       
+   }
+}
\ No newline at end of file