Bo-Jhang Ho
/
mbed_test_fastest_rate
fastest sampling rate
MMA8451Q/MMA8451Q.h@0:459f1731fa6a, 2014-02-03 (annotated)
- Committer:
- TimeString
- Date:
- Mon Feb 03 02:45:03 2014 +0000
- Revision:
- 0:459f1731fa6a
fastest sampling rate for all sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TimeString | 0:459f1731fa6a | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
TimeString | 0:459f1731fa6a | 2 | * |
TimeString | 0:459f1731fa6a | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
TimeString | 0:459f1731fa6a | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
TimeString | 0:459f1731fa6a | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
TimeString | 0:459f1731fa6a | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
TimeString | 0:459f1731fa6a | 7 | * Software is furnished to do so, subject to the following conditions: |
TimeString | 0:459f1731fa6a | 8 | * |
TimeString | 0:459f1731fa6a | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
TimeString | 0:459f1731fa6a | 10 | * substantial portions of the Software. |
TimeString | 0:459f1731fa6a | 11 | * |
TimeString | 0:459f1731fa6a | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
TimeString | 0:459f1731fa6a | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
TimeString | 0:459f1731fa6a | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
TimeString | 0:459f1731fa6a | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
TimeString | 0:459f1731fa6a | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
TimeString | 0:459f1731fa6a | 17 | */ |
TimeString | 0:459f1731fa6a | 18 | |
TimeString | 0:459f1731fa6a | 19 | #ifndef MMA8451Q_H |
TimeString | 0:459f1731fa6a | 20 | #define MMA8451Q_H |
TimeString | 0:459f1731fa6a | 21 | |
TimeString | 0:459f1731fa6a | 22 | #include "mbed.h" |
TimeString | 0:459f1731fa6a | 23 | |
TimeString | 0:459f1731fa6a | 24 | /** |
TimeString | 0:459f1731fa6a | 25 | * MMA8451Q accelerometer example |
TimeString | 0:459f1731fa6a | 26 | * |
TimeString | 0:459f1731fa6a | 27 | * @code |
TimeString | 0:459f1731fa6a | 28 | * #include "mbed.h" |
TimeString | 0:459f1731fa6a | 29 | * #include "MMA8451Q.h" |
TimeString | 0:459f1731fa6a | 30 | * |
TimeString | 0:459f1731fa6a | 31 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
TimeString | 0:459f1731fa6a | 32 | * |
TimeString | 0:459f1731fa6a | 33 | * int main(void) { |
TimeString | 0:459f1731fa6a | 34 | * |
TimeString | 0:459f1731fa6a | 35 | * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); |
TimeString | 0:459f1731fa6a | 36 | * PwmOut rled(LED_RED); |
TimeString | 0:459f1731fa6a | 37 | * PwmOut gled(LED_GREEN); |
TimeString | 0:459f1731fa6a | 38 | * PwmOut bled(LED_BLUE); |
TimeString | 0:459f1731fa6a | 39 | * |
TimeString | 0:459f1731fa6a | 40 | * while (true) { |
TimeString | 0:459f1731fa6a | 41 | * rled = 1.0 - abs(acc.getAccX()); |
TimeString | 0:459f1731fa6a | 42 | * gled = 1.0 - abs(acc.getAccY()); |
TimeString | 0:459f1731fa6a | 43 | * bled = 1.0 - abs(acc.getAccZ()); |
TimeString | 0:459f1731fa6a | 44 | * wait(0.1); |
TimeString | 0:459f1731fa6a | 45 | * } |
TimeString | 0:459f1731fa6a | 46 | * } |
TimeString | 0:459f1731fa6a | 47 | * @endcode |
TimeString | 0:459f1731fa6a | 48 | */ |
TimeString | 0:459f1731fa6a | 49 | class MMA8451Q |
TimeString | 0:459f1731fa6a | 50 | { |
TimeString | 0:459f1731fa6a | 51 | public: |
TimeString | 0:459f1731fa6a | 52 | /** |
TimeString | 0:459f1731fa6a | 53 | * MMA8451Q constructor |
TimeString | 0:459f1731fa6a | 54 | * |
TimeString | 0:459f1731fa6a | 55 | * @param sda SDA pin |
TimeString | 0:459f1731fa6a | 56 | * @param sdl SCL pin |
TimeString | 0:459f1731fa6a | 57 | * @param addr addr of the I2C peripheral |
TimeString | 0:459f1731fa6a | 58 | */ |
TimeString | 0:459f1731fa6a | 59 | MMA8451Q(PinName sda, PinName scl, int addr); |
TimeString | 0:459f1731fa6a | 60 | |
TimeString | 0:459f1731fa6a | 61 | /** |
TimeString | 0:459f1731fa6a | 62 | * MMA8451Q destructor |
TimeString | 0:459f1731fa6a | 63 | */ |
TimeString | 0:459f1731fa6a | 64 | ~MMA8451Q(); |
TimeString | 0:459f1731fa6a | 65 | |
TimeString | 0:459f1731fa6a | 66 | /** |
TimeString | 0:459f1731fa6a | 67 | * Get the value of the WHO_AM_I register |
TimeString | 0:459f1731fa6a | 68 | * |
TimeString | 0:459f1731fa6a | 69 | * @returns WHO_AM_I value |
TimeString | 0:459f1731fa6a | 70 | */ |
TimeString | 0:459f1731fa6a | 71 | uint8_t getWhoAmI(); |
TimeString | 0:459f1731fa6a | 72 | |
TimeString | 0:459f1731fa6a | 73 | /** |
TimeString | 0:459f1731fa6a | 74 | * Get X axis acceleration |
TimeString | 0:459f1731fa6a | 75 | * |
TimeString | 0:459f1731fa6a | 76 | * @returns X axis acceleration |
TimeString | 0:459f1731fa6a | 77 | */ |
TimeString | 0:459f1731fa6a | 78 | float getAccX(); |
TimeString | 0:459f1731fa6a | 79 | |
TimeString | 0:459f1731fa6a | 80 | /** |
TimeString | 0:459f1731fa6a | 81 | * Get Y axis acceleration |
TimeString | 0:459f1731fa6a | 82 | * |
TimeString | 0:459f1731fa6a | 83 | * @returns Y axis acceleration |
TimeString | 0:459f1731fa6a | 84 | */ |
TimeString | 0:459f1731fa6a | 85 | float getAccY(); |
TimeString | 0:459f1731fa6a | 86 | |
TimeString | 0:459f1731fa6a | 87 | /** |
TimeString | 0:459f1731fa6a | 88 | * Get Z axis acceleration |
TimeString | 0:459f1731fa6a | 89 | * |
TimeString | 0:459f1731fa6a | 90 | * @returns Z axis acceleration |
TimeString | 0:459f1731fa6a | 91 | */ |
TimeString | 0:459f1731fa6a | 92 | float getAccZ(); |
TimeString | 0:459f1731fa6a | 93 | |
TimeString | 0:459f1731fa6a | 94 | /** |
TimeString | 0:459f1731fa6a | 95 | * Get XYZ axis acceleration |
TimeString | 0:459f1731fa6a | 96 | * |
TimeString | 0:459f1731fa6a | 97 | * @param res array where acceleration data will be stored |
TimeString | 0:459f1731fa6a | 98 | */ |
TimeString | 0:459f1731fa6a | 99 | void getAccAllAxis(float * res); |
TimeString | 0:459f1731fa6a | 100 | |
TimeString | 0:459f1731fa6a | 101 | /** JK |
TimeString | 0:459f1731fa6a | 102 | * Setup Double Tap detection |
TimeString | 0:459f1731fa6a | 103 | |
TimeString | 0:459f1731fa6a | 104 | |
TimeString | 0:459f1731fa6a | 105 | Example: |
TimeString | 0:459f1731fa6a | 106 | |
TimeString | 0:459f1731fa6a | 107 | #include "mbed.h" |
TimeString | 0:459f1731fa6a | 108 | #include "MMA8451Q.h" |
TimeString | 0:459f1731fa6a | 109 | |
TimeString | 0:459f1731fa6a | 110 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
TimeString | 0:459f1731fa6a | 111 | #define ON 0 |
TimeString | 0:459f1731fa6a | 112 | #define OFF !ON |
TimeString | 0:459f1731fa6a | 113 | |
TimeString | 0:459f1731fa6a | 114 | //Setup the interrupts for the MMA8451Q |
TimeString | 0:459f1731fa6a | 115 | InterruptIn accInt1(PTA14); |
TimeString | 0:459f1731fa6a | 116 | InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer |
TimeString | 0:459f1731fa6a | 117 | |
TimeString | 0:459f1731fa6a | 118 | uint8_t togstat=0;//Led status |
TimeString | 0:459f1731fa6a | 119 | DigitalOut bled(LED_BLUE); |
TimeString | 0:459f1731fa6a | 120 | |
TimeString | 0:459f1731fa6a | 121 | |
TimeString | 0:459f1731fa6a | 122 | void tapTrue(void){//ISR |
TimeString | 0:459f1731fa6a | 123 | if(togstat == 0){ |
TimeString | 0:459f1731fa6a | 124 | togstat = 1; |
TimeString | 0:459f1731fa6a | 125 | bled=ON; |
TimeString | 0:459f1731fa6a | 126 | } else { |
TimeString | 0:459f1731fa6a | 127 | togstat = 0; |
TimeString | 0:459f1731fa6a | 128 | bled=OFF; |
TimeString | 0:459f1731fa6a | 129 | } |
TimeString | 0:459f1731fa6a | 130 | |
TimeString | 0:459f1731fa6a | 131 | } |
TimeString | 0:459f1731fa6a | 132 | |
TimeString | 0:459f1731fa6a | 133 | |
TimeString | 0:459f1731fa6a | 134 | int main(void) { |
TimeString | 0:459f1731fa6a | 135 | |
TimeString | 0:459f1731fa6a | 136 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance |
TimeString | 0:459f1731fa6a | 137 | |
TimeString | 0:459f1731fa6a | 138 | acc.setDoubleTap();//Setup the MMA8451Q to look for a double Tap |
TimeString | 0:459f1731fa6a | 139 | accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14 |
TimeString | 0:459f1731fa6a | 140 | |
TimeString | 0:459f1731fa6a | 141 | while (true) { |
TimeString | 0:459f1731fa6a | 142 | //Interrupt driven so nothing in main loop |
TimeString | 0:459f1731fa6a | 143 | } |
TimeString | 0:459f1731fa6a | 144 | } |
TimeString | 0:459f1731fa6a | 145 | |
TimeString | 0:459f1731fa6a | 146 | |
TimeString | 0:459f1731fa6a | 147 | */ |
TimeString | 0:459f1731fa6a | 148 | void setDoubleTap(void); |
TimeString | 0:459f1731fa6a | 149 | |
TimeString | 0:459f1731fa6a | 150 | private: |
TimeString | 0:459f1731fa6a | 151 | I2C m_i2c; |
TimeString | 0:459f1731fa6a | 152 | int m_addr; |
TimeString | 0:459f1731fa6a | 153 | void readRegs(int addr, uint8_t * data, int len); |
TimeString | 0:459f1731fa6a | 154 | void writeRegs(uint8_t * data, int len); |
TimeString | 0:459f1731fa6a | 155 | int16_t getAccAxis(uint8_t addr); |
TimeString | 0:459f1731fa6a | 156 | |
TimeString | 0:459f1731fa6a | 157 | }; |
TimeString | 0:459f1731fa6a | 158 | |
TimeString | 0:459f1731fa6a | 159 | #endif |