3 axis accellerometer for pitch and roll on lcd and serial output

Dependencies:   mbed C12832

Committer:
Sissors
Date:
Sun Oct 14 08:02:53 2012 +0000
Revision:
0:7bc29a9ea016
Child:
1:8997a1b348dd
v0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:7bc29a9ea016 1 #include "MMA7660.h"
Sissors 0:7bc29a9ea016 2
Sissors 0:7bc29a9ea016 3 MMA7660::MMA7660(PinName sda, PinName scl, PinName interrupt) : _i2c(sda, scl)
Sissors 0:7bc29a9ea016 4 {
Sissors 0:7bc29a9ea016 5 _interrupt = interrupt;
Sissors 0:7bc29a9ea016 6 active = false;
Sissors 0:7bc29a9ea016 7 samplerate = 120;
Sissors 0:7bc29a9ea016 8 }
Sissors 0:7bc29a9ea016 9
Sissors 0:7bc29a9ea016 10 //Since the MMA lacks a WHO_AM_I register, we can only check if there is a device that answers to the I2C address
Sissors 0:7bc29a9ea016 11 bool MMA7660::testConnection( void )
Sissors 0:7bc29a9ea016 12 {
Sissors 0:7bc29a9ea016 13 if (_i2c.write(MMA7660_ADDRESS, NULL, 0) == 0 )
Sissors 0:7bc29a9ea016 14 return true;
Sissors 0:7bc29a9ea016 15 else
Sissors 0:7bc29a9ea016 16 return false;
Sissors 0:7bc29a9ea016 17 }
Sissors 0:7bc29a9ea016 18
Sissors 0:7bc29a9ea016 19 void MMA7660::setActive(bool state)
Sissors 0:7bc29a9ea016 20 {
Sissors 0:7bc29a9ea016 21 char modereg = read(MMA7660_MODE_R);
Sissors 0:7bc29a9ea016 22 modereg &= ~(1<<0);
Sissors 0:7bc29a9ea016 23
Sissors 0:7bc29a9ea016 24 //If it somehow was in testmode, disable that
Sissors 0:7bc29a9ea016 25 if (modereg && (1<<2)) {
Sissors 0:7bc29a9ea016 26 modereg &= ~(1<<2);
Sissors 0:7bc29a9ea016 27 write(MMA7660_MODE_R, modereg);
Sissors 0:7bc29a9ea016 28 }
Sissors 0:7bc29a9ea016 29
Sissors 0:7bc29a9ea016 30 modereg += state;
Sissors 0:7bc29a9ea016 31 write(MMA7660_MODE_R, modereg);
Sissors 0:7bc29a9ea016 32 }
Sissors 0:7bc29a9ea016 33
Sissors 0:7bc29a9ea016 34 //Add timeout!
Sissors 0:7bc29a9ea016 35 void MMA7660::readData(int *data)
Sissors 0:7bc29a9ea016 36 {
Sissors 0:7bc29a9ea016 37 if (!active) {
Sissors 0:7bc29a9ea016 38 setActive(true);
Sissors 0:7bc29a9ea016 39 active = true;
Sissors 0:7bc29a9ea016 40 }
Sissors 0:7bc29a9ea016 41
Sissors 0:7bc29a9ea016 42 char temp[3];
Sissors 0:7bc29a9ea016 43 bool alert;
Sissors 0:7bc29a9ea016 44
Sissors 0:7bc29a9ea016 45 do {
Sissors 0:7bc29a9ea016 46 alert = false;
Sissors 0:7bc29a9ea016 47 read(MMA7660_XOUT_R, temp, 3);
Sissors 0:7bc29a9ea016 48 for (int i = 0; i<3; i++) {
Sissors 0:7bc29a9ea016 49 if (temp[i] > 63)
Sissors 0:7bc29a9ea016 50 alert = true;
Sissors 0:7bc29a9ea016 51 if (temp[i] > 31)
Sissors 0:7bc29a9ea016 52 temp[i] += 128+64;
Sissors 0:7bc29a9ea016 53 data[i] = (signed char)temp[i];
Sissors 0:7bc29a9ea016 54 }
Sissors 0:7bc29a9ea016 55 } while (alert);
Sissors 0:7bc29a9ea016 56 }
Sissors 0:7bc29a9ea016 57
Sissors 0:7bc29a9ea016 58 void MMA7660::readData(float *data)
Sissors 0:7bc29a9ea016 59 {
Sissors 0:7bc29a9ea016 60 int intdata[3];
Sissors 0:7bc29a9ea016 61 readData(intdata);
Sissors 0:7bc29a9ea016 62 for (int i = 0; i<3; i++)
Sissors 0:7bc29a9ea016 63 data[i] = intdata[i]/MMA7660_SENSITIVITY;
Sissors 0:7bc29a9ea016 64 }
Sissors 0:7bc29a9ea016 65
Sissors 0:7bc29a9ea016 66 float MMA7660::getX( void ) {
Sissors 0:7bc29a9ea016 67 return getSingle(0);
Sissors 0:7bc29a9ea016 68 }
Sissors 0:7bc29a9ea016 69
Sissors 0:7bc29a9ea016 70 float MMA7660::getY( void ) {
Sissors 0:7bc29a9ea016 71 return getSingle(1);
Sissors 0:7bc29a9ea016 72 }
Sissors 0:7bc29a9ea016 73
Sissors 0:7bc29a9ea016 74 float MMA7660::getZ( void ) {
Sissors 0:7bc29a9ea016 75 return getSingle(2);
Sissors 0:7bc29a9ea016 76 }
Sissors 0:7bc29a9ea016 77
Sissors 0:7bc29a9ea016 78 void MMA7660::write(char address, char data)
Sissors 0:7bc29a9ea016 79 {
Sissors 0:7bc29a9ea016 80 char temp[2];
Sissors 0:7bc29a9ea016 81 temp[0]=address;
Sissors 0:7bc29a9ea016 82 temp[1]=data;
Sissors 0:7bc29a9ea016 83
Sissors 0:7bc29a9ea016 84 _i2c.write(MMA7660_ADDRESS, temp, 2);
Sissors 0:7bc29a9ea016 85 }
Sissors 0:7bc29a9ea016 86
Sissors 0:7bc29a9ea016 87 char MMA7660::read(char address)
Sissors 0:7bc29a9ea016 88 {
Sissors 0:7bc29a9ea016 89 char retval;
Sissors 0:7bc29a9ea016 90 _i2c.write(MMA7660_ADDRESS, &address, 1, true);
Sissors 0:7bc29a9ea016 91 _i2c.read(MMA7660_ADDRESS, &retval, 1);
Sissors 0:7bc29a9ea016 92 return retval;
Sissors 0:7bc29a9ea016 93 }
Sissors 0:7bc29a9ea016 94
Sissors 0:7bc29a9ea016 95 void MMA7660::read(char address, char *data, int length)
Sissors 0:7bc29a9ea016 96 {
Sissors 0:7bc29a9ea016 97 _i2c.write(MMA7660_ADDRESS, &address, 1, true);
Sissors 0:7bc29a9ea016 98 _i2c.read(MMA7660_ADDRESS, data, length);
Sissors 0:7bc29a9ea016 99 }
Sissors 0:7bc29a9ea016 100
Sissors 0:7bc29a9ea016 101 float MMA7660::getSingle( int number )
Sissors 0:7bc29a9ea016 102 {
Sissors 0:7bc29a9ea016 103 if (!active) {
Sissors 0:7bc29a9ea016 104 setActive(true);
Sissors 0:7bc29a9ea016 105 active = true;
Sissors 0:7bc29a9ea016 106 }
Sissors 0:7bc29a9ea016 107
Sissors 0:7bc29a9ea016 108 signed char temp;
Sissors 0:7bc29a9ea016 109 bool alert;
Sissors 0:7bc29a9ea016 110
Sissors 0:7bc29a9ea016 111 do {
Sissors 0:7bc29a9ea016 112 alert = false;
Sissors 0:7bc29a9ea016 113 temp = read(MMA7660_XOUT_R + number);
Sissors 0:7bc29a9ea016 114 if (temp > 63)
Sissors 0:7bc29a9ea016 115 alert = true;
Sissors 0:7bc29a9ea016 116 if (temp > 31)
Sissors 0:7bc29a9ea016 117 temp += 128+64;
Sissors 0:7bc29a9ea016 118 }
Sissors 0:7bc29a9ea016 119
Sissors 0:7bc29a9ea016 120 while (alert);
Sissors 0:7bc29a9ea016 121
Sissors 0:7bc29a9ea016 122 return temp / MMA7660_SENSITIVITY;
Sissors 0:7bc29a9ea016 123 }