SB
Fork of MMA8451Q by
MMA8451Q.h@6:f834bd401ef2, 2014-02-08 (annotated)
- Committer:
- bmdlh
- Date:
- Sat Feb 08 22:20:08 2014 +0000
- Revision:
- 6:f834bd401ef2
- Parent:
- 5:2d14600116fc
ni men bu zhi dao
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 | #ifndef MMA8451Q_H |
emilmont | 0:6149091f755d | 20 | #define MMA8451Q_H |
bmdlh | 6:f834bd401ef2 | 21 | #define MMA8415Q_SAMPLE800 0x00 |
bmdlh | 6:f834bd401ef2 | 22 | #define MMA8415Q_SAMPLE400 0x08 |
bmdlh | 6:f834bd401ef2 | 23 | #define MMA8415Q_SAMPLE200 0x10 |
bmdlh | 6:f834bd401ef2 | 24 | #define MMA8415Q_SAMPLE100 0x18 |
bmdlh | 6:f834bd401ef2 | 25 | #define MMA8415Q_SAMPLE50 0x20 |
bmdlh | 6:f834bd401ef2 | 26 | #define MMA8415Q_SAMPLE12_5 0x28 |
bmdlh | 6:f834bd401ef2 | 27 | #define MMA8415Q_SAMPLE6_25 0x30 |
bmdlh | 6:f834bd401ef2 | 28 | #define MMA8415Q_SAMPLE1_56 0x38 |
emilmont | 0:6149091f755d | 29 | |
emilmont | 0:6149091f755d | 30 | #include "mbed.h" |
emilmont | 0:6149091f755d | 31 | |
samux | 1:d2630136d51e | 32 | /** |
samux | 1:d2630136d51e | 33 | * MMA8451Q accelerometer example |
samux | 2:a077541cbadc | 34 | * |
samux | 2:a077541cbadc | 35 | * @code |
samux | 1:d2630136d51e | 36 | * #include "mbed.h" |
samux | 1:d2630136d51e | 37 | * #include "MMA8451Q.h" |
chris | 3:db7126dbd63f | 38 | * |
samux | 1:d2630136d51e | 39 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
chris | 3:db7126dbd63f | 40 | * |
chris | 4:c4d879a39775 | 41 | * int main(void) { |
chris | 4:c4d879a39775 | 42 | * |
chris | 3:db7126dbd63f | 43 | * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); |
chris | 3:db7126dbd63f | 44 | * PwmOut rled(LED_RED); |
chris | 3:db7126dbd63f | 45 | * PwmOut gled(LED_GREEN); |
chris | 3:db7126dbd63f | 46 | * PwmOut bled(LED_BLUE); |
chris | 3:db7126dbd63f | 47 | * |
chris | 3:db7126dbd63f | 48 | * while (true) { |
chris | 3:db7126dbd63f | 49 | * rled = 1.0 - abs(acc.getAccX()); |
chris | 3:db7126dbd63f | 50 | * gled = 1.0 - abs(acc.getAccY()); |
chris | 3:db7126dbd63f | 51 | * bled = 1.0 - abs(acc.getAccZ()); |
chris | 3:db7126dbd63f | 52 | * wait(0.1); |
chris | 3:db7126dbd63f | 53 | * } |
samux | 1:d2630136d51e | 54 | * } |
samux | 2:a077541cbadc | 55 | * @endcode |
samux | 1:d2630136d51e | 56 | */ |
emilmont | 0:6149091f755d | 57 | class MMA8451Q |
emilmont | 0:6149091f755d | 58 | { |
emilmont | 0:6149091f755d | 59 | public: |
samux | 1:d2630136d51e | 60 | /** |
samux | 1:d2630136d51e | 61 | * MMA8451Q constructor |
samux | 1:d2630136d51e | 62 | * |
samux | 1:d2630136d51e | 63 | * @param sda SDA pin |
samux | 1:d2630136d51e | 64 | * @param sdl SCL pin |
samux | 1:d2630136d51e | 65 | * @param addr addr of the I2C peripheral |
emilmont | 0:6149091f755d | 66 | */ |
emilmont | 0:6149091f755d | 67 | MMA8451Q(PinName sda, PinName scl, int addr); |
emilmont | 0:6149091f755d | 68 | |
samux | 1:d2630136d51e | 69 | /** |
samux | 1:d2630136d51e | 70 | * MMA8451Q destructor |
emilmont | 0:6149091f755d | 71 | */ |
emilmont | 0:6149091f755d | 72 | ~MMA8451Q(); |
emilmont | 0:6149091f755d | 73 | |
samux | 1:d2630136d51e | 74 | /** |
emilmont | 0:6149091f755d | 75 | * Get the value of the WHO_AM_I register |
emilmont | 0:6149091f755d | 76 | * |
samux | 1:d2630136d51e | 77 | * @returns WHO_AM_I value |
emilmont | 0:6149091f755d | 78 | */ |
emilmont | 0:6149091f755d | 79 | uint8_t getWhoAmI(); |
emilmont | 0:6149091f755d | 80 | |
samux | 1:d2630136d51e | 81 | /** |
emilmont | 0:6149091f755d | 82 | * Get X axis acceleration |
emilmont | 0:6149091f755d | 83 | * |
samux | 1:d2630136d51e | 84 | * @returns X axis acceleration |
emilmont | 0:6149091f755d | 85 | */ |
chris | 3:db7126dbd63f | 86 | float getAccX(); |
emilmont | 0:6149091f755d | 87 | |
samux | 1:d2630136d51e | 88 | /** |
emilmont | 0:6149091f755d | 89 | * Get Y axis acceleration |
emilmont | 0:6149091f755d | 90 | * |
samux | 1:d2630136d51e | 91 | * @returns Y axis acceleration |
emilmont | 0:6149091f755d | 92 | */ |
chris | 3:db7126dbd63f | 93 | float getAccY(); |
emilmont | 0:6149091f755d | 94 | |
samux | 1:d2630136d51e | 95 | /** |
emilmont | 0:6149091f755d | 96 | * Get Z axis acceleration |
emilmont | 0:6149091f755d | 97 | * |
samux | 1:d2630136d51e | 98 | * @returns Z axis acceleration |
emilmont | 0:6149091f755d | 99 | */ |
chris | 3:db7126dbd63f | 100 | float getAccZ(); |
emilmont | 0:6149091f755d | 101 | |
samux | 1:d2630136d51e | 102 | /** |
emilmont | 0:6149091f755d | 103 | * Get XYZ axis acceleration |
emilmont | 0:6149091f755d | 104 | * |
samux | 1:d2630136d51e | 105 | * @param res array where acceleration data will be stored |
emilmont | 0:6149091f755d | 106 | */ |
chris | 3:db7126dbd63f | 107 | void getAccAllAxis(float * res); |
JoKer | 5:2d14600116fc | 108 | |
JoKer | 5:2d14600116fc | 109 | /** JK |
JoKer | 5:2d14600116fc | 110 | * Setup Double Tap detection |
JoKer | 5:2d14600116fc | 111 | |
JoKer | 5:2d14600116fc | 112 | |
JoKer | 5:2d14600116fc | 113 | Example: |
JoKer | 5:2d14600116fc | 114 | |
JoKer | 5:2d14600116fc | 115 | #include "mbed.h" |
JoKer | 5:2d14600116fc | 116 | #include "MMA8451Q.h" |
JoKer | 5:2d14600116fc | 117 | |
JoKer | 5:2d14600116fc | 118 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
JoKer | 5:2d14600116fc | 119 | #define ON 0 |
JoKer | 5:2d14600116fc | 120 | #define OFF !ON |
JoKer | 5:2d14600116fc | 121 | |
JoKer | 5:2d14600116fc | 122 | //Setup the interrupts for the MMA8451Q |
JoKer | 5:2d14600116fc | 123 | InterruptIn accInt1(PTA14); |
JoKer | 5:2d14600116fc | 124 | InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer |
JoKer | 5:2d14600116fc | 125 | |
JoKer | 5:2d14600116fc | 126 | uint8_t togstat=0;//Led status |
JoKer | 5:2d14600116fc | 127 | DigitalOut bled(LED_BLUE); |
JoKer | 5:2d14600116fc | 128 | |
JoKer | 5:2d14600116fc | 129 | |
JoKer | 5:2d14600116fc | 130 | void tapTrue(void){//ISR |
JoKer | 5:2d14600116fc | 131 | if(togstat == 0){ |
JoKer | 5:2d14600116fc | 132 | togstat = 1; |
JoKer | 5:2d14600116fc | 133 | bled=ON; |
JoKer | 5:2d14600116fc | 134 | } else { |
JoKer | 5:2d14600116fc | 135 | togstat = 0; |
JoKer | 5:2d14600116fc | 136 | bled=OFF; |
JoKer | 5:2d14600116fc | 137 | } |
JoKer | 5:2d14600116fc | 138 | |
JoKer | 5:2d14600116fc | 139 | } |
JoKer | 5:2d14600116fc | 140 | |
JoKer | 5:2d14600116fc | 141 | |
JoKer | 5:2d14600116fc | 142 | int main(void) { |
JoKer | 5:2d14600116fc | 143 | |
JoKer | 5:2d14600116fc | 144 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance |
JoKer | 5:2d14600116fc | 145 | |
JoKer | 5:2d14600116fc | 146 | acc.setDoubleTap();//Setup the MMA8451Q to look for a double Tap |
JoKer | 5:2d14600116fc | 147 | accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14 |
JoKer | 5:2d14600116fc | 148 | |
JoKer | 5:2d14600116fc | 149 | while (true) { |
JoKer | 5:2d14600116fc | 150 | //Interrupt driven so nothing in main loop |
JoKer | 5:2d14600116fc | 151 | } |
JoKer | 5:2d14600116fc | 152 | } |
JoKer | 5:2d14600116fc | 153 | |
JoKer | 5:2d14600116fc | 154 | |
JoKer | 5:2d14600116fc | 155 | */ |
JoKer | 5:2d14600116fc | 156 | void setDoubleTap(void); |
bmdlh | 6:f834bd401ef2 | 157 | void Overwrite_dr(float dateRate); |
bmdlh | 6:f834bd401ef2 | 158 | void Setdr(float dateRate); |
emilmont | 0:6149091f755d | 159 | private: |
samux | 1:d2630136d51e | 160 | I2C m_i2c; |
emilmont | 0:6149091f755d | 161 | int m_addr; |
samux | 1:d2630136d51e | 162 | void readRegs(int addr, uint8_t * data, int len); |
samux | 1:d2630136d51e | 163 | void writeRegs(uint8_t * data, int len); |
emilmont | 0:6149091f755d | 164 | int16_t getAccAxis(uint8_t addr); |
bmdlh | 6:f834bd401ef2 | 165 | char dr; |
emilmont | 0:6149091f755d | 166 | |
emilmont | 0:6149091f755d | 167 | }; |
emilmont | 0:6149091f755d | 168 | |
emilmont | 0:6149091f755d | 169 | #endif |