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.
main.cpp@3:777d4daea16d, 2022-04-26 (annotated)
- Committer:
- micros22eq7
- Date:
- Tue Apr 26 05:25:50 2022 +0000
- Revision:
- 3:777d4daea16d
- Parent:
- 1:936a1850d885
v5
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| micros22eq7 | 1:936a1850d885 | 1 | //CÓDIGO 3-A |
| fernando_moraless | 0:a9e068e17f41 | 2 | |
| fernando_moraless | 0:a9e068e17f41 | 3 | #include "mbed.h" |
| fernando_moraless | 0:a9e068e17f41 | 4 | #include "MMA8451Q.h" |
| fernando_moraless | 0:a9e068e17f41 | 5 | #include "stdio.h" |
| fernando_moraless | 0:a9e068e17f41 | 6 | #include "stdlib.h" |
| fernando_moraless | 0:a9e068e17f41 | 7 | |
| fernando_moraless | 0:a9e068e17f41 | 8 | #define MMA8451_I2C_ADDRESS (0x1d << 1) |
| micros22eq7 | 3:777d4daea16d | 9 | #define BUFFER_SIZE 16 |
| micros22eq7 | 3:777d4daea16d | 10 | #define SLAVE_ADDR 0xA0 |
| fernando_moraless | 0:a9e068e17f41 | 11 | |
| micros22eq7 | 3:777d4daea16d | 12 | Serial pc(USBTX, USBRX); |
| fernando_moraless | 0:a9e068e17f41 | 13 | I2C master(I2C_SDA, I2C_SCL); |
| fernando_moraless | 0:a9e068e17f41 | 14 | |
| fernando_moraless | 0:a9e068e17f41 | 15 | |
| fernando_moraless | 0:a9e068e17f41 | 16 | int main(void) |
| fernando_moraless | 0:a9e068e17f41 | 17 | { |
| fernando_moraless | 0:a9e068e17f41 | 18 | master.frequency (100000); |
| micros22eq7 | 1:936a1850d885 | 19 | pc.printf("\x1b[2J"); //CLEAR |
| micros22eq7 | 3:777d4daea16d | 20 | pc.printf("\r"); |
| micros22eq7 | 3:777d4daea16d | 21 | pc.printf("I'M MASTER\r\n"); |
| micros22eq7 | 3:777d4daea16d | 22 | |
| fernando_moraless | 0:a9e068e17f41 | 23 | char buf[BUFFER_SIZE]; |
| micros22eq7 | 3:777d4daea16d | 24 | |
| fernando_moraless | 0:a9e068e17f41 | 25 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
| fernando_moraless | 0:a9e068e17f41 | 26 | // SDA SCL DIRECCIÓN |
| micros22eq7 | 3:777d4daea16d | 27 | |
| fernando_moraless | 0:a9e068e17f41 | 28 | PwmOut rled(LED1); |
| fernando_moraless | 0:a9e068e17f41 | 29 | PwmOut gled(LED2); |
| fernando_moraless | 0:a9e068e17f41 | 30 | PwmOut bled(LED3); |
| micros22eq7 | 3:777d4daea16d | 31 | |
| micros22eq7 | 3:777d4daea16d | 32 | if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) |
| micros22eq7 | 3:777d4daea16d | 33 | pc.printf("CONECTADO: %s\r\n", buf); |
| micros22eq7 | 3:777d4daea16d | 34 | |
| micros22eq7 | 1:936a1850d885 | 35 | float x, y, z; // Aceleracion en cada eje |
| micros22eq7 | 3:777d4daea16d | 36 | |
| fernando_moraless | 0:a9e068e17f41 | 37 | while (true) { |
| micros22eq7 | 1:936a1850d885 | 38 | x = abs(acc.getAccX()); // Obtener aceleracion en cada eje |
| fernando_moraless | 0:a9e068e17f41 | 39 | y = abs(acc.getAccY()); |
| fernando_moraless | 0:a9e068e17f41 | 40 | z = abs(acc.getAccZ()); |
| micros22eq7 | 3:777d4daea16d | 41 | |
| micros22eq7 | 1:936a1850d885 | 42 | rled = 1.0f - x; //Encender LED interno |
| fernando_moraless | 0:a9e068e17f41 | 43 | gled = 1.0f - y; |
| fernando_moraless | 0:a9e068e17f41 | 44 | bled = 1.0f - z; |
| micros22eq7 | 3:777d4daea16d | 45 | |
| micros22eq7 | 3:777d4daea16d | 46 | wait(0.5); |
| fernando_moraless | 0:a9e068e17f41 | 47 | |
| micros22eq7 | 1:936a1850d885 | 48 | sprintf(buf, "%1.1f, %1.1f, %1.1f", x, y, z); |
| fernando_moraless | 0:a9e068e17f41 | 49 | pc.printf("%s\r\n", buf); |
| micros22eq7 | 3:777d4daea16d | 50 | |
| micros22eq7 | 3:777d4daea16d | 51 | //Master escribe en slave |
| micros22eq7 | 3:777d4daea16d | 52 | if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) |
| micros22eq7 | 3:777d4daea16d | 53 | pc.printf("Escrito en slave: %s\r\n", buf); |
| micros22eq7 | 3:777d4daea16d | 54 | |
| micros22eq7 | 3:777d4daea16d | 55 | if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) |
| micros22eq7 | 3:777d4daea16d | 56 | pc.printf("Lee de slave: %s\r\n", buf); |
| fernando_moraless | 0:a9e068e17f41 | 57 | } |
| fernando_moraless | 0:a9e068e17f41 | 58 | } |