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