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:
- 3:764ccaf29ce9
- Parent:
- 1:acf14b6dd1be
- Child:
- 4:fc56fa8aa794
--- a/master_i2c.cpp Fri Aug 10 20:22:44 2012 +0000
+++ b/master_i2c.cpp Thu Sep 20 23:45:05 2012 +0000
@@ -12,7 +12,8 @@
char cmd[2];
char buf[50];
-void i2c_write(char * data) {
+void i2c_write(char * data)
+{
int ack = master.write(SLAVEADDR, cmd, 2); // Send command string
if (ack)
fail=!fail;
@@ -22,7 +23,8 @@
}
}
-void i2c_read(char * buf) {
+void i2c_read(char * buf)
+{
/*
int ack = master.read(SLAVEADDR, buf, 6);
if (ack)
@@ -34,22 +36,19 @@
*/
int ack, data, scan_counter;
scan_counter = 0;
- while(1)
- {
+ while(1) {
data=master.read(ack);
- if(data==0xFF)
- {
+ if(data==0xFF) {
pc_master.printf("Scan finish..\n");
break;
- }
- else
- {
+ } else {
pc_master.printf("data %d reads %d\n",scan_counter++, data);
}
}
}
-void i2c_test() {
+void i2c_test()
+{
master.start();
cmd[0] = 'U';
cmd[1] = 'S';
@@ -63,7 +62,8 @@
}
}
-void i2c_test2() {
+void i2c_test2()
+{
//send signal to mbed2 and wait for response
master.start();
cmd[0] = 'C';
@@ -83,7 +83,8 @@
}
}
-void master_write() {
+void master_write()
+{
char cmd[] = "scan";
while (master.write(SLAVEADDR, cmd, 4)) {
fail=1;
@@ -91,38 +92,28 @@
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;
+void master_read(int* ro_reading)
+{
+ char* data_read;
+ data_read = new char [64*5+2];
+ for(int i=0; i<64*5+2; i++) {
+ data_read[i]=(char)47;
+ }
+ while(data_read[0]==47) {
+ wait(18);
+ master.read(SLAVEADDR|1, data_read, 64*5, true);
}
- 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;
+ for(int i=0; i<64; i++) {
+ int out = 0;
+ out += data_read[i*5+4]-48;
+ out *= 10;
+ out += data_read[i*5+3]-48;
+ out *= 10;
+ out += data_read[i*5+2]-48;
+ out *= 10;
+ out += data_read[i*5+1]-48;
+ out *= 10;
+ out += data_read[i*5+0]-48;
+ ro_reading[i] = out;
}
- 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