A library to read data from the Triple Axis Accelerometer Breakout - MMA8452Q from Sparkfun.
Dependents: IMOB_without_encryption_txpowermax
This library is based off of the MMA8452Q's documentation (http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Accelerometers/MMA8452Q.pdf) and the extremely helpful tips and tricks provided in the arduino library for the same purpose (https://github.com/sparkfun/MMA8452_Accelerometer).
Revision 0:7dd118f48b1b, committed 2012-12-06
- Comitter:
- dagronlund
- Date:
- Thu Dec 06 17:50:14 2012 +0000
- Commit message:
- First revision of mBed library for the Triple Axis Accelerometer Breakout - MMA8452Q from Sparkfun.
Changed in this revision
AccelSensor.cpp | Show annotated file Show diff for this revision Revisions of this file |
AccelSensor.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 7dd118f48b1b AccelSensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AccelSensor.cpp Thu Dec 06 17:50:14 2012 +0000 @@ -0,0 +1,74 @@ +#include "mbed.h" +#include "AccelSensor.h" + +AccelSensor::AccelSensor(PinName sda, PinName scl) : _i2c(sda, scl) { + //No need to initialise anything else. +} + +void AccelSensor::init() { + char c = readRegister(WHO_AM_I); // Read WHO_AM_I register + standby(); // Must be in standby to change registers + char scale = GSCALE; // Set up the full scale range to 2, 4, or 8g. + if (scale > 8) scale = 8; //Easy error check + scale >>= 2; // Neat trick, see page 22. 00 = 2G, 01 = 4G, 10 = 8G + writeRegister(XYZ_DATA_CFG, scale); + active(); // Set to active to start reading +} + +void AccelSensor::standby() { + char c = readRegister(CTRL_REG1); + writeRegister(CTRL_REG1, c & ~(0x01)); //Clear the active bit to go into standby +} + +void AccelSensor::active() { + char c = readRegister(CTRL_REG1); + writeRegister(CTRL_REG1, c | 0x01); //Set the active bit to begin detection +} + +void AccelSensor::readData(int *destination) { + char rawData[6]; // x/y/z accel register data stored here + readRegisters(OUT_X_MSB, 6, rawData); // Read the six raw data registers into data array + // Loop to calculate 12-bit ADC and g value for each axis + for(int i = 0; i < 3 ; i++) { + int value = (rawData[i*2] << 8) | rawData[(i*2)+1]; //Combine the two 8 bit registers into one 12-bit number + value >>= 4; //The registers are left align, here we right align the 12-bit integer + // If the number is negative, we have to make it so manually (no 12-bit data type) + if (rawData[i*2] > 0x7F) { + value |= 0xFFFFF << 12; + } + destination[i] = value; + } +} + +void AccelSensor::readRegisters(char reg, int range, char* dest) { + int ack = 0; + _i2c.start(); + ack = _i2c.write((ADDRESS << 1)); + ack = _i2c.write(reg); + _i2c.start(); + ack = _i2c.write((ADDRESS << 1) | 0x01); + for (int i = 0; i < range - 1; i++) dest[i] = _i2c.read(1); + dest[range - 1] = _i2c.read(0); + _i2c.stop(); +} + +char AccelSensor::readRegister(char reg) { + int ack = 0; + _i2c.start(); + ack = _i2c.write((ADDRESS << 1)); + ack = _i2c.write(reg); + _i2c.start(); + ack = _i2c.write((ADDRESS << 1) | 0x01); + char result = _i2c.read(0); + _i2c.stop(); + return result; +} + +void AccelSensor::writeRegister(char reg, char data) { + int ack = 0; + _i2c.start(); + ack = _i2c.write((ADDRESS << 1)); + ack = _i2c.write(reg); + ack = _i2c.write(data); + _i2c.stop(); +} \ No newline at end of file
diff -r 000000000000 -r 7dd118f48b1b AccelSensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AccelSensor.h Thu Dec 06 17:50:14 2012 +0000 @@ -0,0 +1,33 @@ +#ifndef MBED_NOKIALCD_H +#define MBED_NOKIALCD_H + +#include "mbed.h" + +//http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Accelerometers/MMA8452Q.pdf +//http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Accelerometers/MMA8452Q-Breakout-v11-fixed.pdf +//http://cache.freescale.com/files/sensors/doc/app_note/AN4069.pdf + +// The SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set +#define ADDRESS 0x1D // 0x1D if SA0 is high, 0x1C if low +//Define a few of the registers that we will be accessing on the MMA8452 +#define OUT_X_MSB 0x01 //1 +#define XYZ_DATA_CFG 0x0E //14 +#define WHO_AM_I 0x0D //13 +#define CTRL_REG1 0x2A //42 +#define GSCALE 2 // Sets full-scale range to +/-2, 4, or 8g. Used to calc real g values. + +class AccelSensor { +public: + AccelSensor(PinName sda, PinName scl); + void active(); + void standby(); + void init(); + void readData(int *destination); +private: + void readRegisters(char reg, int range, char* dest); + char readRegister(char reg); + void writeRegister(char reg, char data); + I2C _i2c; +}; + +#endif \ No newline at end of file