accelerometer
Dependents: embedded_answ_2 embedded_answ_2_
MMA7660.h@5:2eed18ba5f03, 2020-08-18 (annotated)
- Committer:
- rudolf5020
- Date:
- Tue Aug 18 20:14:45 2020 +0000
- Revision:
- 5:2eed18ba5f03
- Parent:
- 2:a8e20db7901e
part one of question 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 2:a8e20db7901e | 1 | /* Copyright (c) <year> <copyright holders>, MIT License |
Sissors | 2:a8e20db7901e | 2 | * |
Sissors | 2:a8e20db7901e | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Sissors | 2:a8e20db7901e | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Sissors | 2:a8e20db7901e | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Sissors | 2:a8e20db7901e | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Sissors | 2:a8e20db7901e | 7 | * furnished to do so, subject to the following conditions: |
Sissors | 2:a8e20db7901e | 8 | * |
Sissors | 2:a8e20db7901e | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Sissors | 2:a8e20db7901e | 10 | * substantial portions of the Software. |
Sissors | 2:a8e20db7901e | 11 | * |
Sissors | 2:a8e20db7901e | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Sissors | 2:a8e20db7901e | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Sissors | 2:a8e20db7901e | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Sissors | 2:a8e20db7901e | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Sissors | 2:a8e20db7901e | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sissors | 2:a8e20db7901e | 17 | */ |
Sissors | 2:a8e20db7901e | 18 | |
Sissors | 0:7bc29a9ea016 | 19 | #include "mbed.h" |
Sissors | 0:7bc29a9ea016 | 20 | |
Sissors | 0:7bc29a9ea016 | 21 | |
Sissors | 0:7bc29a9ea016 | 22 | #ifndef MMA7660_H |
Sissors | 0:7bc29a9ea016 | 23 | #define MMA7660_H |
Sissors | 0:7bc29a9ea016 | 24 | |
Sissors | 0:7bc29a9ea016 | 25 | #define MMA7660_ADDRESS 0x98 |
Sissors | 0:7bc29a9ea016 | 26 | #define MMA7660_SENSITIVITY 21.33 |
Sissors | 0:7bc29a9ea016 | 27 | |
Sissors | 0:7bc29a9ea016 | 28 | #define MMA7660_XOUT_R 0x00 |
Sissors | 0:7bc29a9ea016 | 29 | #define MMA7660_YOUT_R 0x01 |
Sissors | 0:7bc29a9ea016 | 30 | #define MMA7660_ZOUT_R 0x02 |
Sissors | 1:8997a1b348dd | 31 | #define MMA7660_TILT_R 0x03 |
Sissors | 1:8997a1b348dd | 32 | #define MMA7660_INT_R 0x06 |
Sissors | 0:7bc29a9ea016 | 33 | #define MMA7660_MODE_R 0x07 |
Sissors | 1:8997a1b348dd | 34 | #define MMA7660_SR_R 0x08 |
Sissors | 1:8997a1b348dd | 35 | |
Sissors | 0:7bc29a9ea016 | 36 | |
Sissors | 2:a8e20db7901e | 37 | /** An interface for the MMA7660 triple axis accelerometer |
Sissors | 2:a8e20db7901e | 38 | * |
Sissors | 2:a8e20db7901e | 39 | * @code |
Sissors | 2:a8e20db7901e | 40 | * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed |
Sissors | 2:a8e20db7901e | 41 | * |
Sissors | 2:a8e20db7901e | 42 | * #include "mbed.h" |
Sissors | 2:a8e20db7901e | 43 | * #include "MMA7660.h" |
Sissors | 2:a8e20db7901e | 44 | * |
Sissors | 2:a8e20db7901e | 45 | * MMA7660 MMA(p28, p27); |
Sissors | 2:a8e20db7901e | 46 | * |
Sissors | 2:a8e20db7901e | 47 | * DigitalOut connectionLed(LED1); |
Sissors | 2:a8e20db7901e | 48 | * PwmOut Zaxis_p(LED2); |
Sissors | 2:a8e20db7901e | 49 | * PwmOut Zaxis_n(LED3); |
Sissors | 2:a8e20db7901e | 50 | * |
Sissors | 2:a8e20db7901e | 51 | * int main() { |
Sissors | 2:a8e20db7901e | 52 | * if (MMA.testConnection()) |
Sissors | 2:a8e20db7901e | 53 | * connectionLed = 1; |
Sissors | 2:a8e20db7901e | 54 | * |
Sissors | 2:a8e20db7901e | 55 | * while(1) { |
Sissors | 2:a8e20db7901e | 56 | * Zaxis_p = MMA.z(); |
Sissors | 2:a8e20db7901e | 57 | * Zaxis_n = -MMA.z(); |
Sissors | 2:a8e20db7901e | 58 | * } |
Sissors | 2:a8e20db7901e | 59 | * |
Sissors | 2:a8e20db7901e | 60 | * } |
Sissors | 2:a8e20db7901e | 61 | * @endcode |
Sissors | 2:a8e20db7901e | 62 | */ |
Sissors | 0:7bc29a9ea016 | 63 | class MMA7660 |
Sissors | 0:7bc29a9ea016 | 64 | { |
Sissors | 0:7bc29a9ea016 | 65 | public: |
Sissors | 2:a8e20db7901e | 66 | /** |
Sissors | 1:8997a1b348dd | 67 | * The 6 different orientations and unknown |
Sissors | 1:8997a1b348dd | 68 | * |
Sissors | 1:8997a1b348dd | 69 | * Up & Down = X-axis |
Sissors | 1:8997a1b348dd | 70 | * Right & Left = Y-axis |
Sissors | 1:8997a1b348dd | 71 | * Back & Front = Z-axis |
Sissors | 2:a8e20db7901e | 72 | * |
Sissors | 1:8997a1b348dd | 73 | */ |
Sissors | 2:a8e20db7901e | 74 | enum Orientation {Up, Down, |
Sissors | 2:a8e20db7901e | 75 | Right, Left, |
Sissors | 2:a8e20db7901e | 76 | Back, Front, |
Sissors | 2:a8e20db7901e | 77 | Unknown |
Sissors | 2:a8e20db7901e | 78 | }; |
Sissors | 2:a8e20db7901e | 79 | |
Sissors | 0:7bc29a9ea016 | 80 | /** |
Sissors | 0:7bc29a9ea016 | 81 | * Creates a new MMA7660 object |
Sissors | 0:7bc29a9ea016 | 82 | * |
Sissors | 0:7bc29a9ea016 | 83 | * @param sda - I2C data pin |
Sissors | 0:7bc29a9ea016 | 84 | * @param scl - I2C clock pin |
Sissors | 2:a8e20db7901e | 85 | * @param active - true (default) to enable the device, false to keep it standby |
Sissors | 0:7bc29a9ea016 | 86 | */ |
Sissors | 2:a8e20db7901e | 87 | MMA7660(PinName sda, PinName scl, bool active = true); |
Sissors | 0:7bc29a9ea016 | 88 | |
Sissors | 0:7bc29a9ea016 | 89 | /** |
Sissors | 0:7bc29a9ea016 | 90 | * Tests if communication is possible with the MMA7660 |
Sissors | 0:7bc29a9ea016 | 91 | * |
Sissors | 0:7bc29a9ea016 | 92 | * Because the MMA7660 lacks a WHO_AM_I register, this function can only check |
Sissors | 0:7bc29a9ea016 | 93 | * if there is an I2C device that responds to the MMA7660 address |
Sissors | 0:7bc29a9ea016 | 94 | * |
Sissors | 0:7bc29a9ea016 | 95 | * @param return - true for successfull connection, false for no connection |
Sissors | 0:7bc29a9ea016 | 96 | */ |
Sissors | 0:7bc29a9ea016 | 97 | bool testConnection( void ); |
Sissors | 2:a8e20db7901e | 98 | |
Sissors | 0:7bc29a9ea016 | 99 | /** |
Sissors | 0:7bc29a9ea016 | 100 | * Sets the active state of the MMA7660 |
Sissors | 0:7bc29a9ea016 | 101 | * |
Sissors | 0:7bc29a9ea016 | 102 | * Note: This is unrelated to awake/sleep mode |
Sissors | 0:7bc29a9ea016 | 103 | * |
Sissors | 0:7bc29a9ea016 | 104 | * @param state - true for active, false for standby |
Sissors | 0:7bc29a9ea016 | 105 | */ |
Sissors | 0:7bc29a9ea016 | 106 | void setActive( bool state); |
Sissors | 2:a8e20db7901e | 107 | |
Sissors | 0:7bc29a9ea016 | 108 | /** |
Sissors | 0:7bc29a9ea016 | 109 | * Reads acceleration data from the sensor |
Sissors | 0:7bc29a9ea016 | 110 | * |
Sissors | 2:a8e20db7901e | 111 | * When the parameter is a pointer to an integer array it will be the raw data. |
Sissors | 0:7bc29a9ea016 | 112 | * When it is a pointer to a float array it will be the acceleration in g's |
Sissors | 2:a8e20db7901e | 113 | * |
Sissors | 2:a8e20db7901e | 114 | * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z |
Sissors | 0:7bc29a9ea016 | 115 | */ |
Sissors | 0:7bc29a9ea016 | 116 | void readData( int *data); |
Sissors | 0:7bc29a9ea016 | 117 | void readData( float *data); |
Sissors | 2:a8e20db7901e | 118 | |
Sissors | 2:a8e20db7901e | 119 | /** |
Sissors | 1:8997a1b348dd | 120 | * Get X-data |
Sissors | 0:7bc29a9ea016 | 121 | * |
Sissors | 0:7bc29a9ea016 | 122 | * @param return - X-acceleration in g's |
Sissors | 0:7bc29a9ea016 | 123 | */ |
Sissors | 2:a8e20db7901e | 124 | float x( void ); |
Sissors | 2:a8e20db7901e | 125 | |
Sissors | 2:a8e20db7901e | 126 | /** |
Sissors | 1:8997a1b348dd | 127 | * Get Y-data |
Sissors | 0:7bc29a9ea016 | 128 | * |
Sissors | 0:7bc29a9ea016 | 129 | * @param return - Y-acceleration in g's |
Sissors | 0:7bc29a9ea016 | 130 | */ |
Sissors | 2:a8e20db7901e | 131 | float y( void ); |
Sissors | 2:a8e20db7901e | 132 | |
Sissors | 2:a8e20db7901e | 133 | /** |
Sissors | 1:8997a1b348dd | 134 | * Get Z-data |
Sissors | 0:7bc29a9ea016 | 135 | * |
Sissors | 0:7bc29a9ea016 | 136 | * @param return - Z-acceleration in g's |
Sissors | 0:7bc29a9ea016 | 137 | */ |
Sissors | 2:a8e20db7901e | 138 | float z( void ); |
Sissors | 2:a8e20db7901e | 139 | |
Sissors | 2:a8e20db7901e | 140 | /** |
Sissors | 1:8997a1b348dd | 141 | * Sets the active samplerate |
Sissors | 1:8997a1b348dd | 142 | * |
Sissors | 1:8997a1b348dd | 143 | * The entered samplerate will be rounded to nearest supported samplerate. |
Sissors | 1:8997a1b348dd | 144 | * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second. |
Sissors | 1:8997a1b348dd | 145 | * |
Sissors | 1:8997a1b348dd | 146 | * @param samplerate - the samplerate that will be set |
Sissors | 1:8997a1b348dd | 147 | */ |
Sissors | 2:a8e20db7901e | 148 | void setSampleRate(int samplerate); |
Sissors | 1:8997a1b348dd | 149 | |
Sissors | 1:8997a1b348dd | 150 | /** |
Sissors | 1:8997a1b348dd | 151 | * Returns if it is on its front, back, or unknown side |
Sissors | 1:8997a1b348dd | 152 | * |
Sissors | 1:8997a1b348dd | 153 | * This is read from MMA7760s registers, page 12 of datasheet |
Sissors | 1:8997a1b348dd | 154 | * |
Sissors | 1:8997a1b348dd | 155 | * @param return - Front, Back or Unknown orientation |
Sissors | 1:8997a1b348dd | 156 | */ |
Sissors | 1:8997a1b348dd | 157 | Orientation getSide( void ); |
Sissors | 2:a8e20db7901e | 158 | |
Sissors | 1:8997a1b348dd | 159 | /** |
Sissors | 1:8997a1b348dd | 160 | * Returns if it is on it left, right, down or up side |
Sissors | 1:8997a1b348dd | 161 | * |
Sissors | 1:8997a1b348dd | 162 | * This is read from MMA7760s registers, page 12 of datasheet |
Sissors | 1:8997a1b348dd | 163 | * |
Sissors | 1:8997a1b348dd | 164 | * @param return - Left, Right, Down, Up or Unknown orientation |
Sissors | 1:8997a1b348dd | 165 | */ |
Sissors | 1:8997a1b348dd | 166 | Orientation getOrientation ( void ); |
Sissors | 0:7bc29a9ea016 | 167 | |
Sissors | 0:7bc29a9ea016 | 168 | |
Sissors | 0:7bc29a9ea016 | 169 | private: |
Sissors | 0:7bc29a9ea016 | 170 | |
Sissors | 0:7bc29a9ea016 | 171 | /** |
Sissors | 1:8997a1b348dd | 172 | * Writes data to the device |
Sissors | 0:7bc29a9ea016 | 173 | * |
Sissors | 0:7bc29a9ea016 | 174 | * @param adress - register address to write to |
Sissors | 0:7bc29a9ea016 | 175 | * @param data - data to write |
Sissors | 0:7bc29a9ea016 | 176 | */ |
Sissors | 0:7bc29a9ea016 | 177 | void write( char address, char data); |
Sissors | 0:7bc29a9ea016 | 178 | |
Sissors | 0:7bc29a9ea016 | 179 | /** |
Sissors | 1:8997a1b348dd | 180 | * Read data from the device |
Sissors | 0:7bc29a9ea016 | 181 | * |
Sissors | 0:7bc29a9ea016 | 182 | * @param adress - register address to write to |
Sissors | 0:7bc29a9ea016 | 183 | * @return - data from the register specified by RA |
Sissors | 0:7bc29a9ea016 | 184 | */ |
Sissors | 0:7bc29a9ea016 | 185 | char read( char adress); |
Sissors | 2:a8e20db7901e | 186 | |
Sissors | 0:7bc29a9ea016 | 187 | /** |
Sissors | 2:a8e20db7901e | 188 | * Read multiple regigsters from the device, more efficient than using multiple normal reads. |
Sissors | 0:7bc29a9ea016 | 189 | * |
Sissors | 0:7bc29a9ea016 | 190 | * @param adress - register address to write to |
Sissors | 0:7bc29a9ea016 | 191 | * @param length - number of bytes to read |
Sissors | 2:a8e20db7901e | 192 | * @param data - pointer where the data needs to be written to |
Sissors | 0:7bc29a9ea016 | 193 | */ |
Sissors | 2:a8e20db7901e | 194 | void read( char adress, char *data, int length); |
Sissors | 2:a8e20db7901e | 195 | |
Sissors | 2:a8e20db7901e | 196 | /** |
Sissors | 2:a8e20db7901e | 197 | * Reads single axis |
Sissors | 2:a8e20db7901e | 198 | */ |
Sissors | 2:a8e20db7901e | 199 | float getSingle(int number); |
Sissors | 0:7bc29a9ea016 | 200 | |
Sissors | 0:7bc29a9ea016 | 201 | I2C _i2c; |
Sissors | 0:7bc29a9ea016 | 202 | bool active; |
Sissors | 0:7bc29a9ea016 | 203 | float samplerate; |
Sissors | 0:7bc29a9ea016 | 204 | }; |
Sissors | 0:7bc29a9ea016 | 205 | |
Sissors | 0:7bc29a9ea016 | 206 | |
Sissors | 0:7bc29a9ea016 | 207 | #endif |