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
- Committer:
- fernando_moraless
- Date:
- 2022-04-25
- Revision:
- 0:a9e068e17f41
- Child:
- 1:936a1850d885
File content as of revision 0:a9e068e17f41:
//CÓDIGO 1-A
#include "mbed.h"
#include "MMA8451Q.h"
#include "stdio.h"
#include "stdlib.h"
#define MMA8451_I2C_ADDRESS (0x1d << 1)
Serial pc(USBTX, USBRX);
#define BUFFER_SIZE 50
#define SLAVE_ADDR 0xA0
I2C master(I2C_SDA, I2C_SCL);
int main(void)
{
master.frequency (100000);
pc.printf("\x1b[2J"); //CLEAR
pc.printf("\r"); //Mueve cursor al origen
pc.printf("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);
}
float x, y, z;
while (true) {
x = abs(acc.getAccX());
y = abs(acc.getAccY());
z = abs(acc.getAccZ());
rled = 1.0f - x;
gled = 1.0f - y;
bled = 1.0f - z;
sprintf(buf, "%1.2f, %1.2f, %1.2f", 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);
}
}
}