modified from jppang's MMA8451Q for MMA8491Q on sensor evaluation board with the FRDM-KL25Z
Fork of MMA8491Q by
MMA8491Q.cpp@0:41db55eef564, 2013-10-10 (annotated)
- Committer:
- jppang
- Date:
- Thu Oct 10 03:20:44 2013 +0000
- Revision:
- 0:41db55eef564
- Child:
- 1:2d4b7fadb1e6
modified from MMA8451Q
Who changed what in which revision?
User | Revision | Line number | New 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 | #include "MMA8491Q.h" |
jppang | 0:41db55eef564 | 20 | |
jppang | 0:41db55eef564 | 21 | #define INT_SOURCE 0x0C |
jppang | 0:41db55eef564 | 22 | #define REG_WHO_AM_I 0x0D |
jppang | 0:41db55eef564 | 23 | #define HP_FILTER_CUTOFF 0x0F |
jppang | 0:41db55eef564 | 24 | #define PULSE_CFG 0x21 |
jppang | 0:41db55eef564 | 25 | #define PULSE_SRC 0x22 |
jppang | 0:41db55eef564 | 26 | #define PULSE_THSX 0x23 |
jppang | 0:41db55eef564 | 27 | #define PULSE_THSY 0x24 |
jppang | 0:41db55eef564 | 28 | #define PULSE_THSZ 0x25 |
jppang | 0:41db55eef564 | 29 | #define PULSE_TMLT 0x26 |
jppang | 0:41db55eef564 | 30 | #define PULSE_LTCY 0x27 |
jppang | 0:41db55eef564 | 31 | #define PULSE_WIND 0x28 |
jppang | 0:41db55eef564 | 32 | #define REG_CTRL_REG_1 0x2A |
jppang | 0:41db55eef564 | 33 | #define CTRL_REG2 0x2B |
jppang | 0:41db55eef564 | 34 | #define CTRL_REG4 0x2D |
jppang | 0:41db55eef564 | 35 | #define CTRL_REG5 0x2E |
jppang | 0:41db55eef564 | 36 | #define REG_OUT_X_MSB 0x01 |
jppang | 0:41db55eef564 | 37 | #define REG_OUT_Y_MSB 0x03 |
jppang | 0:41db55eef564 | 38 | #define REG_OUT_Z_MSB 0x05 |
jppang | 0:41db55eef564 | 39 | |
jppang | 0:41db55eef564 | 40 | #define UINT14_MAX 16383 |
jppang | 0:41db55eef564 | 41 | |
jppang | 0:41db55eef564 | 42 | MMA8491Q::MMA8491Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
jppang | 0:41db55eef564 | 43 | // activate the peripheral |
jppang | 0:41db55eef564 | 44 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
jppang | 0:41db55eef564 | 45 | writeRegs(data, 2); |
jppang | 0:41db55eef564 | 46 | } |
jppang | 0:41db55eef564 | 47 | |
jppang | 0:41db55eef564 | 48 | MMA8491Q::~MMA8491Q() { } |
jppang | 0:41db55eef564 | 49 | |
jppang | 0:41db55eef564 | 50 | uint8_t MMA8491Q::getWhoAmI() { |
jppang | 0:41db55eef564 | 51 | uint8_t who_am_i = 0; |
jppang | 0:41db55eef564 | 52 | readRegs(REG_WHO_AM_I, &who_am_i, 1); |
jppang | 0:41db55eef564 | 53 | return who_am_i; |
jppang | 0:41db55eef564 | 54 | } |
jppang | 0:41db55eef564 | 55 | |
jppang | 0:41db55eef564 | 56 | float MMA8491Q::getAccX() { |
jppang | 0:41db55eef564 | 57 | //divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity) |
jppang | 0:41db55eef564 | 58 | return (float(getAccAxis(REG_OUT_X_MSB))*4.0/4096.0); |
jppang | 0:41db55eef564 | 59 | } |
jppang | 0:41db55eef564 | 60 | |
jppang | 0:41db55eef564 | 61 | float MMA8491Q::getAccY() { |
jppang | 0:41db55eef564 | 62 | return (float(getAccAxis(REG_OUT_Y_MSB))*4.0/4096.0); |
jppang | 0:41db55eef564 | 63 | } |
jppang | 0:41db55eef564 | 64 | |
jppang | 0:41db55eef564 | 65 | float MMA8491Q::getAccZ() { |
jppang | 0:41db55eef564 | 66 | return (float(getAccAxis(REG_OUT_Z_MSB))*4.0/4096.0); |
jppang | 0:41db55eef564 | 67 | } |
jppang | 0:41db55eef564 | 68 | |
jppang | 0:41db55eef564 | 69 | void MMA8491Q::getAccAllAxis(float * res) { |
jppang | 0:41db55eef564 | 70 | res[0] = getAccX(); |
jppang | 0:41db55eef564 | 71 | res[1] = getAccY(); |
jppang | 0:41db55eef564 | 72 | res[2] = getAccZ(); |
jppang | 0:41db55eef564 | 73 | } |
jppang | 0:41db55eef564 | 74 | |
jppang | 0:41db55eef564 | 75 | int16_t MMA8491Q::getAccAxis(uint8_t addr) { |
jppang | 0:41db55eef564 | 76 | int16_t acc; |
jppang | 0:41db55eef564 | 77 | uint8_t res[2]; |
jppang | 0:41db55eef564 | 78 | readRegs(addr, res, 2); |
jppang | 0:41db55eef564 | 79 | |
jppang | 0:41db55eef564 | 80 | acc = (res[0] << 6) | (res[1] >> 2); |
jppang | 0:41db55eef564 | 81 | if (acc > UINT14_MAX/2) |
jppang | 0:41db55eef564 | 82 | acc -= UINT14_MAX; |
jppang | 0:41db55eef564 | 83 | |
jppang | 0:41db55eef564 | 84 | return acc; |
jppang | 0:41db55eef564 | 85 | } |
jppang | 0:41db55eef564 | 86 | |
jppang | 0:41db55eef564 | 87 | void MMA8491Q::setDoubleTap(void){ |
jppang | 0:41db55eef564 | 88 | //Implemented directly from Freescale's AN4072 |
jppang | 0:41db55eef564 | 89 | //Added to MMA8491Q lib |
jppang | 0:41db55eef564 | 90 | |
jppang | 0:41db55eef564 | 91 | uint8_t CTRL_REG1_Data; |
jppang | 0:41db55eef564 | 92 | // int adds; |
jppang | 0:41db55eef564 | 93 | uint8_t data[2] = {REG_CTRL_REG_1, 0x08}; |
jppang | 0:41db55eef564 | 94 | |
jppang | 0:41db55eef564 | 95 | //400 Hz, Standby Mode |
jppang | 0:41db55eef564 | 96 | writeRegs(data,2); |
jppang | 0:41db55eef564 | 97 | |
jppang | 0:41db55eef564 | 98 | //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort |
jppang | 0:41db55eef564 | 99 | data[0]=PULSE_CFG;data[1]=0x2A; |
jppang | 0:41db55eef564 | 100 | writeRegs(data,2); |
jppang | 0:41db55eef564 | 101 | |
jppang | 0:41db55eef564 | 102 | //SetThreshold 3g on X and Y and 5g on Z |
jppang | 0:41db55eef564 | 103 | //Note: Every step is 0.063g |
jppang | 0:41db55eef564 | 104 | //3 g/0.063g = 48 counts |
jppang | 0:41db55eef564 | 105 | //5g/0.063g = 79 counts |
jppang | 0:41db55eef564 | 106 | data[0]=PULSE_THSX;data[1]=0x30; |
jppang | 0:41db55eef564 | 107 | writeRegs(data,2);//Set X Threshold to 3g |
jppang | 0:41db55eef564 | 108 | data[0]=PULSE_THSY;data[1]=0x30; |
jppang | 0:41db55eef564 | 109 | writeRegs(data,2);//Set Y Threshold to 3g |
jppang | 0:41db55eef564 | 110 | data[0]=PULSE_THSZ;data[1]=0x4F; |
jppang | 0:41db55eef564 | 111 | writeRegs(data,2);//Set Z Threshold to 5g |
jppang | 0:41db55eef564 | 112 | |
jppang | 0:41db55eef564 | 113 | //Set Time Limit for Tap Detection to 60 ms LP Mode |
jppang | 0:41db55eef564 | 114 | //Note: 400 Hz ODR, Time step is 1.25 ms per step |
jppang | 0:41db55eef564 | 115 | //60 ms/1.25 ms = 48 counts |
jppang | 0:41db55eef564 | 116 | data[0]=PULSE_TMLT;data[1]=0x30; |
jppang | 0:41db55eef564 | 117 | writeRegs(data,2);//60 ms |
jppang | 0:41db55eef564 | 118 | |
jppang | 0:41db55eef564 | 119 | //Set Latency Time to 200 ms |
jppang | 0:41db55eef564 | 120 | //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts |
jppang | 0:41db55eef564 | 121 | data[0]=PULSE_LTCY;data[1]=0x50; |
jppang | 0:41db55eef564 | 122 | writeRegs(data,2);//200 ms |
jppang | 0:41db55eef564 | 123 | |
jppang | 0:41db55eef564 | 124 | //Set Time Window for second tap to 300 ms |
jppang | 0:41db55eef564 | 125 | //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step |
jppang | 0:41db55eef564 | 126 | //300 ms/2.5 ms = 120 counts |
jppang | 0:41db55eef564 | 127 | data[0]=PULSE_WIND;data[1]=0x78; |
jppang | 0:41db55eef564 | 128 | writeRegs(data,2);//300 ms |
jppang | 0:41db55eef564 | 129 | |
jppang | 0:41db55eef564 | 130 | //Route INT1 to System Interrupt |
jppang | 0:41db55eef564 | 131 | data[0]=CTRL_REG4;data[1]=0x08; |
jppang | 0:41db55eef564 | 132 | writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4 |
jppang | 0:41db55eef564 | 133 | data[0]=CTRL_REG5;data[1]=0x08; |
jppang | 0:41db55eef564 | 134 | writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5 |
jppang | 0:41db55eef564 | 135 | |
jppang | 0:41db55eef564 | 136 | //Set the device to Active Mode |
jppang | 0:41db55eef564 | 137 | readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register |
jppang | 0:41db55eef564 | 138 | CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode. |
jppang | 0:41db55eef564 | 139 | data[0]=REG_CTRL_REG_1; |
jppang | 0:41db55eef564 | 140 | data[1]=CTRL_REG1_Data; |
jppang | 0:41db55eef564 | 141 | writeRegs(data,2);//Write in the updated value to put the device in Active Mode |
jppang | 0:41db55eef564 | 142 | } |
jppang | 0:41db55eef564 | 143 | |
jppang | 0:41db55eef564 | 144 | |
jppang | 0:41db55eef564 | 145 | void MMA8491Q::readRegs(int addr, uint8_t * data, int len) { |
jppang | 0:41db55eef564 | 146 | char t[1] = {addr}; |
jppang | 0:41db55eef564 | 147 | m_i2c.write(m_addr, t, 1, true); |
jppang | 0:41db55eef564 | 148 | m_i2c.read(m_addr, (char *)data, len); |
jppang | 0:41db55eef564 | 149 | } |
jppang | 0:41db55eef564 | 150 | |
jppang | 0:41db55eef564 | 151 | |
jppang | 0:41db55eef564 | 152 | |
jppang | 0:41db55eef564 | 153 | void MMA8491Q::writeRegs(uint8_t * data, int len) { |
jppang | 0:41db55eef564 | 154 | m_i2c.write(m_addr, (char *)data, len); |
jppang | 0:41db55eef564 | 155 | } |