Modified to also return int16_t values.
Fork of MMA8451Q by
MMA8451Q.cpp@6:2511055a5dd2, 2013-06-07 (annotated)
- Committer:
- jhestolano
- Date:
- Fri Jun 07 03:14:46 2013 +0000
- Revision:
- 6:2511055a5dd2
- Parent:
- 5:2d14600116fc
Modified to return int16_t values instead of float type.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:d2630136d51e | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
samux | 1:d2630136d51e | 2 | * |
samux | 1:d2630136d51e | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:d2630136d51e | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
samux | 1:d2630136d51e | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
samux | 1:d2630136d51e | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
samux | 1:d2630136d51e | 7 | * Software is furnished to do so, subject to the following conditions: |
samux | 1:d2630136d51e | 8 | * |
samux | 1:d2630136d51e | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:d2630136d51e | 10 | * substantial portions of the Software. |
samux | 1:d2630136d51e | 11 | * |
samux | 1:d2630136d51e | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:d2630136d51e | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:d2630136d51e | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:d2630136d51e | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:d2630136d51e | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:d2630136d51e | 17 | */ |
samux | 1:d2630136d51e | 18 | |
emilmont | 0:6149091f755d | 19 | #include "MMA8451Q.h" |
emilmont | 0:6149091f755d | 20 | |
JoKer | 5:2d14600116fc | 21 | #define INT_SOURCE 0x0C |
samux | 1:d2630136d51e | 22 | #define REG_WHO_AM_I 0x0D |
JoKer | 5:2d14600116fc | 23 | #define HP_FILTER_CUTOFF 0x0F |
JoKer | 5:2d14600116fc | 24 | #define PULSE_CFG 0x21 |
JoKer | 5:2d14600116fc | 25 | #define PULSE_SRC 0x22 |
JoKer | 5:2d14600116fc | 26 | #define PULSE_THSX 0x23 |
JoKer | 5:2d14600116fc | 27 | #define PULSE_THSY 0x24 |
JoKer | 5:2d14600116fc | 28 | #define PULSE_THSZ 0x25 |
JoKer | 5:2d14600116fc | 29 | #define PULSE_TMLT 0x26 |
JoKer | 5:2d14600116fc | 30 | #define PULSE_LTCY 0x27 |
JoKer | 5:2d14600116fc | 31 | #define PULSE_WIND 0x28 |
JoKer | 5:2d14600116fc | 32 | #define REG_CTRL_REG_1 0x2A |
JoKer | 5:2d14600116fc | 33 | #define CTRL_REG2 0x2B |
JoKer | 5:2d14600116fc | 34 | #define CTRL_REG4 0x2D |
JoKer | 5:2d14600116fc | 35 | #define CTRL_REG5 0x2E |
emilmont | 0:6149091f755d | 36 | #define REG_OUT_X_MSB 0x01 |
emilmont | 0:6149091f755d | 37 | #define REG_OUT_Y_MSB 0x03 |
emilmont | 0:6149091f755d | 38 | #define REG_OUT_Z_MSB 0x05 |
emilmont | 0:6149091f755d | 39 | |
samux | 1:d2630136d51e | 40 | #define UINT14_MAX 16383 |
emilmont | 0:6149091f755d | 41 | |
emilmont | 0:6149091f755d | 42 | MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
emilmont | 0:6149091f755d | 43 | // activate the peripheral |
emilmont | 0:6149091f755d | 44 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
samux | 1:d2630136d51e | 45 | writeRegs(data, 2); |
emilmont | 0:6149091f755d | 46 | } |
emilmont | 0:6149091f755d | 47 | |
emilmont | 0:6149091f755d | 48 | MMA8451Q::~MMA8451Q() { } |
emilmont | 0:6149091f755d | 49 | |
emilmont | 0:6149091f755d | 50 | uint8_t MMA8451Q::getWhoAmI() { |
emilmont | 0:6149091f755d | 51 | uint8_t who_am_i = 0; |
samux | 1:d2630136d51e | 52 | readRegs(REG_WHO_AM_I, &who_am_i, 1); |
emilmont | 0:6149091f755d | 53 | return who_am_i; |
emilmont | 0:6149091f755d | 54 | } |
emilmont | 0:6149091f755d | 55 | |
chris | 3:db7126dbd63f | 56 | float MMA8451Q::getAccX() { |
JoKer | 5:2d14600116fc | 57 | //divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity) |
chris | 3:db7126dbd63f | 58 | return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); |
emilmont | 0:6149091f755d | 59 | } |
emilmont | 0:6149091f755d | 60 | |
chris | 3:db7126dbd63f | 61 | float MMA8451Q::getAccY() { |
chris | 3:db7126dbd63f | 62 | return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); |
emilmont | 0:6149091f755d | 63 | } |
emilmont | 0:6149091f755d | 64 | |
chris | 3:db7126dbd63f | 65 | float MMA8451Q::getAccZ() { |
chris | 3:db7126dbd63f | 66 | return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); |
emilmont | 0:6149091f755d | 67 | } |
emilmont | 0:6149091f755d | 68 | |
chris | 3:db7126dbd63f | 69 | void MMA8451Q::getAccAllAxis(float * res) { |
emilmont | 0:6149091f755d | 70 | res[0] = getAccX(); |
emilmont | 0:6149091f755d | 71 | res[1] = getAccY(); |
emilmont | 0:6149091f755d | 72 | res[2] = getAccZ(); |
emilmont | 0:6149091f755d | 73 | } |
emilmont | 0:6149091f755d | 74 | |
jhestolano | 6:2511055a5dd2 | 75 | int16_t MMA8451Q::getAccX_int() { |
jhestolano | 6:2511055a5dd2 | 76 | return getAccAxis(REG_OUT_X_MSB); |
jhestolano | 6:2511055a5dd2 | 77 | } |
jhestolano | 6:2511055a5dd2 | 78 | |
jhestolano | 6:2511055a5dd2 | 79 | int16_t MMA8451Q::getAccY_int() { |
jhestolano | 6:2511055a5dd2 | 80 | return getAccAxis(REG_OUT_Y_MSB); |
jhestolano | 6:2511055a5dd2 | 81 | } |
jhestolano | 6:2511055a5dd2 | 82 | |
jhestolano | 6:2511055a5dd2 | 83 | int16_t MMA8451Q::getAccZ_int() { |
jhestolano | 6:2511055a5dd2 | 84 | return getAccAxis(REG_OUT_Z_MSB); |
jhestolano | 6:2511055a5dd2 | 85 | } |
jhestolano | 6:2511055a5dd2 | 86 | |
jhestolano | 6:2511055a5dd2 | 87 | void MMA8451Q::getAccAllAxis_int(int16_t* res) { |
jhestolano | 6:2511055a5dd2 | 88 | res[0] = getAccX_int(); |
jhestolano | 6:2511055a5dd2 | 89 | res[1] = getAccY_int(); |
jhestolano | 6:2511055a5dd2 | 90 | res[2] = getAccZ_int(); |
jhestolano | 6:2511055a5dd2 | 91 | } |
jhestolano | 6:2511055a5dd2 | 92 | |
emilmont | 0:6149091f755d | 93 | int16_t MMA8451Q::getAccAxis(uint8_t addr) { |
emilmont | 0:6149091f755d | 94 | int16_t acc; |
emilmont | 0:6149091f755d | 95 | uint8_t res[2]; |
samux | 1:d2630136d51e | 96 | readRegs(addr, res, 2); |
emilmont | 0:6149091f755d | 97 | |
emilmont | 0:6149091f755d | 98 | acc = (res[0] << 6) | (res[1] >> 2); |
emilmont | 0:6149091f755d | 99 | if (acc > UINT14_MAX/2) |
emilmont | 0:6149091f755d | 100 | acc -= UINT14_MAX; |
emilmont | 0:6149091f755d | 101 | |
emilmont | 0:6149091f755d | 102 | return acc; |
emilmont | 0:6149091f755d | 103 | } |
emilmont | 0:6149091f755d | 104 | |
JoKer | 5:2d14600116fc | 105 | void MMA8451Q::setDoubleTap(void){ |
JoKer | 5:2d14600116fc | 106 | //Implemented directly from Freescale's AN4072 |
JoKer | 5:2d14600116fc | 107 | //Added to MMA8451Q lib |
JoKer | 5:2d14600116fc | 108 | |
JoKer | 5:2d14600116fc | 109 | uint8_t CTRL_REG1_Data; |
JoKer | 5:2d14600116fc | 110 | // int adds; |
JoKer | 5:2d14600116fc | 111 | uint8_t data[2] = {REG_CTRL_REG_1, 0x08}; |
JoKer | 5:2d14600116fc | 112 | |
JoKer | 5:2d14600116fc | 113 | //400 Hz, Standby Mode |
JoKer | 5:2d14600116fc | 114 | writeRegs(data,2); |
JoKer | 5:2d14600116fc | 115 | |
JoKer | 5:2d14600116fc | 116 | //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort |
JoKer | 5:2d14600116fc | 117 | data[0]=PULSE_CFG;data[1]=0x2A; |
JoKer | 5:2d14600116fc | 118 | writeRegs(data,2); |
JoKer | 5:2d14600116fc | 119 | |
JoKer | 5:2d14600116fc | 120 | //SetThreshold 3g on X and Y and 5g on Z |
JoKer | 5:2d14600116fc | 121 | //Note: Every step is 0.063g |
JoKer | 5:2d14600116fc | 122 | //3 g/0.063g = 48 counts |
JoKer | 5:2d14600116fc | 123 | //5g/0.063g = 79 counts |
JoKer | 5:2d14600116fc | 124 | data[0]=PULSE_THSX;data[1]=0x30; |
JoKer | 5:2d14600116fc | 125 | writeRegs(data,2);//Set X Threshold to 3g |
JoKer | 5:2d14600116fc | 126 | data[0]=PULSE_THSY;data[1]=0x30; |
JoKer | 5:2d14600116fc | 127 | writeRegs(data,2);//Set Y Threshold to 3g |
JoKer | 5:2d14600116fc | 128 | data[0]=PULSE_THSZ;data[1]=0x4F; |
JoKer | 5:2d14600116fc | 129 | writeRegs(data,2);//Set Z Threshold to 5g |
JoKer | 5:2d14600116fc | 130 | |
JoKer | 5:2d14600116fc | 131 | //Set Time Limit for Tap Detection to 60 ms LP Mode |
JoKer | 5:2d14600116fc | 132 | //Note: 400 Hz ODR, Time step is 1.25 ms per step |
JoKer | 5:2d14600116fc | 133 | //60 ms/1.25 ms = 48 counts |
JoKer | 5:2d14600116fc | 134 | data[0]=PULSE_TMLT;data[1]=0x30; |
JoKer | 5:2d14600116fc | 135 | writeRegs(data,2);//60 ms |
JoKer | 5:2d14600116fc | 136 | |
JoKer | 5:2d14600116fc | 137 | //Set Latency Time to 200 ms |
JoKer | 5:2d14600116fc | 138 | //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts |
JoKer | 5:2d14600116fc | 139 | data[0]=PULSE_LTCY;data[1]=0x50; |
JoKer | 5:2d14600116fc | 140 | writeRegs(data,2);//200 ms |
JoKer | 5:2d14600116fc | 141 | |
JoKer | 5:2d14600116fc | 142 | //Set Time Window for second tap to 300 ms |
JoKer | 5:2d14600116fc | 143 | //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step |
JoKer | 5:2d14600116fc | 144 | //300 ms/2.5 ms = 120 counts |
JoKer | 5:2d14600116fc | 145 | data[0]=PULSE_WIND;data[1]=0x78; |
JoKer | 5:2d14600116fc | 146 | writeRegs(data,2);//300 ms |
JoKer | 5:2d14600116fc | 147 | |
JoKer | 5:2d14600116fc | 148 | //Route INT1 to System Interrupt |
JoKer | 5:2d14600116fc | 149 | data[0]=CTRL_REG4;data[1]=0x08; |
JoKer | 5:2d14600116fc | 150 | writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4 |
JoKer | 5:2d14600116fc | 151 | data[0]=CTRL_REG5;data[1]=0x08; |
JoKer | 5:2d14600116fc | 152 | writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5 |
JoKer | 5:2d14600116fc | 153 | |
JoKer | 5:2d14600116fc | 154 | //Set the device to Active Mode |
JoKer | 5:2d14600116fc | 155 | readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register |
JoKer | 5:2d14600116fc | 156 | CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode. |
JoKer | 5:2d14600116fc | 157 | data[0]=REG_CTRL_REG_1; |
JoKer | 5:2d14600116fc | 158 | data[1]=CTRL_REG1_Data; |
JoKer | 5:2d14600116fc | 159 | writeRegs(data,2);//Write in the updated value to put the device in Active Mode |
JoKer | 5:2d14600116fc | 160 | } |
JoKer | 5:2d14600116fc | 161 | |
JoKer | 5:2d14600116fc | 162 | |
samux | 1:d2630136d51e | 163 | void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 164 | char t[1] = {addr}; |
emilmont | 0:6149091f755d | 165 | m_i2c.write(m_addr, t, 1, true); |
emilmont | 0:6149091f755d | 166 | m_i2c.read(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 167 | } |
emilmont | 0:6149091f755d | 168 | |
JoKer | 5:2d14600116fc | 169 | |
JoKer | 5:2d14600116fc | 170 | |
samux | 1:d2630136d51e | 171 | void MMA8451Q::writeRegs(uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 172 | m_i2c.write(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 173 | } |