Programme de base pour la machine à états sur l'accélomètre

Dependencies:   mbed MMA8451Q

Committer:
vermaelen
Date:
Wed Jun 03 00:32:28 2020 +0000
Revision:
0:c188fa46ae96
Child:
1:a1664788d34a
v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vermaelen 0:c188fa46ae96 1 #include "mbed.h"
vermaelen 0:c188fa46ae96 2 #include "MMA8451Q.h"
vermaelen 0:c188fa46ae96 3
vermaelen 0:c188fa46ae96 4 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
vermaelen 0:c188fa46ae96 5 PinName const SDA = PTE25;
vermaelen 0:c188fa46ae96 6 PinName const SCL = PTE24;
vermaelen 0:c188fa46ae96 7 #elif defined (TARGET_KL05Z)
vermaelen 0:c188fa46ae96 8 PinName const SDA = PTB4;
vermaelen 0:c188fa46ae96 9 PinName const SCL = PTB3;
vermaelen 0:c188fa46ae96 10 #elif defined (TARGET_K20D50M)
vermaelen 0:c188fa46ae96 11 PinName const SDA = PTB1;
vermaelen 0:c188fa46ae96 12 PinName const SCL = PTB0;
vermaelen 0:c188fa46ae96 13 #else
vermaelen 0:c188fa46ae96 14 #error TARGET NOT DEFINED
vermaelen 0:c188fa46ae96 15 #endif
vermaelen 0:c188fa46ae96 16
vermaelen 0:c188fa46ae96 17 #define MMA8451_I2C_ADDRESS (0x1d<<1)
vermaelen 0:c188fa46ae96 18
vermaelen 0:c188fa46ae96 19 int main(void)
vermaelen 0:c188fa46ae96 20 {
vermaelen 0:c188fa46ae96 21 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
vermaelen 0:c188fa46ae96 22 PwmOut rled(LED1);
vermaelen 0:c188fa46ae96 23 PwmOut gled(LED2);
vermaelen 0:c188fa46ae96 24 PwmOut bled(LED3);
vermaelen 0:c188fa46ae96 25
vermaelen 0:c188fa46ae96 26 printf("MMA8451 ID: %d\n", acc.getWhoAmI());
vermaelen 0:c188fa46ae96 27
vermaelen 0:c188fa46ae96 28 while (true) {
vermaelen 0:c188fa46ae96 29 float x, y, z;
vermaelen 0:c188fa46ae96 30 x = abs(acc.getAccX());
vermaelen 0:c188fa46ae96 31 y = abs(acc.getAccY());
vermaelen 0:c188fa46ae96 32 z = abs(acc.getAccZ());
vermaelen 0:c188fa46ae96 33 rled = 1.0f - x;
vermaelen 0:c188fa46ae96 34 gled = 1.0f - y;
vermaelen 0:c188fa46ae96 35 bled = 1.0f - z;
vermaelen 0:c188fa46ae96 36 wait(0.1f);
vermaelen 0:c188fa46ae96 37 printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
vermaelen 0:c188fa46ae96 38 }
vermaelen 0:c188fa46ae96 39 }