angle

Dependencies:   MMA8451Q mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MMA8451Q.h"
00003 
00004 #if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
00005   PinName const SDA = PTE25;
00006   PinName const SCL = PTE24;
00007 #elif defined (TARGET_KL05Z)
00008   PinName const SDA = PTB4;
00009   PinName const SCL = PTB3;
00010 #elif defined (TARGET_K20D50M)
00011   PinName const SDA = PTB1;
00012   PinName const SCL = PTB0;
00013 #else
00014   #error TARGET NOT DEFINED
00015 #endif
00016 
00017 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00018 Serial pc(USBTX, USBRX);
00019 
00020 int main(void)
00021 {
00022     MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
00023     
00024 
00025     printf("MMA8451 ID: %d\n", acc.getWhoAmI());
00026 
00027     while (true) {
00028         float x, y, z;
00029         x = abs(acc.getAccX());
00030         y = abs(acc.getAccY());
00031         z = abs(acc.getAccZ());
00032        
00033         pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", 180*asin(x/sqrt(x*x+y*y+z*z))/ 3.14159, 180*asin(y/sqrt(x*x+y*y+z*z))/ 3.14159, 180*asin(z/sqrt(x*x+y*y+z*z))/ 3.14159);
00034     }
00035 }