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@1:936a1850d885, 2022-04-25 (annotated)
- Committer:
- micros22eq7
- Date:
- Mon Apr 25 18:46:01 2022 +0000
- Revision:
- 1:936a1850d885
- Parent:
- 0:a9e068e17f41
- Child:
- 3:777d4daea16d
Obtencion de valores del acelerometro interno del KL25Z y conexion entre microcontroladores KL25Z por protocolo de comunicacion I2C. Modulo MASTER / MAESTRO.
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) |
| fernando_moraless | 0:a9e068e17f41 | 9 | Serial pc(USBTX, USBRX); |
| fernando_moraless | 0:a9e068e17f41 | 10 | |
| fernando_moraless | 0:a9e068e17f41 | 11 | #define BUFFER_SIZE 50 |
| fernando_moraless | 0:a9e068e17f41 | 12 | #define SLAVE_ADDR 0xA0 |
| 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 |
| fernando_moraless | 0:a9e068e17f41 | 20 | pc.printf("\r"); //Mueve cursor al origen |
| fernando_moraless | 0:a9e068e17f41 | 21 | pc.printf("MASTER\r\n"); |
| fernando_moraless | 0:a9e068e17f41 | 22 | |
| fernando_moraless | 0:a9e068e17f41 | 23 | char buf[BUFFER_SIZE]; |
| fernando_moraless | 0:a9e068e17f41 | 24 | |
| fernando_moraless | 0:a9e068e17f41 | 25 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
| fernando_moraless | 0:a9e068e17f41 | 26 | // SDA SCL DIRECCIÓN |
| fernando_moraless | 0:a9e068e17f41 | 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); |
| fernando_moraless | 0:a9e068e17f41 | 31 | |
| fernando_moraless | 0:a9e068e17f41 | 32 | if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) { |
| fernando_moraless | 0:a9e068e17f41 | 33 | pc.printf("CONECTADO %s\r\n", buf); |
| fernando_moraless | 0:a9e068e17f41 | 34 | } |
| fernando_moraless | 0:a9e068e17f41 | 35 | |
| micros22eq7 | 1:936a1850d885 | 36 | float x, y, z; // Aceleracion en cada eje |
| fernando_moraless | 0:a9e068e17f41 | 37 | |
| fernando_moraless | 0:a9e068e17f41 | 38 | while (true) { |
| fernando_moraless | 0:a9e068e17f41 | 39 | |
| micros22eq7 | 1:936a1850d885 | 40 | x = abs(acc.getAccX()); // Obtener aceleracion en cada eje |
| fernando_moraless | 0:a9e068e17f41 | 41 | y = abs(acc.getAccY()); |
| fernando_moraless | 0:a9e068e17f41 | 42 | z = abs(acc.getAccZ()); |
| micros22eq7 | 1:936a1850d885 | 43 | |
| micros22eq7 | 1:936a1850d885 | 44 | wait(1); |
| micros22eq7 | 1:936a1850d885 | 45 | |
| micros22eq7 | 1:936a1850d885 | 46 | rled = 1.0f - x; //Encender LED interno |
| fernando_moraless | 0:a9e068e17f41 | 47 | gled = 1.0f - y; |
| fernando_moraless | 0:a9e068e17f41 | 48 | bled = 1.0f - z; |
| fernando_moraless | 0:a9e068e17f41 | 49 | |
| micros22eq7 | 1:936a1850d885 | 50 | //pc.printf("X: %1.1f, Y: %1.1f, Z: %1.1f\r\n", x, y, z); |
| micros22eq7 | 1:936a1850d885 | 51 | sprintf(buf, "%1.1f, %1.1f, %1.1f", x, y, z); |
| fernando_moraless | 0:a9e068e17f41 | 52 | pc.printf("%s\r\n", buf); |
| micros22eq7 | 1:936a1850d885 | 53 | |
| fernando_moraless | 0:a9e068e17f41 | 54 | if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) |
| fernando_moraless | 0:a9e068e17f41 | 55 | { |
| fernando_moraless | 0:a9e068e17f41 | 56 | wait(0.2); |
| fernando_moraless | 0:a9e068e17f41 | 57 | pc.printf("Written to slave: %s\r\n", buf); |
| fernando_moraless | 0:a9e068e17f41 | 58 | } |
| fernando_moraless | 0:a9e068e17f41 | 59 | |
| fernando_moraless | 0:a9e068e17f41 | 60 | wait(1); |
| fernando_moraless | 0:a9e068e17f41 | 61 | |
| fernando_moraless | 0:a9e068e17f41 | 62 | if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) |
| fernando_moraless | 0:a9e068e17f41 | 63 | { |
| fernando_moraless | 0:a9e068e17f41 | 64 | wait(0.2); |
| fernando_moraless | 0:a9e068e17f41 | 65 | pc.printf("Written to slave: %s\r\n", buf); |
| fernando_moraless | 0:a9e068e17f41 | 66 | } |
| fernando_moraless | 0:a9e068e17f41 | 67 | } |
| fernando_moraless | 0:a9e068e17f41 | 68 | } |