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: lab_digital_sensors
Diff: MMA7660.cpp
- Revision:
- 0:7bc29a9ea016
- Child:
- 1:8997a1b348dd
--- /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