embed Grove 3-Axis Digital Accelerometer

Dependencies:   MMA7660FC mbed

Fork of TestCode_MMA7660FC by Edoardo De Marchi

Committer:
s890506
Date:
Wed Feb 04 04:54:16 2015 +0000
Revision:
3:3c1ca823b659
Parent:
2:152191e37eb1
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edodm85 0:a006e8002089 1 /* Author: Edoardo De Marchi
edodm85 0:a006e8002089 2 * Name: Test Code for MMA7660FC
edodm85 0:a006e8002089 3 */
edodm85 0:a006e8002089 4 #include "mbed.h"
edodm85 0:a006e8002089 5 #include "MMA7660FC.h"
edodm85 0:a006e8002089 6
s890506 3:3c1ca823b659 7 DigitalOut myled1(LED1);
s890506 3:3c1ca823b659 8 DigitalOut myled2(LED2);
s890506 3:3c1ca823b659 9 DigitalOut myled3(LED3);
s890506 3:3c1ca823b659 10 DigitalOut myled4(LED4);
s890506 3:3c1ca823b659 11
edodm85 1:78138640bd0e 12 #define ADDR_MMA7660 0x98 // I2C SLAVE ADDR MMA7660FC
edodm85 0:a006e8002089 13
edodm85 1:78138640bd0e 14 MMA7660FC Acc(p28, p27, ADDR_MMA7660); //sda, scl, Addr
edodm85 0:a006e8002089 15 Serial pc(USBTX, USBRX);
edodm85 0:a006e8002089 16
edodm85 0:a006e8002089 17
edodm85 0:a006e8002089 18 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};
edodm85 0:a006e8002089 19
edodm85 0:a006e8002089 20
edodm85 0:a006e8002089 21
edodm85 0:a006e8002089 22 int main()
edodm85 0:a006e8002089 23 {
edodm85 0:a006e8002089 24
edodm85 0:a006e8002089 25 Acc.init(); // Initialization
edodm85 0:a006e8002089 26 pc.printf("Value reg 0x06: %#x\n", Acc.read_reg(0x06)); // Test the correct value of the register 0x06
edodm85 0:a006e8002089 27 pc.printf("Value reg 0x08: %#x\n", Acc.read_reg(0x08)); // Test the correct value of the register 0x08
edodm85 0:a006e8002089 28 pc.printf("Value reg 0x07: %#x\n\r", Acc.read_reg(0x07)); // Test the correct value of the register 0x07
edodm85 0:a006e8002089 29
edodm85 0:a006e8002089 30 while(1)
edodm85 0:a006e8002089 31 {
s890506 3:3c1ca823b659 32 myled4=1;
edodm85 1:78138640bd0e 33 float x=0, y=0, z=0;
s890506 3:3c1ca823b659 34 float ax=0, ay=0, az=0;
edodm85 0:a006e8002089 35
edodm85 2:152191e37eb1 36 Acc.read_Tilt(&x, &y, &z); // Read the acceleration
edodm85 1:78138640bd0e 37 pc.printf("Tilt x: %2.2f degree \n", x); // Print the tilt orientation of the X axis
edodm85 1:78138640bd0e 38 pc.printf("Tilt y: %2.2f degree \n", y); // Print the tilt orientation of the Y axis
edodm85 1:78138640bd0e 39 pc.printf("Tilt z: %2.2f degree \n", z); // Print the tilt orientation of the Z axis
edodm85 0:a006e8002089 40
edodm85 1:78138640bd0e 41 wait_ms(100);
edodm85 1:78138640bd0e 42
edodm85 2:152191e37eb1 43 pc.printf("x: %1.3f g \n", G_VALUE[Acc.read_x()]); // Print the X axis acceleration
edodm85 2:152191e37eb1 44 pc.printf("y: %1.3f g \n", G_VALUE[Acc.read_y()]); // Print the Y axis acceleration
edodm85 2:152191e37eb1 45 pc.printf("z: %1.3f g \n", G_VALUE[Acc.read_z()]); // Print the Z axis acceleration
edodm85 0:a006e8002089 46 pc.printf("\n");
s890506 3:3c1ca823b659 47
s890506 3:3c1ca823b659 48 ax = G_VALUE[Acc.read_x()];
s890506 3:3c1ca823b659 49 ay = G_VALUE[Acc.read_y()];
s890506 3:3c1ca823b659 50 az = G_VALUE[Acc.read_z()];
s890506 3:3c1ca823b659 51
s890506 3:3c1ca823b659 52 if(ax<0){
s890506 3:3c1ca823b659 53 myled1=1;
s890506 3:3c1ca823b659 54 }else{
s890506 3:3c1ca823b659 55 myled1=0;
s890506 3:3c1ca823b659 56 }
s890506 3:3c1ca823b659 57 if(ay<0){
s890506 3:3c1ca823b659 58 myled2=1;
s890506 3:3c1ca823b659 59 }else{
s890506 3:3c1ca823b659 60 myled2=0;
s890506 3:3c1ca823b659 61 }
s890506 3:3c1ca823b659 62 if(az<0){
s890506 3:3c1ca823b659 63 myled3=1;
s890506 3:3c1ca823b659 64 }else{
s890506 3:3c1ca823b659 65 myled3=0;
s890506 3:3c1ca823b659 66 }
s890506 3:3c1ca823b659 67
s890506 3:3c1ca823b659 68 wait(1);
edodm85 0:a006e8002089 69
edodm85 0:a006e8002089 70 }
edodm85 0:a006e8002089 71 }
edodm85 0:a006e8002089 72