Use accelerometer to interrupt.

Dependencies:   mbed SDFileSystem

Fork of shomberg_hw_7 by Russell Shomberg

Committer:
rshomberg
Date:
Sun Nov 18 19:24:26 2018 +0000
Revision:
29:d33071ffaa5f
Parent:
28:a59485b1626b
working code. Tested. Works well.; Doesn't have ability to change interrupt.

Who changed what in which revision?

UserRevisionLine numberNew 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 24:9264fbd225d0 9
rshomberg 21:c95c6b9e9377 10 // Register definitions
rshomberg 21:c95c6b9e9377 11 #define REG_STATUS 0x00
rshomberg 21:c95c6b9e9377 12 #define OUT_X_MSB 0x01
rshomberg 21:c95c6b9e9377 13 #define OUT_X_LSB 0x02
rshomberg 21:c95c6b9e9377 14 #define OUT_Y_MSB 0x03
rshomberg 21:c95c6b9e9377 15 #define OUT_Y_LSB 0x04
rshomberg 21:c95c6b9e9377 16 #define OUT_Z_MSB 0x05
rshomberg 21:c95c6b9e9377 17 #define OUT_Z_LSB 0x06
rshomberg 28:a59485b1626b 18 #define REG_INT_SOURCE 0x0C
rshomberg 21:c95c6b9e9377 19 #define REG_WHO_AM_I 0x0D
rshomberg 21:c95c6b9e9377 20 #define REG_XYZ_DATA_CFG 0x0E
rshomberg 28:a59485b1626b 21
rshomberg 23:61d87ea09c26 22 #define REG_TRANSIENT_CFG 0x1D
rshomberg 23:61d87ea09c26 23 #define REG_TRANSIENT_SRC 0x1E
rshomberg 23:61d87ea09c26 24 #define REG_TRANSIENT_THS 0x1F
rshomberg 23:61d87ea09c26 25 #define REG_TRANSIENT_COUNT 0x20
rshomberg 21:c95c6b9e9377 26
rshomberg 28:a59485b1626b 27 #define REG_CTRL_REG1 0x2A
rshomberg 28:a59485b1626b 28 #define REG_CTRL_REG4 0x2D
rshomberg 28:a59485b1626b 29 #define REG_CTRL_REG5 0x2E
rshomberg 28:a59485b1626b 30
rshomberg 21:c95c6b9e9377 31 // WHO_AM_I check
rshomberg 21:c95c6b9e9377 32 #define FACTORY_ID 0x2A
rshomberg 21:c95c6b9e9377 33
rshomberg 21:c95c6b9e9377 34 // Scale definitions
rshomberg 21:c95c6b9e9377 35 #define SCALE_2G 2
rshomberg 21:c95c6b9e9377 36 #define SCALE_4G 4
rshomberg 21:c95c6b9e9377 37 #define SCALE_8G 8
rshomberg 21:c95c6b9e9377 38
rshomberg 21:c95c6b9e9377 39 // Data rates
rshomberg 21:c95c6b9e9377 40 #define ODR_800HZ 0
rshomberg 21:c95c6b9e9377 41 #define ODR_400HZ 1
rshomberg 21:c95c6b9e9377 42 #define ODR_200HZ 2
rshomberg 21:c95c6b9e9377 43 #define ODR_100HZ 3
rshomberg 21:c95c6b9e9377 44 #define ODR_50HZ 4
rshomberg 21:c95c6b9e9377 45 #define ODR_12_5HZ 5
rshomberg 21:c95c6b9e9377 46 #define ODR_6_25HZ 6
rshomberg 21:c95c6b9e9377 47 #define ODR_1_56HZ 7
rshomberg 21:c95c6b9e9377 48
rshomberg 21:c95c6b9e9377 49 // Init values
rshomberg 21:c95c6b9e9377 50 #define DEFAULT_FSR SCALE_2G
rshomberg 28:a59485b1626b 51 #define DEFAULT_ODR ODR_50HZ
rshomberg 21:c95c6b9e9377 52
rshomberg 21:c95c6b9e9377 53 // Class declaration
rshomberg 21:c95c6b9e9377 54 class MMA8452Q
rshomberg 21:c95c6b9e9377 55 {
rshomberg 21:c95c6b9e9377 56 public:
rshomberg 21:c95c6b9e9377 57 MMA8452Q(PinName sda, PinName scl, int addr);
rshomberg 21:c95c6b9e9377 58 ~MMA8452Q();
rshomberg 21:c95c6b9e9377 59 bool init();
rshomberg 21:c95c6b9e9377 60 uint8_t available();
rshomberg 21:c95c6b9e9377 61 void setScale(uint8_t fsr);
rshomberg 21:c95c6b9e9377 62 void setODR(uint8_t odr);
rshomberg 21:c95c6b9e9377 63 void standby();
rshomberg 21:c95c6b9e9377 64 void active();
rshomberg 21:c95c6b9e9377 65 float readX();
rshomberg 21:c95c6b9e9377 66 float readY();
rshomberg 21:c95c6b9e9377 67 float readZ();
rshomberg 21:c95c6b9e9377 68 uint8_t readRegister(uint8_t reg);
rshomberg 21:c95c6b9e9377 69 void writeRegister(uint8_t reg, uint8_t data);
rshomberg 23:61d87ea09c26 70 void setInterrupt();
rshomberg 21:c95c6b9e9377 71
rshomberg 21:c95c6b9e9377 72 private:
rshomberg 21:c95c6b9e9377 73 I2C m_i2c;
rshomberg 21:c95c6b9e9377 74 int m_addr;
rshomberg 21:c95c6b9e9377 75 int scale;
rshomberg 21:c95c6b9e9377 76 };
rshomberg 21:c95c6b9e9377 77
rshomberg 21:c95c6b9e9377 78 #endif