Use accelerometer to interrupt.
Dependencies: mbed SDFileSystem
Fork of shomberg_hw_7 by
MMA8452Q.h@21:c95c6b9e9377, 2018-11-05 (annotated)
- Committer:
- rshomberg
- Date:
- Mon Nov 05 20:17:14 2018 +0000
- Revision:
- 21:c95c6b9e9377
- Child:
- 23:61d87ea09c26
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rshomberg | 21:c95c6b9e9377 | 1 | // Library for our MMA8452Q 3-axis accelerometer |
rshomberg | 21:c95c6b9e9377 | 2 | // Based on the MMA8452Q Arduino Library by Jim Lindblom (SparkFun Electronics) |
rshomberg | 21:c95c6b9e9377 | 3 | |
rshomberg | 21:c95c6b9e9377 | 4 | #ifndef MMA8452Q_H |
rshomberg | 21:c95c6b9e9377 | 5 | #define MMA8452Q_H |
rshomberg | 21:c95c6b9e9377 | 6 | |
rshomberg | 21:c95c6b9e9377 | 7 | #include "mbed.h" |
rshomberg | 21:c95c6b9e9377 | 8 | |
rshomberg | 21:c95c6b9e9377 | 9 | // Register definitions |
rshomberg | 21:c95c6b9e9377 | 10 | #define REG_STATUS 0x00 |
rshomberg | 21:c95c6b9e9377 | 11 | #define OUT_X_MSB 0x01 |
rshomberg | 21:c95c6b9e9377 | 12 | #define OUT_X_LSB 0x02 |
rshomberg | 21:c95c6b9e9377 | 13 | #define OUT_Y_MSB 0x03 |
rshomberg | 21:c95c6b9e9377 | 14 | #define OUT_Y_LSB 0x04 |
rshomberg | 21:c95c6b9e9377 | 15 | #define OUT_Z_MSB 0x05 |
rshomberg | 21:c95c6b9e9377 | 16 | #define OUT_Z_LSB 0x06 |
rshomberg | 21:c95c6b9e9377 | 17 | #define REG_WHO_AM_I 0x0D |
rshomberg | 21:c95c6b9e9377 | 18 | #define REG_XYZ_DATA_CFG 0x0E |
rshomberg | 21:c95c6b9e9377 | 19 | #define REG_CTRL_REG1 0x2A |
rshomberg | 21:c95c6b9e9377 | 20 | |
rshomberg | 21:c95c6b9e9377 | 21 | // WHO_AM_I check |
rshomberg | 21:c95c6b9e9377 | 22 | #define FACTORY_ID 0x2A |
rshomberg | 21:c95c6b9e9377 | 23 | |
rshomberg | 21:c95c6b9e9377 | 24 | // Scale definitions |
rshomberg | 21:c95c6b9e9377 | 25 | #define SCALE_2G 2 |
rshomberg | 21:c95c6b9e9377 | 26 | #define SCALE_4G 4 |
rshomberg | 21:c95c6b9e9377 | 27 | #define SCALE_8G 8 |
rshomberg | 21:c95c6b9e9377 | 28 | |
rshomberg | 21:c95c6b9e9377 | 29 | // Data rates |
rshomberg | 21:c95c6b9e9377 | 30 | #define ODR_800HZ 0 |
rshomberg | 21:c95c6b9e9377 | 31 | #define ODR_400HZ 1 |
rshomberg | 21:c95c6b9e9377 | 32 | #define ODR_200HZ 2 |
rshomberg | 21:c95c6b9e9377 | 33 | #define ODR_100HZ 3 |
rshomberg | 21:c95c6b9e9377 | 34 | #define ODR_50HZ 4 |
rshomberg | 21:c95c6b9e9377 | 35 | #define ODR_12_5HZ 5 |
rshomberg | 21:c95c6b9e9377 | 36 | #define ODR_6_25HZ 6 |
rshomberg | 21:c95c6b9e9377 | 37 | #define ODR_1_56HZ 7 |
rshomberg | 21:c95c6b9e9377 | 38 | |
rshomberg | 21:c95c6b9e9377 | 39 | // Init values |
rshomberg | 21:c95c6b9e9377 | 40 | #define DEFAULT_FSR SCALE_2G |
rshomberg | 21:c95c6b9e9377 | 41 | #define DEFAULT_ODR ODR_800HZ |
rshomberg | 21:c95c6b9e9377 | 42 | |
rshomberg | 21:c95c6b9e9377 | 43 | |
rshomberg | 21:c95c6b9e9377 | 44 | // Class declaration |
rshomberg | 21:c95c6b9e9377 | 45 | class MMA8452Q |
rshomberg | 21:c95c6b9e9377 | 46 | { |
rshomberg | 21:c95c6b9e9377 | 47 | public: |
rshomberg | 21:c95c6b9e9377 | 48 | MMA8452Q(PinName sda, PinName scl, int addr); |
rshomberg | 21:c95c6b9e9377 | 49 | ~MMA8452Q(); |
rshomberg | 21:c95c6b9e9377 | 50 | bool init(); |
rshomberg | 21:c95c6b9e9377 | 51 | uint8_t available(); |
rshomberg | 21:c95c6b9e9377 | 52 | void setScale(uint8_t fsr); |
rshomberg | 21:c95c6b9e9377 | 53 | void setODR(uint8_t odr); |
rshomberg | 21:c95c6b9e9377 | 54 | void standby(); |
rshomberg | 21:c95c6b9e9377 | 55 | void active(); |
rshomberg | 21:c95c6b9e9377 | 56 | float readX(); |
rshomberg | 21:c95c6b9e9377 | 57 | float readY(); |
rshomberg | 21:c95c6b9e9377 | 58 | float readZ(); |
rshomberg | 21:c95c6b9e9377 | 59 | uint8_t readRegister(uint8_t reg); |
rshomberg | 21:c95c6b9e9377 | 60 | void writeRegister(uint8_t reg, uint8_t data); |
rshomberg | 21:c95c6b9e9377 | 61 | |
rshomberg | 21:c95c6b9e9377 | 62 | private: |
rshomberg | 21:c95c6b9e9377 | 63 | I2C m_i2c; |
rshomberg | 21:c95c6b9e9377 | 64 | int m_addr; |
rshomberg | 21:c95c6b9e9377 | 65 | int scale; |
rshomberg | 21:c95c6b9e9377 | 66 | }; |
rshomberg | 21:c95c6b9e9377 | 67 | |
rshomberg | 21:c95c6b9e9377 | 68 | #endif |