MECH478

Committer:
daroldkellyjr
Date:
Thu May 12 15:26:25 2022 +0000
Revision:
0:26f36d63a397
Committed;

Who changed what in which revision?

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