Programa

Dependencies:   mbed

Committer:
darley_gonzalez
Date:
Tue Mar 02 22:37:23 2021 +0000
Revision:
0:782b008e3b3a
I2c Slave

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darley_gonzalez 0:782b008e3b3a 1 #include "MMA8451Q.h"
darley_gonzalez 0:782b008e3b3a 2 #include "mbed.h"
darley_gonzalez 0:782b008e3b3a 3
darley_gonzalez 0:782b008e3b3a 4 #if defined(TARGET_KL25Z) || defined(TARGET_KL46Z)
darley_gonzalez 0:782b008e3b3a 5 PinName const SDA = PTE25;
darley_gonzalez 0:782b008e3b3a 6 PinName const SCL = PTE24; //pines acelerometro
darley_gonzalez 0:782b008e3b3a 7 #elif defined(TARGET_KL05Z)
darley_gonzalez 0:782b008e3b3a 8 PinName const SDA = PTB4;
darley_gonzalez 0:782b008e3b3a 9 PinName const SCL = PTB3;
darley_gonzalez 0:782b008e3b3a 10 #elif defined(TARGET_K20D50M)
darley_gonzalez 0:782b008e3b3a 11 PinName const SDA = PTB1;
darley_gonzalez 0:782b008e3b3a 12 PinName const SCL = PTB0;
darley_gonzalez 0:782b008e3b3a 13 #else
darley_gonzalez 0:782b008e3b3a 14 #error TARGET NOT DEFINED
darley_gonzalez 0:782b008e3b3a 15 #endif
darley_gonzalez 0:782b008e3b3a 16 #define MMA8451_I2C_ADDRESS (0x1d << 1)
darley_gonzalez 0:782b008e3b3a 17 I2CSlave slave(PTE0, PTE1);
darley_gonzalez 0:782b008e3b3a 18 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
darley_gonzalez 0:782b008e3b3a 19 PwmOut rled(D3);
darley_gonzalez 0:782b008e3b3a 20 PwmOut gled(D4);
darley_gonzalez 0:782b008e3b3a 21 PwmOut bled(D5);
darley_gonzalez 0:782b008e3b3a 22 Serial pc(USBTX, USBRX);
darley_gonzalez 0:782b008e3b3a 23
darley_gonzalez 0:782b008e3b3a 24 DigitalOut led1(LED2);
darley_gonzalez 0:782b008e3b3a 25
darley_gonzalez 0:782b008e3b3a 26 float datos()
darley_gonzalez 0:782b008e3b3a 27 {
darley_gonzalez 0:782b008e3b3a 28 float x, y, z;
darley_gonzalez 0:782b008e3b3a 29 x = abs(acc.getAccX());
darley_gonzalez 0:782b008e3b3a 30 y = abs(acc.getAccY());
darley_gonzalez 0:782b008e3b3a 31 z = abs(acc.getAccZ());
darley_gonzalez 0:782b008e3b3a 32 rled = 1.0f - x;
darley_gonzalez 0:782b008e3b3a 33 gled = 1.0f - y;
darley_gonzalez 0:782b008e3b3a 34 bled = 1.0f - z;
darley_gonzalez 0:782b008e3b3a 35 wait_us(100000);
darley_gonzalez 0:782b008e3b3a 36 return y;
darley_gonzalez 0:782b008e3b3a 37 // printf("X: %f, Y: %f, Z: %f\n", x,y,z);
darley_gonzalez 0:782b008e3b3a 38 }
darley_gonzalez 0:782b008e3b3a 39 int main(void)
darley_gonzalez 0:782b008e3b3a 40 {
darley_gonzalez 0:782b008e3b3a 41 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
darley_gonzalez 0:782b008e3b3a 42 PwmOut rled(D3);
darley_gonzalez 0:782b008e3b3a 43 PwmOut gled(D4);
darley_gonzalez 0:782b008e3b3a 44 PwmOut bled(D5);
darley_gonzalez 0:782b008e3b3a 45 char buf[13]; //recibir datos para guardar en un buffer
darley_gonzalez 0:782b008e3b3a 46 printf("MMA8451 ID: %d\n", acc.getWhoAmI()); //identifica que el sensor esta en funcionamiento en serial
darley_gonzalez 0:782b008e3b3a 47 slave.address(0xA0); //10100000
darley_gonzalez 0:782b008e3b3a 48 while (true) {
darley_gonzalez 0:782b008e3b3a 49 // 10 0111 110 0100
darley_gonzalez 0:782b008e3b3a 50 int i = slave.receive();
darley_gonzalez 0:782b008e3b3a 51 switch (i) {
darley_gonzalez 0:782b008e3b3a 52 case I2CSlave::WriteAddressed:
darley_gonzalez 0:782b008e3b3a 53 slave.read(buf, 13);
darley_gonzalez 0:782b008e3b3a 54 // sprintf(buf, "%f\n", datos);
darley_gonzalez 0:782b008e3b3a 55 float x, y, z; //enviar datos de acelerometro
darley_gonzalez 0:782b008e3b3a 56 x = abs(acc.getAccX());
darley_gonzalez 0:782b008e3b3a 57 y = abs(acc.getAccY());
darley_gonzalez 0:782b008e3b3a 58 z = abs(acc.getAccZ());
darley_gonzalez 0:782b008e3b3a 59 sprintf(buf,"%f%f%f\n",x,y,z);
darley_gonzalez 0:782b008e3b3a 60 pc.printf("%f%f%f\n",x,y,z);
darley_gonzalez 0:782b008e3b3a 61
darley_gonzalez 0:782b008e3b3a 62 printf("%s\n", buf);
darley_gonzalez 0:782b008e3b3a 63 slave.write(buf, 13);
darley_gonzalez 0:782b008e3b3a 64 led1 = !led1;
darley_gonzalez 0:782b008e3b3a 65 break; //rompe el ciclo y reinicia el buffer
darley_gonzalez 0:782b008e3b3a 66 }
darley_gonzalez 0:782b008e3b3a 67
darley_gonzalez 0:782b008e3b3a 68 for (int i = 0; i < 10; i++) {
darley_gonzalez 0:782b008e3b3a 69 buf[i] = 0; // Clear buffer
darley_gonzalez 0:782b008e3b3a 70 }
darley_gonzalez 0:782b008e3b3a 71 }
darley_gonzalez 0:782b008e3b3a 72 }