Library for MMA7660FC Accelerometer device
Dependents: TestCode_MMA7660FC 3D_Accelerometer_Tester RTOS-aap-board-modules embed_Grove_3-Axis_Digital_Accelerometer ... more
Revision 8:122af194c74b, committed 2013-08-07
- Comitter:
- edodm85
- Date:
- Wed Aug 07 19:50:09 2013 +0000
- Parent:
- 7:74eb2a4803ba
- Child:
- 9:2cc4a317402d
- Commit message:
- Added the functions read_Side() and read_Orientation()
Changed in this revision
MMA7660FC.cpp | Show annotated file Show diff for this revision Revisions of this file |
MMA7660FC.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MMA7660FC.cpp Wed Aug 07 19:33:25 2013 +0000 +++ b/MMA7660FC.cpp Wed Aug 07 19:50:09 2013 +0000 @@ -28,33 +28,28 @@ // Connect module at I2C address addr using I2C port pins sda and scl MMA7660FC::MMA7660FC(PinName sda, PinName scl, int addr) : m_i2c(sda, scl) { - SPI_W_Address = addr; // Write address - SPI_R_Address = SPI_W_Address | 1; // Read address - + SPI_W_Address = addr; // Write address + SPI_R_Address = SPI_W_Address | 1; // Read address } // Destroys instance MMA7660FC::~MMA7660FC() { - } // Device initialization void MMA7660FC::init() -{ - +{ write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement - write_reg(SR_STATUS, 0x07); // 1 Samples/Second - + write_reg(SR_STATUS, 0x07); // 1 Samples/Second } // Reads the tilt angle void MMA7660FC::read_Tilt(float *x, float *y, float *z) { - const char Addr_X = OUT_X; char buf[3] = {0,0,0}; @@ -64,15 +59,13 @@ // 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]]; - + *z = TILT_Z[(int)buf[2]]; } // Reads x data int MMA7660FC::read_x() { - m_i2c.start(); // Start m_i2c.write(SPI_W_Address); // A write to device m_i2c.write(OUT_X); // Register to read @@ -82,14 +75,12 @@ m_i2c.stop(); return (int)x; - } // Reads y data int MMA7660FC::read_y() { - m_i2c.start(); // Start m_i2c.write(SPI_W_Address); // A write to device m_i2c.write(OUT_Y); // Register to read @@ -99,14 +90,12 @@ m_i2c.stop(); return (int)y; - } // Reads z data int MMA7660FC::read_z() { - m_i2c.start(); // Start m_i2c.write(SPI_W_Address); // A write to device m_i2c.write(OUT_Z); // Register to read @@ -116,14 +105,44 @@ m_i2c.stop(); return (int)z; +} + // Reads MMA7660FC Orientation +char const* MMA7660FC::read_Side() +{ + char tiltStatus = read_reg(TILT_STATUS); + + if((tiltStatus & 0x03) == 0x01) + return "Front"; + if((tiltStatus & 0x03) == 0x02) + return "Back"; + + return "Unknown"; } + // Reads MMA7660FC Orientation +char const* MMA7660FC::read_Orientation() +{ + char tiltStatus = read_reg(TILT_STATUS); + + if((tiltStatus & 0x1c) == 0x04) + return "Left"; + if((tiltStatus & 0x1c) == 0x08) + return "Right"; + if((tiltStatus & 0x1c) == 0x14) + return "Down"; + if((tiltStatus & 0x1c) == 0x18) + return "Up"; + + return "Unknown"; +} + + + // Read from specified MMA7660FC register char MMA7660FC::read_reg(char addr) -{ - +{ m_i2c.start(); // Start m_i2c.write(SPI_W_Address); // A write to device m_i2c.write(addr); // Register to read @@ -132,15 +151,13 @@ char c = m_i2c.read(0); // Read the data m_i2c.stop(); - return c; - + return c; } // Write register (The device must be placed in Standby Mode to change the value of the registers) void MMA7660FC::write_reg(char addr, char data) { - char cmd[2] = {0, 0}; cmd[0] = MODE_STATUS; @@ -153,15 +170,12 @@ cmd[0] = MODE_STATUS; cmd[1] = 0x01; // Active Mode on - m_i2c.write(SPI_W_Address, cmd, 2); - + m_i2c.write(SPI_W_Address, cmd, 2); } // check if the address exist on an I2C bus int MMA7660FC::check() -{ - +{ return m_i2c.write(SPI_W_Address, NULL, 0); - } \ No newline at end of file
--- a/MMA7660FC.h Wed Aug 07 19:33:25 2013 +0000 +++ b/MMA7660FC.h Wed Aug 07 19:50:09 2013 +0000 @@ -73,7 +73,7 @@ public: - /** Create an MMA7660FC object connected to the specified I2C object + /** Creates an MMA7660FC object connected to the specified I2C object * * @param sda I2C data port * @param scl I2C clock port @@ -99,39 +99,51 @@ */ void read_Tilt(float *x, float *y, float *z); - /** Read the x register of the MMA7660FC + /** Reads the x register of the MMA7660FC * * @returns The value of x acceleration */ int read_x(); - /** Read the y register of the MMA7660FC + /** Reads the y register of the MMA7660FC * * @returns The value of y acceleration */ int read_y(); - /** Read the z register of the MMA7660FC + /** Reads the z register of the MMA7660FC * * @returns The value of z acceleration */ int read_z(); + + /** Reads the Front or Back position of the device + * + * @returns The Front or Back position + */ + char const* read_Side(); + + /** Reads the Orientation of the device + * + * @returns The Left or Right or Down or Up Orientation + */ + char const* read_Orientation(); - /** Read from specified MMA7660FC register + /** Reads from specified MMA7660FC register * * @param addr The internal registeraddress of the MMA7660FC * @returns The value of the register */ char read_reg(char addr); - /** Write to specified MMA7660FC register + /** Writes to specified MMA7660FC register * * @param addr The internal registeraddress of the MMA7660FC * @param data New value of the register */ void write_reg(char addr, char data); - /** Check if the address exist on an I2C bus + /** Checks if the address exist on an I2C bus * * @returns 0 on success, or non-0 on failure */