3-Axis Digital Accelerometer is the key part in projects like orientation detection, gesture detection and Motion detection. This 3-Asix Digital Accelerometer(±1.5g) is based on Freescale's low power consumption module, MMA7660FC. It features up to 10,000g high shock surviability and configurable Samples per Second rate. For generous applications that don't require too large measurement range, this is a great choice because it's durable, energy saving and cost-efficient.

Fork of Grove_3-Axis_Digital_Accelerometer_MMA7660FC_Library by Austin Blackstone

Committer:
mbedAustin
Date:
Fri Sep 05 23:14:35 2014 +0000
Revision:
0:cc40c3196635
Child:
1:08f271727b67
Grove 3axis digital accelorometer with library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:cc40c3196635 1 /*
mbedAustin 0:cc40c3196635 2 * MMA7760.h
mbedAustin 0:cc40c3196635 3 * Library for accelerometer_MMA7760
mbedAustin 0:cc40c3196635 4 *
mbedAustin 0:cc40c3196635 5 * Copyright (c) 2013 seeed technology inc.
mbedAustin 0:cc40c3196635 6 * Author : FrankieChu
mbedAustin 0:cc40c3196635 7 * Create Time : Jan 2013
mbedAustin 0:cc40c3196635 8 * Change Log :
mbedAustin 0:cc40c3196635 9 *
mbedAustin 0:cc40c3196635 10 * The MIT License (MIT)
mbedAustin 0:cc40c3196635 11 *
mbedAustin 0:cc40c3196635 12 * Permission is hereby granted, free of charge, to any person obtaining a copy
mbedAustin 0:cc40c3196635 13 * of this software and associated documentation files (the "Software"), to deal
mbedAustin 0:cc40c3196635 14 * in the Software without restriction, including without limitation the rights
mbedAustin 0:cc40c3196635 15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mbedAustin 0:cc40c3196635 16 * copies of the Software, and to permit persons to whom the Software is
mbedAustin 0:cc40c3196635 17 * furnished to do so, subject to the following conditions:
mbedAustin 0:cc40c3196635 18 *
mbedAustin 0:cc40c3196635 19 * The above copyright notice and this permission notice shall be included in
mbedAustin 0:cc40c3196635 20 * all copies or substantial portions of the Software.
mbedAustin 0:cc40c3196635 21 *
mbedAustin 0:cc40c3196635 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mbedAustin 0:cc40c3196635 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mbedAustin 0:cc40c3196635 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mbedAustin 0:cc40c3196635 25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mbedAustin 0:cc40c3196635 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbedAustin 0:cc40c3196635 27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mbedAustin 0:cc40c3196635 28 * THE SOFTWARE.
mbedAustin 0:cc40c3196635 29 */
mbedAustin 0:cc40c3196635 30
mbedAustin 0:cc40c3196635 31 #include "MMA7660.h"
mbedAustin 0:cc40c3196635 32
mbedAustin 0:cc40c3196635 33 I2C i2c(I2C_SDA,I2C_SCL);
mbedAustin 0:cc40c3196635 34
mbedAustin 0:cc40c3196635 35 /*Function: Write a byte to the register of the MMA7660*/
mbedAustin 0:cc40c3196635 36 void MMA7660::write(uint8_t _register, uint8_t _data)
mbedAustin 0:cc40c3196635 37 {
mbedAustin 0:cc40c3196635 38 char data[2] = {_register,_data};
mbedAustin 0:cc40c3196635 39 int check;
mbedAustin 0:cc40c3196635 40 //Wire.begin();
mbedAustin 0:cc40c3196635 41 //Wire.beginTransmission(MMA7660_ADDR);
mbedAustin 0:cc40c3196635 42 //Wire.write(_register);
mbedAustin 0:cc40c3196635 43 //Wire.write(_data);
mbedAustin 0:cc40c3196635 44 //Wire.endTransmission();
mbedAustin 0:cc40c3196635 45 check = i2c.write(MMA7660_ADDR<<1,data,2);
mbedAustin 0:cc40c3196635 46 if(check != 0)
mbedAustin 0:cc40c3196635 47 printf("I2C Write Failure\n\r");
mbedAustin 0:cc40c3196635 48 }
mbedAustin 0:cc40c3196635 49 /*Function: Read a byte from the regitster of the MMA7660*/
mbedAustin 0:cc40c3196635 50 uint8_t MMA7660::read(uint8_t _register)
mbedAustin 0:cc40c3196635 51 {
mbedAustin 0:cc40c3196635 52 char data_read[2] = {_register,0};
mbedAustin 0:cc40c3196635 53 int check;
mbedAustin 0:cc40c3196635 54 check = i2c.write(MMA7660_ADDR<<1,data_read,1); // write _register to set up read
mbedAustin 0:cc40c3196635 55 check = i2c.read(MMA7660_ADDR<<1,data_read,2); // execute read
mbedAustin 0:cc40c3196635 56 if(check != 0)
mbedAustin 0:cc40c3196635 57 printf("I2C Read failure\n\r");
mbedAustin 0:cc40c3196635 58 printf("read back data 0x%x, 0x%x from register 0x%x",data_read[0],data_read[1],_register);
mbedAustin 0:cc40c3196635 59 //uint8_t data_read;
mbedAustin 0:cc40c3196635 60 //Wire.begin();
mbedAustin 0:cc40c3196635 61 //Wire.beginTransmission(MMA7660_ADDR);
mbedAustin 0:cc40c3196635 62 //Wire.write(_register);
mbedAustin 0:cc40c3196635 63 //Wire.endTransmission();
mbedAustin 0:cc40c3196635 64 //Wire.beginTransmission(MMA7660_ADDR);
mbedAustin 0:cc40c3196635 65 //Wire.requestFrom(MMA7660_ADDR,1);
mbedAustin 0:cc40c3196635 66 //while(Wire.available())
mbedAustin 0:cc40c3196635 67 //{
mbedAustin 0:cc40c3196635 68 // data_read = Wire.read();
mbedAustin 0:cc40c3196635 69 //}
mbedAustin 0:cc40c3196635 70 //Wire.endTransmission();
mbedAustin 0:cc40c3196635 71 return data_read[1];
mbedAustin 0:cc40c3196635 72 }
mbedAustin 0:cc40c3196635 73
mbedAustin 0:cc40c3196635 74 void MMA7660::init()
mbedAustin 0:cc40c3196635 75 {
mbedAustin 0:cc40c3196635 76 i2c.frequency(400000);
mbedAustin 0:cc40c3196635 77 setMode(MMA7660_STAND_BY);
mbedAustin 0:cc40c3196635 78 setSampleRate(AUTO_SLEEP_32);
mbedAustin 0:cc40c3196635 79 setMode(MMA7660_ACTIVE);
mbedAustin 0:cc40c3196635 80 }
mbedAustin 0:cc40c3196635 81 void MMA7660::setMode(uint8_t mode)
mbedAustin 0:cc40c3196635 82 {
mbedAustin 0:cc40c3196635 83 write(MMA7660_MODE,mode);
mbedAustin 0:cc40c3196635 84 }
mbedAustin 0:cc40c3196635 85 void MMA7660::setSampleRate(uint8_t rate)
mbedAustin 0:cc40c3196635 86 {
mbedAustin 0:cc40c3196635 87 write(MMA7660_SR,rate);
mbedAustin 0:cc40c3196635 88 }
mbedAustin 0:cc40c3196635 89 /*Function: Get the contents of the registers in the MMA7660*/
mbedAustin 0:cc40c3196635 90 /* so as to calculate the acceleration. */
mbedAustin 0:cc40c3196635 91 void MMA7660::getXYZ(int8_t *x,int8_t *y,int8_t *z)
mbedAustin 0:cc40c3196635 92 {
mbedAustin 0:cc40c3196635 93 char val[3];
mbedAustin 0:cc40c3196635 94 int count = 0;
mbedAustin 0:cc40c3196635 95 bool done = false;
mbedAustin 0:cc40c3196635 96 val[0] = val[1] = val[2] = 64;
mbedAustin 0:cc40c3196635 97 // while(Wire.available() > 0)
mbedAustin 0:cc40c3196635 98 // Wire.read();
mbedAustin 0:cc40c3196635 99 // Wire.requestFrom(MMA7660_ADDR,3);
mbedAustin 0:cc40c3196635 100 // while(Wire.available())
mbedAustin 0:cc40c3196635 101 // {
mbedAustin 0:cc40c3196635 102 // if(count < 3)
mbedAustin 0:cc40c3196635 103 // {
mbedAustin 0:cc40c3196635 104 // while ( val[count] > 63 ) // reload the damn thing it is bad
mbedAustin 0:cc40c3196635 105 // {
mbedAustin 0:cc40c3196635 106 // val[count] = Wire.read();
mbedAustin 0:cc40c3196635 107 // }
mbedAustin 0:cc40c3196635 108 // }
mbedAustin 0:cc40c3196635 109 // count++;
mbedAustin 0:cc40c3196635 110 // }
mbedAustin 0:cc40c3196635 111 for(count = 0; count < 3 && done == false; count ++){
mbedAustin 0:cc40c3196635 112 i2c.read(MMA7660_ADDR<<1, val,3);
mbedAustin 0:cc40c3196635 113 if(val[0] < 63 && val[1]<63 && val[2]<63)
mbedAustin 0:cc40c3196635 114 done = true;
mbedAustin 0:cc40c3196635 115 }
mbedAustin 0:cc40c3196635 116
mbedAustin 0:cc40c3196635 117 *x = ((char)(val[0]<<2))/4;
mbedAustin 0:cc40c3196635 118 *y = ((char)(val[1]<<2))/4;
mbedAustin 0:cc40c3196635 119 *z = ((char)(val[2]<<2))/4;
mbedAustin 0:cc40c3196635 120 }
mbedAustin 0:cc40c3196635 121
mbedAustin 0:cc40c3196635 122 void MMA7660::getAcceleration(float *ax,float *ay,float *az)
mbedAustin 0:cc40c3196635 123 {
mbedAustin 0:cc40c3196635 124 int8_t x,y,z;
mbedAustin 0:cc40c3196635 125 getXYZ(&x,&y,&z);
mbedAustin 0:cc40c3196635 126 *ax = x/21.00;
mbedAustin 0:cc40c3196635 127 *ay = y/21.00;
mbedAustin 0:cc40c3196635 128 *az = z/21.00;
mbedAustin 0:cc40c3196635 129 }