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: main.cpp
- Revision:
- 3:777d4daea16d
- Parent:
- 1:936a1850d885
diff -r 0835e015c255 -r 777d4daea16d main.cpp
--- a/main.cpp Mon Apr 25 21:29:21 2022 +0000
+++ b/main.cpp Tue Apr 26 05:25:50 2022 +0000
@@ -6,10 +6,10 @@
#include "stdlib.h"
#define MMA8451_I2C_ADDRESS (0x1d << 1)
-Serial pc(USBTX, USBRX);
+#define BUFFER_SIZE 16
+#define SLAVE_ADDR 0xA0
-#define BUFFER_SIZE 50
-#define SLAVE_ADDR 0xA0
+Serial pc(USBTX, USBRX);
I2C master(I2C_SDA, I2C_SCL);
@@ -17,52 +17,42 @@
{
master.frequency (100000);
pc.printf("\x1b[2J"); //CLEAR
- pc.printf("\r"); //Mueve cursor al origen
- pc.printf("MASTER\r\n");
-
+ pc.printf("\r");
+ pc.printf("I'M MASTER\r\n");
+
char buf[BUFFER_SIZE];
-
+
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
// SDA SCL DIRECCIÓN
-
+
PwmOut rled(LED1);
PwmOut gled(LED2);
PwmOut bled(LED3);
-
- if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) {
- pc.printf("CONECTADO %s\r\n", buf);
- }
-
+
+ if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE) == 0)
+ pc.printf("CONECTADO: %s\r\n", buf);
+
float x, y, z; // Aceleracion en cada eje
-
+
while (true) {
-
x = abs(acc.getAccX()); // Obtener aceleracion en cada eje
y = abs(acc.getAccY());
z = abs(acc.getAccZ());
-
- wait(1);
-
+
rled = 1.0f - x; //Encender LED interno
gled = 1.0f - y;
bled = 1.0f - z;
+
+ wait(0.5);
- //pc.printf("X: %1.1f, Y: %1.1f, Z: %1.1f\r\n", x, y, z);
sprintf(buf, "%1.1f, %1.1f, %1.1f", x, y, z);
pc.printf("%s\r\n", buf);
-
- if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE) == 0)
- {
- wait(0.2);
- pc.printf("Written to slave: %s\r\n", buf);
- }
-
- wait(1);
-
- if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE) == 0)
- {
- wait(0.2);
- pc.printf("Written to slave: %s\r\n", buf);
- }
+
+ //Master escribe en slave
+ if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE) == 0)
+ pc.printf("Escrito en slave: %s\r\n", buf);
+
+ if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE) == 0)
+ pc.printf("Lee de slave: %s\r\n", buf);
}
}
\ No newline at end of file