modified from jppang's MMA8451Q for MMA8491Q on sensor evaluation board with the FRDM-KL25Z

Dependencies:   mbed

Fork of MMA8491Q by JP PANG

Committer:
DiyGuy
Date:
Fri Nov 29 23:14:21 2013 +0000
Revision:
3:3f5c717835c3
Parent:
2:b79d19bdfe10
Added main() example and exercises sensor EN input to trigger new readings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jppang 0:41db55eef564 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
jppang 0:41db55eef564 2 *
jppang 0:41db55eef564 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jppang 0:41db55eef564 4 * and associated documentation files (the "Software"), to deal in the Software without
jppang 0:41db55eef564 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
jppang 0:41db55eef564 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
jppang 0:41db55eef564 7 * Software is furnished to do so, subject to the following conditions:
jppang 0:41db55eef564 8 *
jppang 0:41db55eef564 9 * The above copyright notice and this permission notice shall be included in all copies or
jppang 0:41db55eef564 10 * substantial portions of the Software.
jppang 0:41db55eef564 11 *
jppang 0:41db55eef564 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jppang 0:41db55eef564 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jppang 0:41db55eef564 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jppang 0:41db55eef564 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jppang 0:41db55eef564 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jppang 0:41db55eef564 17 */
jppang 0:41db55eef564 18
jppang 0:41db55eef564 19 #ifndef MMA8491Q_H
jppang 0:41db55eef564 20 #define MMA8491Q_H
jppang 0:41db55eef564 21
jppang 0:41db55eef564 22 #include "mbed.h"
jppang 0:41db55eef564 23
jppang 0:41db55eef564 24 /**
jppang 0:41db55eef564 25 * MMA8491Q accelerometer example
jppang 0:41db55eef564 26 *
jppang 0:41db55eef564 27 * @code
jppang 0:41db55eef564 28 * #include "mbed.h"
jppang 0:41db55eef564 29 * #include "MMA8491Q.h"
jppang 0:41db55eef564 30 *
DiyGuy 3:3f5c717835c3 31 * #define MMA8451_I2C_ADDRESS (0x55<<1)
DiyGuy 3:3f5c717835c3 32 *
DiyGuy 3:3f5c717835c3 33 * Serial pc(USBTX, USBRX);
jppang 0:41db55eef564 34 *
jppang 0:41db55eef564 35 * int main(void) {
jppang 0:41db55eef564 36 *
DiyGuy 3:3f5c717835c3 37 * MMA8491Q acc(PTE0, PTE1, MMA8451_I2C_ADDRESS, PTA13);
DiyGuy 3:3f5c717835c3 38 *
DiyGuy 3:3f5c717835c3 39 * PwmOut rled(LED_RED);
DiyGuy 3:3f5c717835c3 40 * PwmOut gled(LED_GREEN);
DiyGuy 3:3f5c717835c3 41 * PwmOut bled(LED_BLUE);
DiyGuy 3:3f5c717835c3 42 *
DiyGuy 3:3f5c717835c3 43 * float accData[3];
DiyGuy 3:3f5c717835c3 44 *
DiyGuy 3:3f5c717835c3 45 * while (true) {
DiyGuy 3:3f5c717835c3 46 * acc.getAccAllAxis(accData);
DiyGuy 3:3f5c717835c3 47 * accX = accData[0];
DiyGuy 3:3f5c717835c3 48 * accY = accData[1];
DiyGuy 3:3f5c717835c3 49 * accZ = accData[2];
DiyGuy 3:3f5c717835c3 50 *
DiyGuy 3:3f5c717835c3 51 * pc.printf("X = %5.3f", accX);
DiyGuy 3:3f5c717835c3 52 * pc.printf(" Y = %5.3f", accY);
DiyGuy 3:3f5c717835c3 53 * pc.printf(" Z = %5.3f\r\n", accZ);
jppang 0:41db55eef564 54 *
DiyGuy 3:3f5c717835c3 55 * wait(1.0);
DiyGuy 3:3f5c717835c3 56 * }
DiyGuy 3:3f5c717835c3 57 *}
DiyGuy 3:3f5c717835c3 58 *
jppang 0:41db55eef564 59 * @endcode
jppang 0:41db55eef564 60 */
DiyGuy 3:3f5c717835c3 61
jppang 0:41db55eef564 62 class MMA8491Q
jppang 0:41db55eef564 63 {
jppang 0:41db55eef564 64 public:
jppang 0:41db55eef564 65 /**
jppang 0:41db55eef564 66 * MMA8491Q constructor
jppang 0:41db55eef564 67 *
jppang 0:41db55eef564 68 * @param sda SDA pin
jppang 0:41db55eef564 69 * @param sdl SCL pin
jppang 0:41db55eef564 70 * @param addr addr of the I2C peripheral
DiyGuy 2:b79d19bdfe10 71 * @param en EN pin
DiyGuy 2:b79d19bdfe10 72 * @param Xout digital output pin
DiyGuy 2:b79d19bdfe10 73 * @param Yout digital output pin
DiyGuy 2:b79d19bdfe10 74 * @param Zout digital output pin
jppang 0:41db55eef564 75 */
DiyGuy 2:b79d19bdfe10 76 MMA8491Q(PinName sda, PinName scl, int addr, PinName en);
jppang 1:2d4b7fadb1e6 77
DiyGuy 2:b79d19bdfe10 78 MMA8491Q(PinName sda, PinName scl, int addr, PinName en, PinName Xout, PinName Yout, PinName Zout);
jppang 0:41db55eef564 79
jppang 0:41db55eef564 80 /**
jppang 0:41db55eef564 81 * MMA8491Q destructor
jppang 0:41db55eef564 82 */
jppang 0:41db55eef564 83 ~MMA8491Q();
jppang 0:41db55eef564 84
jppang 0:41db55eef564 85 /**
jppang 0:41db55eef564 86 * Get X axis acceleration
jppang 0:41db55eef564 87 *
jppang 0:41db55eef564 88 * @returns X axis acceleration
jppang 0:41db55eef564 89 */
jppang 0:41db55eef564 90 float getAccX();
jppang 0:41db55eef564 91
jppang 0:41db55eef564 92 /**
jppang 0:41db55eef564 93 * Get Y axis acceleration
jppang 0:41db55eef564 94 *
jppang 0:41db55eef564 95 * @returns Y axis acceleration
jppang 0:41db55eef564 96 */
jppang 0:41db55eef564 97 float getAccY();
jppang 0:41db55eef564 98
jppang 0:41db55eef564 99 /**
jppang 0:41db55eef564 100 * Get Z axis acceleration
jppang 0:41db55eef564 101 *
jppang 0:41db55eef564 102 * @returns Z axis acceleration
jppang 0:41db55eef564 103 */
jppang 0:41db55eef564 104 float getAccZ();
jppang 0:41db55eef564 105
jppang 0:41db55eef564 106 /**
jppang 0:41db55eef564 107 * Get XYZ axis acceleration
jppang 0:41db55eef564 108 *
jppang 0:41db55eef564 109 * @param res array where acceleration data will be stored
DiyGuy 2:b79d19bdfe10 110 * @returns true if sensor was ready and data was read
jppang 0:41db55eef564 111 */
DiyGuy 2:b79d19bdfe10 112 uint8_t getAccAllAxis(float * res);
jppang 0:41db55eef564 113
jppang 0:41db55eef564 114
jppang 0:41db55eef564 115 private:
jppang 0:41db55eef564 116 I2C m_i2c;
DiyGuy 2:b79d19bdfe10 117 int m_addr; //MMA8491Q address
DiyGuy 2:b79d19bdfe10 118 DigitalOut m_en; //MMA8491Q ENABLE pin
DiyGuy 2:b79d19bdfe10 119 PinName m_xout;
DiyGuy 2:b79d19bdfe10 120 PinName m_yout;
DiyGuy 2:b79d19bdfe10 121 PinName m_zout;
DiyGuy 2:b79d19bdfe10 122 uint8_t isDataAvailable( uint8_t mask);
DiyGuy 2:b79d19bdfe10 123 float intAccToFloat(uint8_t * data);
DiyGuy 2:b79d19bdfe10 124 void toggleEN(void);
jppang 0:41db55eef564 125 void readRegs(int addr, uint8_t * data, int len);
jppang 0:41db55eef564 126 void writeRegs(uint8_t * data, int len);
jppang 0:41db55eef564 127 int16_t getAccAxis(uint8_t addr);
jppang 1:2d4b7fadb1e6 128
jppang 0:41db55eef564 129
jppang 0:41db55eef564 130 };
jppang 0:41db55eef564 131
jppang 0:41db55eef564 132 #endif