embed Grove 3-Axis Digital Accelerometer

Dependencies:   MMA7660FC mbed

Fork of TestCode_MMA7660FC by Edoardo De Marchi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Author: Edoardo De Marchi 
00002  * Name: Test Code for MMA7660FC
00003  */
00004 #include "mbed.h"
00005 #include "MMA7660FC.h"
00006 
00007 DigitalOut myled1(LED1);
00008 DigitalOut myled2(LED2);
00009 DigitalOut myled3(LED3);
00010 DigitalOut myled4(LED4);
00011 
00012 #define ADDR_MMA7660 0x98                   // I2C SLAVE ADDR MMA7660FC
00013 
00014 MMA7660FC Acc(p28, p27, ADDR_MMA7660);      //sda, scl, Addr
00015 Serial pc(USBTX, USBRX);
00016 
00017 
00018 float G_VALUE[64] = {0, 0.047, 0.094, 0.141, 0.188, 0.234, 0.281, 0.328, 0.375, 0.422, 0.469, 0.516, 0.563, 0.609, 0.656, 0.703, 0.750, 0.797, 0.844, 0.891, 0.938, 0.984, 1.031, 1.078, 1.125, 1.172, 1.219, 1.266, 1.313, 1.359, 1.406, 1.453, -1.500, -1.453, -1.406, -1.359, -1.313, -1.266, -1.219, -1.172, -1.125, -1.078, -1.031, -0.984, -0.938, -0.891, -0.844, -0.797, -0.750, -0.703, -0.656, -0.609, -0.563, -0.516, -0.469, -0.422, -0.375, -0.328, -0.281, -0.234, -0.188, -0.141, -0.094, -0.047};
00019 
00020 
00021 
00022 int main() 
00023 {
00024 
00025     Acc.init();                                                     // Initialization
00026     pc.printf("Value reg 0x06: %#x\n", Acc.read_reg(0x06));         // Test the correct value of the register 0x06
00027     pc.printf("Value reg 0x08: %#x\n", Acc.read_reg(0x08));         // Test the correct value of the register 0x08
00028     pc.printf("Value reg 0x07: %#x\n\r", Acc.read_reg(0x07));       // Test the correct value of the register 0x07
00029            
00030     while(1)
00031     {   
00032         myled4=1;
00033         float x=0, y=0, z=0;
00034         float ax=0, ay=0, az=0;
00035         
00036         Acc.read_Tilt(&x, &y, &z);                                  // Read the acceleration                    
00037         pc.printf("Tilt x: %2.2f degree \n", x);                    // Print the tilt orientation of the X axis
00038         pc.printf("Tilt y: %2.2f degree \n", y);                    // Print the tilt orientation of the Y axis
00039         pc.printf("Tilt z: %2.2f degree \n", z);                    // Print the tilt orientation of the Z axis
00040 
00041         wait_ms(100);
00042 
00043         pc.printf("x: %1.3f g \n", G_VALUE[Acc.read_x()]);          // Print the X axis acceleration
00044         pc.printf("y: %1.3f g \n", G_VALUE[Acc.read_y()]);          // Print the Y axis acceleration
00045         pc.printf("z: %1.3f g \n", G_VALUE[Acc.read_z()]);          // Print the Z axis acceleration
00046         pc.printf("\n");
00047         
00048         ax = G_VALUE[Acc.read_x()];
00049         ay = G_VALUE[Acc.read_y()];
00050         az = G_VALUE[Acc.read_z()];
00051         
00052         if(ax<0){
00053             myled1=1;
00054         }else{
00055             myled1=0; 
00056         }
00057         if(ay<0){
00058             myled2=1;
00059         }else{
00060             myled2=0; 
00061         }
00062         if(az<0){
00063             myled3=1;
00064         }else{
00065             myled3=0; 
00066         }
00067         
00068         wait(1);
00069           
00070     }
00071 }
00072