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