I2C Accelerometer code example for FRDM-KL25Z

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 PinName const SDA = PTE25;
00005 PinName const SCL = PTE24;
00006 
00007 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00008 
00009 int main(void) {
00010     MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
00011     PwmOut rled(LED1);
00012     PwmOut gled(LED2);
00013     PwmOut bled(LED3);
00014     
00015     printf("MMA8451 ID: %d\n", acc.getWhoAmI());
00016 
00017     while (true) 
00018     {
00019         float x, y, z;
00020         x = rled = 1.0 - abs(acc.getAccX());
00021         y = gled = 1.0 - abs(acc.getAccY());
00022         z = bled = 1.0 - abs(acc.getAccZ());
00023         wait(0.1);
00024         printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
00025     }
00026 }