Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: master_i2c.cpp
- Revision:
- 1:acf14b6dd1be
- Child:
- 3:764ccaf29ce9
diff -r c928c2d8bd02 -r acf14b6dd1be master_i2c.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/master_i2c.cpp Fri Aug 03 06:10:02 2012 +0000
@@ -0,0 +1,128 @@
+//master
+#include "mbed.h"
+#include <stdlib.h>
+
+I2C master(p28, p27); // sda, scl
+Serial pc_master(USBTX, USBRX); // tx, rx
+DigitalOut success(LED1);
+DigitalOut fail(LED2);
+DigitalIn read(p29);
+
+const int SLAVEADDR = 0x88; // define the I2C Slave Address (mbed 2)
+char cmd[2];
+char buf[50];
+
+void i2c_write(char * data) {
+ int ack = master.write(SLAVEADDR, cmd, 2); // Send command string
+ if (ack)
+ fail=!fail;
+ else {
+ pc_master.printf("Master is writing..\n");
+ success=!success;
+ }
+}
+
+void i2c_read(char * buf) {
+ /*
+ int ack = master.read(SLAVEADDR, buf, 6);
+ if (ack)
+ fail=!fail;
+ else {
+ pc_master.printf("Slave sent: %s\n", buf);
+ success=!success;
+ }
+ */
+ int ack, data, scan_counter;
+ scan_counter = 0;
+ while(1)
+ {
+ data=master.read(ack);
+ if(data==0xFF)
+ {
+ pc_master.printf("Scan finish..\n");
+ break;
+ }
+ else
+ {
+ pc_master.printf("data %d reads %d\n",scan_counter++, data);
+ }
+ }
+}
+
+void i2c_test() {
+ master.start();
+ cmd[0] = 'U';
+ cmd[1] = 'S';
+ while (1) {
+ if (read) {
+ i2c_read(buf);
+ } else {
+ i2c_write(cmd);
+ }
+ wait(0.07);
+ }
+}
+
+void i2c_test2() {
+ //send signal to mbed2 and wait for response
+ master.start();
+ cmd[0] = 'C';
+ while (master.write(SLAVEADDR, cmd, 2)) {
+ fail=!fail;
+ }
+ int ack = 1;
+ while (1) {
+ if (read) {
+ while (ack) {
+ ack = master.read(SLAVEADDR, buf, 2);
+ }
+ success = 1;
+ //pc_master.printf("Slave sent: $s\n", buf);
+ break;
+ }
+ }
+}
+
+void master_write() {
+ char cmd[] = "scan";
+ while (master.write(SLAVEADDR, cmd, 4)) {
+ fail=1;
+ }
+ fail = 0;
+}
+
+void master_read() {
+ /*
+ char * data[2];
+ for (int i=0; i<2; i++) {
+ int ack = 1;
+ while (ack) {
+ ack = master.read(SLAVEADDR, data[i], 10);
+ fail = 1;
+ }
+ fail = 0;
+ }
+ pc_master.printf("master gets: %s and %s", *(data[0]), *(data[1]));
+ */
+ pc_master.printf("master starts reading\n");
+ char data_read[64*5+1];
+ for(int i=0;i<64*5;i++)
+ {
+ data_read[i]=(char)49;
+ }
+ master.read(SLAVEADDR|1, data_read, 64*5, true);
+ for(int i=0;i<64;i++)
+ {
+ pc_master.printf("master gets: %c%c%c%c%c\n", data_read[5*i],data_read[5*i+1],data_read[5*i+2],data_read[5*i+3],data_read[5*i+4]);
+ }
+ pc_master.printf("master finishes reading\n");
+}
+
+void m2s_scan() {
+ pc_master.printf("master sends command..\n");
+ master_read();
+ master_write();
+
+ master_read();
+ master_read();
+}
\ No newline at end of file