ejer 5 pag embebidos

Dependencies:   mbed MMA8451Q

Committer:
Charin
Date:
Wed Apr 10 14:40:53 2019 +0000
Revision:
0:8bdbfbb8ddc1
se

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charin 0:8bdbfbb8ddc1 1 #include "mbed.h"
Charin 0:8bdbfbb8ddc1 2 #include "math.h"
Charin 0:8bdbfbb8ddc1 3 #include "MMA8451Q.h"
Charin 0:8bdbfbb8ddc1 4
Charin 0:8bdbfbb8ddc1 5 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
Charin 0:8bdbfbb8ddc1 6 PinName const SDA = PTE25;
Charin 0:8bdbfbb8ddc1 7 PinName const SCL = PTE24;
Charin 0:8bdbfbb8ddc1 8 #elif defined (TARGET_KL05Z)
Charin 0:8bdbfbb8ddc1 9 PinName const SDA = PTB4;
Charin 0:8bdbfbb8ddc1 10 PinName const SCL = PTB3;
Charin 0:8bdbfbb8ddc1 11 #elif defined (TARGET_K20D50M)
Charin 0:8bdbfbb8ddc1 12 PinName const SDA = PTB1;
Charin 0:8bdbfbb8ddc1 13 PinName const SCL = PTB0;
Charin 0:8bdbfbb8ddc1 14 #else
Charin 0:8bdbfbb8ddc1 15 #error TARGET NOT DEFINED
Charin 0:8bdbfbb8ddc1 16 #endif
Charin 0:8bdbfbb8ddc1 17
Charin 0:8bdbfbb8ddc1 18 #define MMA8451_I2C_ADDRESS (0x1d<<1)
Charin 0:8bdbfbb8ddc1 19
Charin 0:8bdbfbb8ddc1 20 Ticker timer;
Charin 0:8bdbfbb8ddc1 21 Ticker timer2;
Charin 0:8bdbfbb8ddc1 22
Charin 0:8bdbfbb8ddc1 23 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
Charin 0:8bdbfbb8ddc1 24 PwmOut rled(LED1);
Charin 0:8bdbfbb8ddc1 25 PwmOut gled(LED2);
Charin 0:8bdbfbb8ddc1 26 PwmOut bled(LED3);
Charin 0:8bdbfbb8ddc1 27
Charin 0:8bdbfbb8ddc1 28
Charin 0:8bdbfbb8ddc1 29
Charin 0:8bdbfbb8ddc1 30 void posicion ()
Charin 0:8bdbfbb8ddc1 31 {
Charin 0:8bdbfbb8ddc1 32
Charin 0:8bdbfbb8ddc1 33 float x, y, z;
Charin 0:8bdbfbb8ddc1 34 x = abs(acc.getAccX());
Charin 0:8bdbfbb8ddc1 35 y = abs(acc.getAccY());
Charin 0:8bdbfbb8ddc1 36 z = abs(acc.getAccZ());
Charin 0:8bdbfbb8ddc1 37
Charin 0:8bdbfbb8ddc1 38 rled = 1.0f - x;
Charin 0:8bdbfbb8ddc1 39 gled = 1.0f - y;
Charin 0:8bdbfbb8ddc1 40 bled = 1.0f - z;
Charin 0:8bdbfbb8ddc1 41 printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
Charin 0:8bdbfbb8ddc1 42
Charin 0:8bdbfbb8ddc1 43 }
Charin 0:8bdbfbb8ddc1 44
Charin 0:8bdbfbb8ddc1 45 void angulo ()
Charin 0:8bdbfbb8ddc1 46 {
Charin 0:8bdbfbb8ddc1 47 float ady, op, angulo;
Charin 0:8bdbfbb8ddc1 48 ady = abs(acc.getAccX());
Charin 0:8bdbfbb8ddc1 49 op = abs(acc.getAccZ());
Charin 0:8bdbfbb8ddc1 50
Charin 0:8bdbfbb8ddc1 51 angulo = atan (op / ady);
Charin 0:8bdbfbb8ddc1 52
Charin 0:8bdbfbb8ddc1 53 printf ("Angulo = %1.2f\n", angulo);
Charin 0:8bdbfbb8ddc1 54
Charin 0:8bdbfbb8ddc1 55 }
Charin 0:8bdbfbb8ddc1 56
Charin 0:8bdbfbb8ddc1 57 int main(void)
Charin 0:8bdbfbb8ddc1 58 {
Charin 0:8bdbfbb8ddc1 59
Charin 0:8bdbfbb8ddc1 60 timer.attach (&posicion, 0.5);
Charin 0:8bdbfbb8ddc1 61 timer2.attach (&angulo, 0.4);
Charin 0:8bdbfbb8ddc1 62
Charin 0:8bdbfbb8ddc1 63 while (true) {
Charin 0:8bdbfbb8ddc1 64
Charin 0:8bdbfbb8ddc1 65
Charin 0:8bdbfbb8ddc1 66 }
Charin 0:8bdbfbb8ddc1 67 }