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

Dependencies:   mbed C12832

Committer:
Sissors
Date:
Tue Oct 16 19:42:19 2012 +0000
Revision:
1:8997a1b348dd
Parent:
0:7bc29a9ea016
Child:
2:a8e20db7901e
Tapping doesnt work yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:7bc29a9ea016 1 #include "mbed.h"
Sissors 0:7bc29a9ea016 2
Sissors 0:7bc29a9ea016 3
Sissors 0:7bc29a9ea016 4 #ifndef MMA7660_H
Sissors 0:7bc29a9ea016 5 #define MMA7660_H
Sissors 0:7bc29a9ea016 6
Sissors 0:7bc29a9ea016 7 #define MMA7660_ADDRESS 0x98
Sissors 0:7bc29a9ea016 8 #define MMA7660_SENSITIVITY 21.33
Sissors 0:7bc29a9ea016 9
Sissors 0:7bc29a9ea016 10 #define MMA7660_XOUT_R 0x00
Sissors 0:7bc29a9ea016 11 #define MMA7660_YOUT_R 0x01
Sissors 0:7bc29a9ea016 12 #define MMA7660_ZOUT_R 0x02
Sissors 1:8997a1b348dd 13 #define MMA7660_TILT_R 0x03
Sissors 1:8997a1b348dd 14 #define MMA7660_INT_R 0x06
Sissors 0:7bc29a9ea016 15 #define MMA7660_MODE_R 0x07
Sissors 1:8997a1b348dd 16 #define MMA7660_SR_R 0x08
Sissors 1:8997a1b348dd 17
Sissors 0:7bc29a9ea016 18
Sissors 0:7bc29a9ea016 19 class MMA7660
Sissors 0:7bc29a9ea016 20 {
Sissors 0:7bc29a9ea016 21 public:
Sissors 1:8997a1b348dd 22 /**
Sissors 1:8997a1b348dd 23 * The 6 different orientations and unknown
Sissors 1:8997a1b348dd 24 *
Sissors 1:8997a1b348dd 25 * Up & Down = X-axis
Sissors 1:8997a1b348dd 26 * Right & Left = Y-axis
Sissors 1:8997a1b348dd 27 * Back & Front = Z-axis
Sissors 1:8997a1b348dd 28 *
Sissors 1:8997a1b348dd 29 */
Sissors 1:8997a1b348dd 30 enum Orientation{Up, Down,
Sissors 1:8997a1b348dd 31 Right, Left,
Sissors 1:8997a1b348dd 32 Back, Front,
Sissors 1:8997a1b348dd 33 Unknown};
Sissors 1:8997a1b348dd 34
Sissors 0:7bc29a9ea016 35 /**
Sissors 0:7bc29a9ea016 36 * Creates a new MMA7660 object
Sissors 0:7bc29a9ea016 37 *
Sissors 0:7bc29a9ea016 38 * @param sda - I2C data pin
Sissors 0:7bc29a9ea016 39 * @param scl - I2C clock pin
Sissors 0:7bc29a9ea016 40 * @param interrupt - Interrupt pin (default = NC)
Sissors 0:7bc29a9ea016 41 */
Sissors 0:7bc29a9ea016 42 MMA7660(PinName sda, PinName scl, PinName interrupt = NC);
Sissors 0:7bc29a9ea016 43
Sissors 0:7bc29a9ea016 44 /**
Sissors 0:7bc29a9ea016 45 * Tests if communication is possible with the MMA7660
Sissors 0:7bc29a9ea016 46 *
Sissors 0:7bc29a9ea016 47 * Because the MMA7660 lacks a WHO_AM_I register, this function can only check
Sissors 0:7bc29a9ea016 48 * if there is an I2C device that responds to the MMA7660 address
Sissors 0:7bc29a9ea016 49 *
Sissors 0:7bc29a9ea016 50 * @param return - true for successfull connection, false for no connection
Sissors 0:7bc29a9ea016 51 */
Sissors 0:7bc29a9ea016 52 bool testConnection( void );
Sissors 0:7bc29a9ea016 53
Sissors 0:7bc29a9ea016 54 /**
Sissors 0:7bc29a9ea016 55 * Sets the active state of the MMA7660
Sissors 0:7bc29a9ea016 56 *
Sissors 0:7bc29a9ea016 57 * Note: This is unrelated to awake/sleep mode
Sissors 0:7bc29a9ea016 58 *
Sissors 0:7bc29a9ea016 59 * @param state - true for active, false for standby
Sissors 0:7bc29a9ea016 60 */
Sissors 0:7bc29a9ea016 61 void setActive( bool state);
Sissors 0:7bc29a9ea016 62
Sissors 0:7bc29a9ea016 63 /**
Sissors 0:7bc29a9ea016 64 * Reads acceleration data from the sensor
Sissors 0:7bc29a9ea016 65 *
Sissors 0:7bc29a9ea016 66 * When the parameter is a pointer to an integer array it will be the raw data.
Sissors 0:7bc29a9ea016 67 * When it is a pointer to a float array it will be the acceleration in g's
Sissors 0:7bc29a9ea016 68 *
Sissors 0:7bc29a9ea016 69 * @param data - pointer to array with length 3 where the acceleration data will be stores, X-Y-Z
Sissors 0:7bc29a9ea016 70 */
Sissors 0:7bc29a9ea016 71 void readData( int *data);
Sissors 0:7bc29a9ea016 72 void readData( float *data);
Sissors 0:7bc29a9ea016 73
Sissors 1:8997a1b348dd 74 /**
Sissors 1:8997a1b348dd 75 * Get X-data
Sissors 0:7bc29a9ea016 76 *
Sissors 0:7bc29a9ea016 77 * @param return - X-acceleration in g's
Sissors 0:7bc29a9ea016 78 */
Sissors 0:7bc29a9ea016 79 float getX( void );
Sissors 0:7bc29a9ea016 80
Sissors 1:8997a1b348dd 81 /**
Sissors 1:8997a1b348dd 82 * Get Y-data
Sissors 0:7bc29a9ea016 83 *
Sissors 0:7bc29a9ea016 84 * @param return - Y-acceleration in g's
Sissors 0:7bc29a9ea016 85 */
Sissors 0:7bc29a9ea016 86 float getY( void );
Sissors 0:7bc29a9ea016 87
Sissors 1:8997a1b348dd 88 /**
Sissors 1:8997a1b348dd 89 * Get Z-data
Sissors 0:7bc29a9ea016 90 *
Sissors 0:7bc29a9ea016 91 * @param return - Z-acceleration in g's
Sissors 0:7bc29a9ea016 92 */
Sissors 1:8997a1b348dd 93 float getZ( void );
Sissors 1:8997a1b348dd 94
Sissors 1:8997a1b348dd 95 /**
Sissors 1:8997a1b348dd 96 * Sets the active samplerate
Sissors 1:8997a1b348dd 97 *
Sissors 1:8997a1b348dd 98 * The entered samplerate will be rounded to nearest supported samplerate.
Sissors 1:8997a1b348dd 99 * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second.
Sissors 1:8997a1b348dd 100 *
Sissors 1:8997a1b348dd 101 * @param samplerate - the samplerate that will be set
Sissors 1:8997a1b348dd 102 */
Sissors 1:8997a1b348dd 103 void setSampleRate(int samplerate);
Sissors 1:8997a1b348dd 104
Sissors 1:8997a1b348dd 105 /**
Sissors 1:8997a1b348dd 106 * Returns which side is pointing down
Sissors 1:8997a1b348dd 107 *
Sissors 1:8997a1b348dd 108 * @param return - Orientation which is closest to down
Sissors 1:8997a1b348dd 109 */
Sissors 1:8997a1b348dd 110 Orientation getGlobalOrientation( void );
Sissors 1:8997a1b348dd 111
Sissors 1:8997a1b348dd 112 /**
Sissors 1:8997a1b348dd 113 * Returns if it is on its front, back, or unknown side
Sissors 1:8997a1b348dd 114 *
Sissors 1:8997a1b348dd 115 * This is read from MMA7760s registers, page 12 of datasheet
Sissors 1:8997a1b348dd 116 *
Sissors 1:8997a1b348dd 117 * @param return - Front, Back or Unknown orientation
Sissors 1:8997a1b348dd 118 */
Sissors 1:8997a1b348dd 119 Orientation getSide( void );
Sissors 1:8997a1b348dd 120
Sissors 1:8997a1b348dd 121 /**
Sissors 1:8997a1b348dd 122 * Returns if it is on it left, right, down or up side
Sissors 1:8997a1b348dd 123 *
Sissors 1:8997a1b348dd 124 * This is read from MMA7760s registers, page 12 of datasheet
Sissors 1:8997a1b348dd 125 *
Sissors 1:8997a1b348dd 126 * @param return - Left, Right, Down, Up or Unknown orientation
Sissors 1:8997a1b348dd 127 */
Sissors 1:8997a1b348dd 128 Orientation getOrientation ( void );
Sissors 1:8997a1b348dd 129
Sissors 1:8997a1b348dd 130 /**
Sissors 1:8997a1b348dd 131 * Returns if since last check there has been a tap
Sissors 1:8997a1b348dd 132 *
Sissors 1:8997a1b348dd 133 * @param return - bool that is true when a tap has been detected
Sissors 1:8997a1b348dd 134 */
Sissors 1:8997a1b348dd 135 bool isTapped( void );
Sissors 1:8997a1b348dd 136
Sissors 0:7bc29a9ea016 137
Sissors 0:7bc29a9ea016 138
Sissors 0:7bc29a9ea016 139 private:
Sissors 0:7bc29a9ea016 140
Sissors 0:7bc29a9ea016 141 /**
Sissors 1:8997a1b348dd 142 * Writes data to the device
Sissors 0:7bc29a9ea016 143 *
Sissors 0:7bc29a9ea016 144 * @param adress - register address to write to
Sissors 0:7bc29a9ea016 145 * @param data - data to write
Sissors 0:7bc29a9ea016 146 */
Sissors 0:7bc29a9ea016 147 void write( char address, char data);
Sissors 0:7bc29a9ea016 148
Sissors 0:7bc29a9ea016 149 /**
Sissors 1:8997a1b348dd 150 * Read data from the device
Sissors 0:7bc29a9ea016 151 *
Sissors 0:7bc29a9ea016 152 * @param adress - register address to write to
Sissors 0:7bc29a9ea016 153 * @return - data from the register specified by RA
Sissors 0:7bc29a9ea016 154 */
Sissors 0:7bc29a9ea016 155 char read( char adress);
Sissors 0:7bc29a9ea016 156
Sissors 0:7bc29a9ea016 157 /**
Sissors 0:7bc29a9ea016 158 * Read multiple regigsters from the device, more efficient than using multiple normal reads.
Sissors 0:7bc29a9ea016 159 *
Sissors 0:7bc29a9ea016 160 * @param adress - register address to write to
Sissors 0:7bc29a9ea016 161 * @param length - number of bytes to read
Sissors 0:7bc29a9ea016 162 * @param data - pointer where the data needs to be written to
Sissors 0:7bc29a9ea016 163 */
Sissors 0:7bc29a9ea016 164 void read( char adress, char *data, int length);
Sissors 0:7bc29a9ea016 165
Sissors 0:7bc29a9ea016 166 /**
Sissors 0:7bc29a9ea016 167 * Reads single axis
Sissors 0:7bc29a9ea016 168 */
Sissors 0:7bc29a9ea016 169 float getSingle(int number);
Sissors 0:7bc29a9ea016 170
Sissors 0:7bc29a9ea016 171 I2C _i2c;
Sissors 0:7bc29a9ea016 172 bool active;
Sissors 0:7bc29a9ea016 173 PinName _interrupt;
Sissors 0:7bc29a9ea016 174 float samplerate;
Sissors 0:7bc29a9ea016 175 };
Sissors 0:7bc29a9ea016 176
Sissors 0:7bc29a9ea016 177
Sissors 0:7bc29a9ea016 178 #endif