Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Websocket_Ethernet_acc app-board-Sprint-WS-Acc app-board-Ethernet-Websocket app-board-Wifly-Websocket ... more
Revision 0:7bc29a9ea016, committed 2012-10-14
- Comitter:
- Sissors
- Date:
- Sun Oct 14 08:02:53 2012 +0000
- Child:
- 1:8997a1b348dd
- Commit message:
- v0.1
Changed in this revision
| MMA7660.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MMA7660.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660.cpp Sun Oct 14 08:02:53 2012 +0000
@@ -0,0 +1,123 @@
+#include "MMA7660.h"
+
+MMA7660::MMA7660(PinName sda, PinName scl, PinName interrupt) : _i2c(sda, scl)
+{
+ _interrupt = interrupt;
+ active = false;
+ samplerate = 120;
+}
+
+//Since the MMA lacks a WHO_AM_I register, we can only check if there is a device that answers to the I2C address
+bool MMA7660::testConnection( void )
+{
+ if (_i2c.write(MMA7660_ADDRESS, NULL, 0) == 0 )
+ return true;
+ else
+ return false;
+}
+
+void MMA7660::setActive(bool state)
+{
+ char modereg = read(MMA7660_MODE_R);
+ modereg &= ~(1<<0);
+
+ //If it somehow was in testmode, disable that
+ if (modereg && (1<<2)) {
+ modereg &= ~(1<<2);
+ write(MMA7660_MODE_R, modereg);
+ }
+
+ modereg += state;
+ write(MMA7660_MODE_R, modereg);
+}
+
+//Add timeout!
+void MMA7660::readData(int *data)
+{
+ if (!active) {
+ setActive(true);
+ active = true;
+ }
+
+ char temp[3];
+ bool alert;
+
+ do {
+ alert = false;
+ read(MMA7660_XOUT_R, temp, 3);
+ for (int i = 0; i<3; i++) {
+ if (temp[i] > 63)
+ alert = true;
+ if (temp[i] > 31)
+ temp[i] += 128+64;
+ data[i] = (signed char)temp[i];
+ }
+ } while (alert);
+}
+
+void MMA7660::readData(float *data)
+{
+ int intdata[3];
+ readData(intdata);
+ for (int i = 0; i<3; i++)
+ data[i] = intdata[i]/MMA7660_SENSITIVITY;
+}
+
+float MMA7660::getX( void ) {
+ return getSingle(0);
+ }
+
+float MMA7660::getY( void ) {
+ return getSingle(1);
+ }
+
+float MMA7660::getZ( void ) {
+ return getSingle(2);
+ }
+
+void MMA7660::write(char address, char data)
+{
+ char temp[2];
+ temp[0]=address;
+ temp[1]=data;
+
+ _i2c.write(MMA7660_ADDRESS, temp, 2);
+}
+
+char MMA7660::read(char address)
+{
+ char retval;
+ _i2c.write(MMA7660_ADDRESS, &address, 1, true);
+ _i2c.read(MMA7660_ADDRESS, &retval, 1);
+ return retval;
+}
+
+void MMA7660::read(char address, char *data, int length)
+{
+ _i2c.write(MMA7660_ADDRESS, &address, 1, true);
+ _i2c.read(MMA7660_ADDRESS, data, length);
+}
+
+float MMA7660::getSingle( int number )
+{
+ if (!active) {
+ setActive(true);
+ active = true;
+ }
+
+ signed char temp;
+ bool alert;
+
+ do {
+ alert = false;
+ temp = read(MMA7660_XOUT_R + number);
+ if (temp > 63)
+ alert = true;
+ if (temp > 31)
+ temp += 128+64;
+ }
+
+ while (alert);
+
+ return temp / MMA7660_SENSITIVITY;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660.h Sun Oct 14 08:02:53 2012 +0000
@@ -0,0 +1,115 @@
+#include "mbed.h"
+
+
+#ifndef MMA7660_H
+#define MMA7660_H
+
+#define MMA7660_ADDRESS 0x98
+#define MMA7660_SENSITIVITY 21.33
+
+#define MMA7660_XOUT_R 0x00
+#define MMA7660_YOUT_R 0x01
+#define MMA7660_ZOUT_R 0x02
+#define MMA7660_MODE_R 0x07
+
+class MMA7660
+{
+public:
+ /**
+ * Creates a new MMA7660 object
+ *
+ * @param sda - I2C data pin
+ * @param scl - I2C clock pin
+ * @param interrupt - Interrupt pin (default = NC)
+ */
+ MMA7660(PinName sda, PinName scl, PinName interrupt = NC);
+
+ /**
+ * Tests if communication is possible with the MMA7660
+ *
+ * Because the MMA7660 lacks a WHO_AM_I register, this function can only check
+ * if there is an I2C device that responds to the MMA7660 address
+ *
+ * @param return - true for successfull connection, false for no connection
+ */
+ bool testConnection( void );
+
+ /**
+ * Sets the active state of the MMA7660
+ *
+ * Note: This is unrelated to awake/sleep mode
+ *
+ * @param state - true for active, false for standby
+ */
+ void setActive( bool state);
+
+ /**
+ * Reads acceleration data from the sensor
+ *
+ * When the parameter is a pointer to an integer array it will be the raw data.
+ * When it is a pointer to a float array it will be the acceleration in g's
+ *
+ * @param data - pointer to array with length 3 where the acceleration data will be stores, X-Y-Z
+ */
+ void readData( int *data);
+ void readData( float *data);
+
+ /** Get X-data
+ *
+ * @param return - X-acceleration in g's
+ */
+ float getX( void );
+
+ /** Get Y-data
+ *
+ * @param return - Y-acceleration in g's
+ */
+ float getY( void );
+
+ /** Get Z-data
+ *
+ * @param return - Z-acceleration in g's
+ */
+ float getZ( void );
+
+
+private:
+
+ /**
+ * Writes data to the device, could be private, but public is handy so you can transmit directly to the MPU.
+ *
+ * @param adress - register address to write to
+ * @param data - data to write
+ */
+ void write( char address, char data);
+
+ /**
+ * Read data from the device, could be private, but public is handy so you can transmit directly to the MPU.
+ *
+ * @param adress - register address to write to
+ * @return - data from the register specified by RA
+ */
+ char read( char adress);
+
+ /**
+ * Read multiple regigsters from the device, more efficient than using multiple normal reads.
+ *
+ * @param adress - register address to write to
+ * @param length - number of bytes to read
+ * @param data - pointer where the data needs to be written to
+ */
+ void read( char adress, char *data, int length);
+
+ /**
+ * Reads single axis
+ */
+ float getSingle(int number);
+
+ I2C _i2c;
+ bool active;
+ PinName _interrupt;
+ float samplerate;
+};
+
+
+#endif
MMA7660