Works with 8g
Fork of MMA8452 by
Revision 0:bcf2aa85d7f9, committed 2013-10-04
- Comitter:
- nherriot
- Date:
- Fri Oct 04 14:48:02 2013 +0000
- Child:
- 1:ef026bf28798
- Commit message:
- Initial commit fro mma8452
Changed in this revision
MMA8452.cpp | Show annotated file Show diff for this revision Revisions of this file |
MMA8452.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8452.cpp Fri Oct 04 14:48:02 2013 +0000 @@ -0,0 +1,193 @@ +// Author: Nicholas Herriot +/* Copyright (c) 2013 Vodafone, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +# include "MMA8452.h" + + + +float TILT_XY[64] = {0, 2.69, 5.38, 8.08, 10.81, 13.55, 16.33, 19.16, 22.02, 24.95, 27.95, 31.04, 34.23, 37.54, 41.01, 44.68, 48.59, 52.83, 57.54, 62.95, 69.64, 79.86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -79.86, -69.64, -62.95, -57.54, -52.83, -48.59, -44.68, -41.01, -37.54, -34.23, -31.04, -27.95, -24.95, -22.02, -19.16, -16.33, -13.55, -10.81, -8.08, -5.38, -2.69}; +float TILT_Z[64] = {90.00, 87.31, 84.62, 81.92, 79.19, 76.45, 73.67, 70.84, 67.98, 65.05, 62.05, 58.96, 55.77, 52.46, 48.99, 45.32, 41.41, 37.17, 32.46, 27.05, 20.36, 10.14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -10.14, -20.36, -27.05, -32.46, -37.17, -41.41, -45.32, -48.99, -52.46, -55.77, -58.96, -62.05, -65.05, -67.98, -70.84, -73.67, -76.45, -79.19, -81.92, -84.62}; + + + +// Connect module at I2C address using I2C port pins sda and scl +Accelerometer_MMA8452::Accelerometer_MMA8452(PinName sda, PinName scl,int frequency) : m_i2c(sda, scl): m_frequency(frequency): +{ + //m_i2c.frequency(m_frequency); +} + + +// Destroys instance +Accelerometer_MMA8452::~Accelerometer_MMA8452() +{ + +} + +// Setting the control register bit 1 to true to activate the MMA8452 +int Accelerometer_MMA8452::activate() +{ + char mcu_address = (MMA8452_ADDRESS <1); + char init[2]; + init[0] = CTRL_REG_1; // control register 1 + init[1] = 0x01; // set to active + if(m_i2c.write(mcu_address,init,2) == 0) + { + // pc.printf("The initialisation worked"); + return 0; + } + else + { + // pc.printf("The initialisation failed"); + return 1; + } + +} + + +// Device initialization +void Accelerometer_MMA8452::init() +{ + + write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement + write_reg(SR_STATUS, 0x00); // 120 Samples/Second + write_reg(MODE_STATUS, 0x01); // Active Mode + +} + + +// Reads the tilt angle +void Accelerometer_MMA8452::read_Tilt(float *x, float *y, float *z) +{ + + const char Addr_X = OUT_X_MSB; + char buf[3] = {0,0,0}; + + m_i2c.write(MMA8452_ADDRESS, &Addr_X, 1); // Pointer to the OUT_X_MSB register + m_i2c.read(MMA8452_ADDRESS, buf, 3); // Read register content into buffer with 6bit + + // returns the x, y, z coordinates transformed into full degrees + *x = TILT_XY[(int)buf[0]]; + *y = TILT_XY[(int)buf[1]]; + *z = TILT_Z[(int)buf[2]]; + +} + + +// Reads x data +int Accelerometer_MMA8452::read_x() +{ + char mcu_address = (MMA8452_ADDRESS <1); + + m_i2c.start(); // Start + m_i2c.write(mcu_address); // A write to device 0x98 + m_i2c.write(OUT_X_MSB); // Register to read + m_i2c.start(); + m_i2c.write(mcu_address); // Read from device 0x99 + char x = m_i2c.read(0); // Read the data + m_i2c.stop(); + + return (int)x; + +} + + +// Reads y data +int Accelerometer_MMA8452::read_y() +{ + char mcu_address = (MMA8452_ADDRESS <1); + + m_i2c.start(); // Start + m_i2c.write(mcu_address); // A write to device 0x98 + m_i2c.write(OUT_Y_MSB); // Register to read + m_i2c.start(); + m_i2c.write(mcu_address); // Read from device 0x99 + char y = m_i2c.read(0); // Read the data + m_i2c.stop(); + + return (int)y; + +} + + +// Reads z data +int Accelerometer_MMA8452::read_z() +{ + char mcu_address = (MMA8452_ADDRESS <1); + + m_i2c.start(); // Start + m_i2c.write(mcu_address); // A write to device 0x98 + m_i2c.write(OUT_Z_MSB); // Register to read + m_i2c.start(); + m_i2c.write(mcu_address); // Read from device 0x99 + char z = m_i2c.read(0); // Read the data + m_i2c.stop(); + + return (int)z; + +} + + +// Reads xyz + + + + + // Write register (The device must be placed in Standby Mode to change the value of the registers) +void Accelerometer_MMA8452::write_reg(char addr, char data) +{ + + char cmd[2] = {0, 0}; + + cmd[0] = MODE_STATUS; + cmd[1] = 0x00; // Standby Mode on + m_i2c.write(MMA8452_ADDRESS, cmd, 2); + + cmd[0] = addr; + cmd[1] = data; // New value of the register + m_i2c.write(MMA8452_ADDRESS, cmd, 2); + + cmd[0] = MODE_STATUS; + cmd[1] = 0x01; // Active Mode on + m_i2c.write(MMA8452_ADDRESS, cmd, 2); + +} + + + + // Read from specified MMA7660FC register +char Accelerometer_MMA8452::read_reg(char addr) +{ + + m_i2c.start(); // Start + m_i2c.write(0x98); // A write to device 0x98 + m_i2c.write(addr); // Register to read + m_i2c.start(); + m_i2c.write(0x99); // Read from device 0x99 + char c = m_i2c.read(0); // Read the data + m_i2c.stop(); + + return c; + +} + + + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8452.h Fri Oct 04 14:48:02 2013 +0000 @@ -0,0 +1,194 @@ +// Author: Nicholas Herriot +/* Copyright (c) 2013 Vodafone, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +// the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set +// see the Table 10. I2C Device Address Sequence in Freescale MMA8452Q pdf + +//#define SA0 1 +//#if SA0 + //#define MMA8452_ADDRESS 0x3A // SA0 is high, 0x1C if low - it should be 0x1D, but we shift now to save shifting in the code +//#else + //#define MMA8452_ADDRESS 0x1C +//#endif + +//#ifndef MBED_MMA8452 + +//#define MBED_MMA8452 + +#include "mbed.h" + +/** Accelerometer MMA8452 class + * + * Example: + * @code + * + * #include "mbed.h" + * #include "MMA8452.h" + * + * + * Accelerometer_MMA8452 Acc(p28, p27); + * serial pc(USBTX, USBRX); + * + * int main() + * { + * Acc.init(); + * while(1) + * { + * int x=0, y=0, z=0; + * Acc.read_Tilt(&x, &y, &z); + * pc.printf("Tilt x: %2.2f degree \n", x); // Print the tilt orientation of the X axis + * pc.printf("Tilt y: %2.2f degree \n", y); // Print the tilt orientation of the Y axis + * pc.printf("Tilt z: %2.2f degree \n", z); // Print the tilt orientation of the Z axis + * wait(1); + * } + * } + * @endcode + */ + +// More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q +#define SA0 1 +#if SA0 + #define MMA8452_ADDRESS 0x1D // SA0 is high, 0x1C if low - +#else + #define MMA8452_ADDRESS 0x1C +#endif + +// Register descriptions found in section 6 of pdf +#define STATUS 0x00 // Type 'read' : Real time status, should return 0x00 +#define OUT_X_MSB 0x01 // Type 'read' : x axis - 8 most significatn bit of a 12 bit sample +#define OUT_X_LSB 0x02 // Type 'read' : x axis - 4 least significatn bit of a 12 bit sample +#define OUT_Y_MSB 0x03 // Type 'read' : y axis - 8 most significatn bit of a 12 bit sample +#define OUT_Y_LSB 0x04 // Type 'read' : y axis - 4 least significatn bit of a 12 bit sample +#define OUT_Z_MSB 0x05 // Type 'read' : z axis - 8 most significatn bit of a 12 bit sample +#define OUT_Z_LSB 0x06 // Type 'read' : z axis - 4 least significatn bit of a 12 bit sample + +#define SYSMOD 0x0B // Type 'read' : This tells you if device is active, sleep or standy 0x00=STANDBY 0x01=WAKE 0x02=SLEEP +#define WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A + +#define PL_STATUS 0x10 // Type 'read' : This shows portrait landscape mode orientation +#define PL_CFG 0x11 // Type 'read/write' : This allows portrait landscape configuration +#define PL_COUNT 0x12 // Type 'read' : This is the portraint landscape debounce counter +#define PL_BF_ZCOMP 0x13 // Type 'read' : +#define PL_THS_REG 0x14 // Type 'read' : + +#define FF_MT_CFG 0X15 // Type 'read/write' : Freefaul motion functional block configuration +#define FF_MT_SRC 0X16 // Type 'read' : Freefaul motion event source register +#define FF_COUNT 0X17 // Type 'read' : Freefaul motion threshold register +#define FF_COUNT 0X18 // Type 'read' : Freefaul motion debouce counter + +#define ASLP_COUNT 0x29 // Type 'read/write' : Counter setting for auto sleep +#define CTRL_REG_1 0x2A // Type 'read/write' : +#define CTRL_REG_2 0x2B // Type 'read/write' : +#define CTRL_REG_3 0x2C // Type 'read/write' : +#define CTRL_REG_4 0x2D // Type 'read/write' : +#define CTRL_REG_5 0x2E // Type 'read/write' : + + + +#define TILT_STATUS 0x03 // Tilt Status (Read only) +#define SRST_STATUS 0x04 // Sample Rate Status Register (Read only) +#define SPCNT_STATUS 0x05 // Sleep Count Register (Read/Write) +#define INTSU_STATUS 0x06 // Interrupt Setup Register +#define MODE_STATUS 0x07 // Mode Register (Read/Write) +#define SR_STATUS 0x08 // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write) +#define PDET_STATUS 0x09 // Tap/Pulse Detection Register (Read/Write) +#define PD_STATUS 0xA // Tap/Pulse Debounce Count Register (Read/Write) + + + + + + +class Accelerometer_MMA8452 +{ + public: + + + /** Create an accelerometer object connected to the specified I2C object + * + * @param sda I2C data port + * @param scl I2C8452 clock port + * + */ + Accelerometer_MMA8452(PinName sda, PinName scl); + + /** Destroys an MMA8452 object + * + */ + ~Accelerometer_MMA8452(); + + /** Activate the MMA8452 (required) + * + * + */ + int activate(); + + /** Initialization of device MMA8452 (required) + * + */ + void init(); + + /** Read the Tilt Angle using Three Axis + * + * @param *x Value of x tilt + * @param *y Value of y tilt + * @param *z Value of z tilt + */ + void read_Tilt(float *x, float *y, float *z); + + /** Read the x register of the MMA8452 + * + * @returns The value of x acceleration + */ + int read_x(); + + /** Read the y register of the MMA8452 + * + * @returns The value of y acceleration + */ + int read_y(); + + /** Read the z register of the MMA8452 + * + * @returns The value of z acceleration + */ + int read_z(); + + /** Read from specified MMA8452 register + * + * @param addr The internal registeraddress of the MMA8452 + * @returns The value of the register + */ + char read_reg(char addr); + + /** Write to specified MMA8452 register + * + * @param addr The internal registeraddress of the MMA8452 + * @param data New value of the register + */ + void write_reg(char addr, char data); + + + private: + I2C m_i2c; + int m_frequencey; + +}; + +//#endif