Library for driving the MMA8452 accelerometer over I2C

Dependents:   MMA8452_Test MMA8452_Demo Dualing_Tanks IMU-Controlled_MP3_Player ... more

Here is a simple example:

#include "mbed.h"
#include "MMA8452.h"

int main() {
   Serial pc(USBTX,USBRX);
   pc.baud(115200);
   double x = 0, y = 0, z = 0;

   MMA8452 acc(p28, p27, 40000);
   acc.setBitDepth(MMA8452::BIT_DEPTH_12);
   acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
   acc.setDataRate(MMA8452::RATE_100);
   
   while(1) {
      if(!acc.isXYZReady()) {
         wait(0.01);
         continue;
      }
      acc.readXYZGravity(&x,&y,&z);
      pc.printf("Gravities: %lf %lf %lf\r\n",x,y,z);
   }
}

An easy way to test that this actually works is to run the loop above and hold the MMA8452 parallel to the ground along the respective axis (and upsidedown in each axis). You will see 1G on the respective axis and 0G on the others.

Committer:
ashleymills
Date:
Thu Oct 17 10:08:51 2013 +0000
Revision:
8:89272163f395
Parent:
7:8aa5123d403f
Parent:
6:f6bde04bf8be
Child:
9:dfb0f6a7a455
made some simplifications

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nherriot 0:bcf2aa85d7f9 1 // Author: Nicholas Herriot
nherriot 0:bcf2aa85d7f9 2 /* Copyright (c) 2013 Vodafone, MIT License
nherriot 0:bcf2aa85d7f9 3 *
nherriot 0:bcf2aa85d7f9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
nherriot 0:bcf2aa85d7f9 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
nherriot 0:bcf2aa85d7f9 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
nherriot 0:bcf2aa85d7f9 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
nherriot 0:bcf2aa85d7f9 8 * furnished to do so, subject to the following conditions:
nherriot 0:bcf2aa85d7f9 9 *
nherriot 0:bcf2aa85d7f9 10 * The above copyright notice and this permission notice shall be included in all copies or
nherriot 0:bcf2aa85d7f9 11 * substantial portions of the Software.
nherriot 0:bcf2aa85d7f9 12 *
nherriot 0:bcf2aa85d7f9 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
nherriot 0:bcf2aa85d7f9 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
nherriot 0:bcf2aa85d7f9 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
nherriot 0:bcf2aa85d7f9 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
nherriot 0:bcf2aa85d7f9 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nherriot 0:bcf2aa85d7f9 18 */
nherriot 0:bcf2aa85d7f9 19
nherriot 0:bcf2aa85d7f9 20 # include "MMA8452.h"
nherriot 0:bcf2aa85d7f9 21
nherriot 0:bcf2aa85d7f9 22 // Connect module at I2C address using I2C port pins sda and scl
nherriot 1:ef026bf28798 23 Accelerometer_MMA8452::Accelerometer_MMA8452(PinName sda, PinName scl,int frequency) : m_i2c(sda, scl) , m_frequency(frequency)
nherriot 0:bcf2aa85d7f9 24 {
nherriot 0:bcf2aa85d7f9 25 //m_i2c.frequency(m_frequency);
ashleymills 6:f6bde04bf8be 26
ashleymills 6:f6bde04bf8be 27 // setup read and write addresses to avoid duplication
ashleymills 6:f6bde04bf8be 28 _readAddress = (MMA8452_ADDRESS<<1) | 0x01;
ashleymills 6:f6bde04bf8be 29 _writeAddress = (MMA8452_ADDRESS<<1) & 0xFE;
nherriot 0:bcf2aa85d7f9 30 }
nherriot 0:bcf2aa85d7f9 31
nherriot 0:bcf2aa85d7f9 32
nherriot 0:bcf2aa85d7f9 33 // Destroys instance
ashleymills 6:f6bde04bf8be 34 Accelerometer_MMA8452::~Accelerometer_MMA8452()
nherriot 0:bcf2aa85d7f9 35 {
nherriot 0:bcf2aa85d7f9 36
nherriot 0:bcf2aa85d7f9 37 }
nherriot 0:bcf2aa85d7f9 38
nherriot 0:bcf2aa85d7f9 39 // Setting the control register bit 1 to true to activate the MMA8452
nherriot 0:bcf2aa85d7f9 40 int Accelerometer_MMA8452::activate()
nherriot 0:bcf2aa85d7f9 41 {
ashleymills 6:f6bde04bf8be 42 // set control register 1 to active
ashleymills 6:f6bde04bf8be 43 char init[2] = {CTRL_REG_1,ACTIVE};
nherriot 3:ffb0b1650ca2 44
ashleymills 6:f6bde04bf8be 45 // perform write and return error code
ashleymills 6:f6bde04bf8be 46 return m_i2c.write(_writeAddress,init,2);
nherriot 3:ffb0b1650ca2 47 }
nherriot 3:ffb0b1650ca2 48
nherriot 3:ffb0b1650ca2 49
nherriot 5:b3d0abd97e55 50 // Get 'Fast Read Mode' called F_READ. If bit 1 is set '1' then F_READ is active. Fast read will skip LSB when reading xyz
nherriot 5:b3d0abd97e55 51 // resisters from 0x01 to 0x06. When F_READ is '0' then all 6 registers will be read.
nherriot 5:b3d0abd97e55 52
ashleymills 6:f6bde04bf8be 53 int Accelerometer_MMA8452::get_CTRL_Reg1(int* CTRL_Reg)
nherriot 5:b3d0abd97e55 54 {
nherriot 5:b3d0abd97e55 55 m_i2c.start();
ashleymills 6:f6bde04bf8be 56 if( m_i2c.write(_writeAddress) == 0)
nherriot 5:b3d0abd97e55 57 {
ashleymills 8:89272163f395 58 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 5:b3d0abd97e55 59 }
nherriot 5:b3d0abd97e55 60 if( m_i2c.write( CTRL_REG_1) == 0)
nherriot 5:b3d0abd97e55 61 {
nherriot 5:b3d0abd97e55 62 return 1; // we failed to write 'status' to the chip
nherriot 5:b3d0abd97e55 63 }
nherriot 5:b3d0abd97e55 64 m_i2c.start();
ashleymills 8:89272163f395 65 if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
nherriot 5:b3d0abd97e55 66 {
nherriot 5:b3d0abd97e55 67 return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
nherriot 5:b3d0abd97e55 68 }
ashleymills 6:f6bde04bf8be 69 *CTRL_Reg = m_i2c.read(0);
nherriot 5:b3d0abd97e55 70 m_i2c.stop();
nherriot 5:b3d0abd97e55 71 return 0;
nherriot 5:b3d0abd97e55 72 }
nherriot 5:b3d0abd97e55 73
nherriot 3:ffb0b1650ca2 74 // Setting the control register bit 1 to true to activate the MMA8452
nherriot 3:ffb0b1650ca2 75 int Accelerometer_MMA8452::standby()
nherriot 3:ffb0b1650ca2 76 {
ashleymills 8:89272163f395 77 // set control register 1 to standby
ashleymills 8:89272163f395 78 char init[2] = {CTRL_REG_1,STANDBY};
nherriot 1:ef026bf28798 79
ashleymills 8:89272163f395 80 // write to the register and return the error code
ashleymills 8:89272163f395 81 return m_i2c.write(_writeAddress,init,2);
nherriot 0:bcf2aa85d7f9 82 }
nherriot 0:bcf2aa85d7f9 83
nherriot 0:bcf2aa85d7f9 84
nherriot 3:ffb0b1650ca2 85
nherriot 0:bcf2aa85d7f9 86 // Device initialization
nherriot 0:bcf2aa85d7f9 87 void Accelerometer_MMA8452::init()
nherriot 0:bcf2aa85d7f9 88 {
nherriot 0:bcf2aa85d7f9 89
nherriot 0:bcf2aa85d7f9 90 write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement
nherriot 0:bcf2aa85d7f9 91 write_reg(SR_STATUS, 0x00); // 120 Samples/Second
nherriot 0:bcf2aa85d7f9 92 write_reg(MODE_STATUS, 0x01); // Active Mode
nherriot 0:bcf2aa85d7f9 93
nherriot 0:bcf2aa85d7f9 94 }
nherriot 0:bcf2aa85d7f9 95
nherriot 3:ffb0b1650ca2 96 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 97 int Accelerometer_MMA8452::get_SystemMode(int& deviceSystemMode)
nherriot 3:ffb0b1650ca2 98 {
nherriot 3:ffb0b1650ca2 99 m_i2c.start();
ashleymills 8:89272163f395 100 if( m_i2c.write(_writeAddress) == 0)
nherriot 3:ffb0b1650ca2 101 {
nherriot 3:ffb0b1650ca2 102 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 103 }
nherriot 3:ffb0b1650ca2 104 if( m_i2c.write( SYSMOD) == 0)
nherriot 3:ffb0b1650ca2 105 {
nherriot 3:ffb0b1650ca2 106 return 1; // we failed to write 'status' to the chip
nherriot 3:ffb0b1650ca2 107 }
nherriot 3:ffb0b1650ca2 108 m_i2c.start();
ashleymills 8:89272163f395 109 if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
nherriot 3:ffb0b1650ca2 110 {
nherriot 3:ffb0b1650ca2 111 return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
nherriot 3:ffb0b1650ca2 112 }
nherriot 3:ffb0b1650ca2 113 deviceSystemMode = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 114 m_i2c.stop();
nherriot 3:ffb0b1650ca2 115 return 0;
nherriot 3:ffb0b1650ca2 116
nherriot 3:ffb0b1650ca2 117
nherriot 3:ffb0b1650ca2 118 }
nherriot 3:ffb0b1650ca2 119
nherriot 3:ffb0b1650ca2 120
nherriot 3:ffb0b1650ca2 121
nherriot 3:ffb0b1650ca2 122 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 123 int Accelerometer_MMA8452::get_Status(int& deviceStatus)
nherriot 3:ffb0b1650ca2 124 {
nherriot 3:ffb0b1650ca2 125 m_i2c.start();
ashleymills 8:89272163f395 126 if( m_i2c.write(_writeAddress) == 0) // tell the bus this is a write
nherriot 3:ffb0b1650ca2 127 {
nherriot 3:ffb0b1650ca2 128 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 129 }
ashleymills 8:89272163f395 130 if( m_i2c.write(STATUS) == 0)
nherriot 3:ffb0b1650ca2 131 {
nherriot 3:ffb0b1650ca2 132 return 1; // we failed to write 'status' to the chip
nherriot 3:ffb0b1650ca2 133 }
nherriot 3:ffb0b1650ca2 134 m_i2c.start();
ashleymills 8:89272163f395 135 if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
nherriot 3:ffb0b1650ca2 136 {
nherriot 3:ffb0b1650ca2 137 return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
nherriot 3:ffb0b1650ca2 138 }
nherriot 3:ffb0b1650ca2 139 deviceStatus = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 140 m_i2c.stop();
nherriot 3:ffb0b1650ca2 141 return 0;
nherriot 3:ffb0b1650ca2 142
nherriot 3:ffb0b1650ca2 143
nherriot 3:ffb0b1650ca2 144 }
nherriot 3:ffb0b1650ca2 145
nherriot 0:bcf2aa85d7f9 146
nherriot 1:ef026bf28798 147 // Get device ID
nherriot 2:66db0f91b215 148 int Accelerometer_MMA8452::get_DeviceID(int& deviceID)
nherriot 1:ef026bf28798 149 {
nherriot 1:ef026bf28798 150 m_i2c.start();
ashleymills 8:89272163f395 151 if( m_i2c.write(_writeAddress) == 0) // just good practice to force bit 1 to a '0' by ANDing with 0xFE
nherriot 1:ef026bf28798 152 {
nherriot 3:ffb0b1650ca2 153 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 1:ef026bf28798 154 }
nherriot 1:ef026bf28798 155 if( m_i2c.write( WHO_AM_I) == 0)
nherriot 1:ef026bf28798 156 {
nherriot 3:ffb0b1650ca2 157 return 1; // we failed to write 'who am i' to the chip
nherriot 1:ef026bf28798 158 }
nherriot 1:ef026bf28798 159 m_i2c.start();
ashleymills 8:89272163f395 160 if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
nherriot 1:ef026bf28798 161 {
nherriot 3:ffb0b1650ca2 162 return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
nherriot 1:ef026bf28798 163 }
nherriot 2:66db0f91b215 164 deviceID = m_i2c.read(0);
nherriot 1:ef026bf28798 165 m_i2c.stop();
nherriot 1:ef026bf28798 166 return 0;
nherriot 1:ef026bf28798 167
nherriot 1:ef026bf28798 168 }
nherriot 1:ef026bf28798 169
nherriot 1:ef026bf28798 170
nherriot 3:ffb0b1650ca2 171 /*
nherriot 3:ffb0b1650ca2 172 // Reads x data
nherriot 3:ffb0b1650ca2 173 int Accelerometer_MMA8452::read_x(int& xaxisLSB)
nherriot 0:bcf2aa85d7f9 174 {
nherriot 3:ffb0b1650ca2 175 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 176 m_i2c.start();
nherriot 3:ffb0b1650ca2 177 if( m_i2c.write( mcu_address & 0xFE) == 0) // just good practice to force bit 1 to a '0' by ANDing with 0xFE
nherriot 3:ffb0b1650ca2 178 {
nherriot 3:ffb0b1650ca2 179 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 180 }
nherriot 3:ffb0b1650ca2 181 if( m_i2c.write( OUT_X_MSB) == 0)
nherriot 3:ffb0b1650ca2 182 {
nherriot 3:ffb0b1650ca2 183 return 1; // we failed to write 'X axis LSB' to the chip
nherriot 3:ffb0b1650ca2 184 }
nherriot 3:ffb0b1650ca2 185 m_i2c.start();
nherriot 3:ffb0b1650ca2 186 if( m_i2c.write( mcu_address | 0x01) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
nherriot 3:ffb0b1650ca2 187 {
nherriot 3:ffb0b1650ca2 188 return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
nherriot 3:ffb0b1650ca2 189 }
nherriot 3:ffb0b1650ca2 190 xaxisLSB = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 191 m_i2c.stop();
nherriot 3:ffb0b1650ca2 192 return 0;
nherriot 3:ffb0b1650ca2 193 }
nherriot 3:ffb0b1650ca2 194 */
nherriot 0:bcf2aa85d7f9 195
ashleymills 8:89272163f395 196 int Accelerometer_MMA8452::read_raw(char src, char *dst, int len) {
ashleymills 8:89272163f395 197 // this is the register we want to get data from
ashleymills 8:89272163f395 198 char register_address[1];
ashleymills 8:89272163f395 199 register_address[0] = src;
ashleymills 8:89272163f395 200
ashleymills 8:89272163f395 201 if(m_i2c.write(_writeAddress,register_address,1,true) == 0)
ashleymills 8:89272163f395 202 {
ashleymills 8:89272163f395 203 if(m_i2c.read(_readAddress,dst,len)==0)
ashleymills 8:89272163f395 204 {
ashleymills 8:89272163f395 205 return 0;
ashleymills 8:89272163f395 206 }
ashleymills 8:89272163f395 207 }
ashleymills 8:89272163f395 208
ashleymills 8:89272163f395 209 // failure case, zero array and return error
ashleymills 8:89272163f395 210 for(int i=0; i<len; i++) {
ashleymills 8:89272163f395 211 dst[i] = 0x00;
ashleymills 8:89272163f395 212 }
ashleymills 8:89272163f395 213 return 1;
ashleymills 8:89272163f395 214 }
nherriot 3:ffb0b1650ca2 215
nherriot 3:ffb0b1650ca2 216 // Reads x data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 217 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 218 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 219 // the raw data.
nherriot 3:ffb0b1650ca2 220 //int Accelerometer_MMA8452::read_x(int& xaxisLSB)
nherriot 3:ffb0b1650ca2 221 int Accelerometer_MMA8452::read_x_raw(char *xaxis)
ashleymills 8:89272163f395 222 {
ashleymills 8:89272163f395 223 // this is the register we want to get data from
ashleymills 8:89272163f395 224 char xaxis_register[1] = {OUT_X_MSB};
nherriot 3:ffb0b1650ca2 225 //signed short s = 0;
nherriot 0:bcf2aa85d7f9 226
ashleymills 8:89272163f395 227 if(m_i2c.write(_writeAddress,xaxis_register,1,true) == 0)
nherriot 3:ffb0b1650ca2 228 {
ashleymills 8:89272163f395 229 if(m_i2c.read(_readAddress,xaxis,2)==0)
nherriot 3:ffb0b1650ca2 230 {
ashleymills 8:89272163f395 231 return 0;
nherriot 3:ffb0b1650ca2 232 }
nherriot 3:ffb0b1650ca2 233 }
ashleymills 8:89272163f395 234
ashleymills 8:89272163f395 235 // failure case
ashleymills 8:89272163f395 236 xaxis[0] = 0x00; // make sure the array is set to zero
ashleymills 8:89272163f395 237 xaxis[1] = 0x00;
ashleymills 8:89272163f395 238 return 1; // failed to write and request the OUT_X_MSB bit
nherriot 0:bcf2aa85d7f9 239 }
nherriot 0:bcf2aa85d7f9 240
nherriot 0:bcf2aa85d7f9 241
nherriot 3:ffb0b1650ca2 242 // Reads y data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 243 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 244 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 245 // the raw data.
nherriot 3:ffb0b1650ca2 246
ashleymills 8:89272163f395 247 int Accelerometer_MMA8452::read_y_raw(char *yaxis) {
ashleymills 8:89272163f395 248 return read_raw(OUT_Y_MSB,yaxis,2);
nherriot 3:ffb0b1650ca2 249 }
nherriot 3:ffb0b1650ca2 250
nherriot 0:bcf2aa85d7f9 251
nherriot 3:ffb0b1650ca2 252
nherriot 3:ffb0b1650ca2 253 // Reads z data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 254 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 255 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 256 // the raw data.
nherriot 3:ffb0b1650ca2 257
nherriot 3:ffb0b1650ca2 258 int Accelerometer_MMA8452::read_z_raw(char *zaxis)
nherriot 3:ffb0b1650ca2 259 {
ashleymills 8:89272163f395 260 return read_raw(OUT_Z_MSB,zaxis,2);
nherriot 3:ffb0b1650ca2 261 }
nherriot 0:bcf2aa85d7f9 262
nherriot 0:bcf2aa85d7f9 263
nherriot 0:bcf2aa85d7f9 264
nherriot 0:bcf2aa85d7f9 265 // Reads y data
nherriot 0:bcf2aa85d7f9 266 int Accelerometer_MMA8452::read_y()
nherriot 0:bcf2aa85d7f9 267 {
nherriot 1:ef026bf28798 268 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 0:bcf2aa85d7f9 269
nherriot 0:bcf2aa85d7f9 270 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 271 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 272 m_i2c.write(OUT_Y_MSB); // Register to read
nherriot 0:bcf2aa85d7f9 273 m_i2c.start();
nherriot 0:bcf2aa85d7f9 274 m_i2c.write(mcu_address); // Read from device 0x99
nherriot 1:ef026bf28798 275 int y = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 276 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 277
nherriot 1:ef026bf28798 278 return y;
nherriot 0:bcf2aa85d7f9 279
nherriot 0:bcf2aa85d7f9 280 }
nherriot 0:bcf2aa85d7f9 281
nherriot 0:bcf2aa85d7f9 282
nherriot 0:bcf2aa85d7f9 283 // Reads z data
nherriot 0:bcf2aa85d7f9 284 int Accelerometer_MMA8452::read_z()
nherriot 0:bcf2aa85d7f9 285 {
nherriot 1:ef026bf28798 286 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 0:bcf2aa85d7f9 287
nherriot 0:bcf2aa85d7f9 288 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 289 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 290 m_i2c.write(OUT_Z_MSB); // Register to read
nherriot 0:bcf2aa85d7f9 291 m_i2c.start();
nherriot 0:bcf2aa85d7f9 292 m_i2c.write(mcu_address); // Read from device 0x99
nherriot 1:ef026bf28798 293 int z = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 294 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 295
nherriot 1:ef026bf28798 296 return z;
nherriot 0:bcf2aa85d7f9 297
nherriot 0:bcf2aa85d7f9 298 }
nherriot 0:bcf2aa85d7f9 299
nherriot 0:bcf2aa85d7f9 300
nherriot 0:bcf2aa85d7f9 301 // Reads xyz
nherriot 1:ef026bf28798 302 int Accelerometer_MMA8452::read_xyz(char *x, char *y, char *z)
nherriot 1:ef026bf28798 303 {
nherriot 1:ef026bf28798 304
nherriot 1:ef026bf28798 305
nherriot 1:ef026bf28798 306 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 1:ef026bf28798 307 char register_buffer[6] ={0,0,0,0,0,0};
nherriot 1:ef026bf28798 308 const char Addr_X = OUT_X_MSB;
nherriot 1:ef026bf28798 309 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 1:ef026bf28798 310 m_i2c.write(MMA8452_ADDRESS, &Addr_X, 1); // Pointer to the OUT_X_MSB register
nherriot 1:ef026bf28798 311
nherriot 1:ef026bf28798 312 if(m_i2c.write(mcu_address,&Addr_X,1) == 0)
nherriot 1:ef026bf28798 313 {
nherriot 1:ef026bf28798 314 if(m_i2c.read(mcu_address,register_buffer,6) == 0)
nherriot 1:ef026bf28798 315 {
nherriot 1:ef026bf28798 316 *x = register_buffer[1];
nherriot 1:ef026bf28798 317 *y = register_buffer[3];
nherriot 1:ef026bf28798 318 *z = register_buffer[5];
nherriot 1:ef026bf28798 319 return 0; // yahoooooo
nherriot 1:ef026bf28798 320 }
nherriot 1:ef026bf28798 321 else
nherriot 1:ef026bf28798 322 {
nherriot 1:ef026bf28798 323 return 1; // failed oh nooo!
nherriot 1:ef026bf28798 324 }
nherriot 1:ef026bf28798 325 }
nherriot 1:ef026bf28798 326 else
nherriot 1:ef026bf28798 327 {
nherriot 1:ef026bf28798 328 return 1; // failed oh nooo!
nherriot 1:ef026bf28798 329 }
nherriot 0:bcf2aa85d7f9 330
nherriot 1:ef026bf28798 331 }
nherriot 0:bcf2aa85d7f9 332
nherriot 0:bcf2aa85d7f9 333 // Write register (The device must be placed in Standby Mode to change the value of the registers)
nherriot 0:bcf2aa85d7f9 334 void Accelerometer_MMA8452::write_reg(char addr, char data)
nherriot 0:bcf2aa85d7f9 335 {
nherriot 0:bcf2aa85d7f9 336
nherriot 0:bcf2aa85d7f9 337 char cmd[2] = {0, 0};
nherriot 0:bcf2aa85d7f9 338
nherriot 0:bcf2aa85d7f9 339 cmd[0] = MODE_STATUS;
nherriot 0:bcf2aa85d7f9 340 cmd[1] = 0x00; // Standby Mode on
nherriot 0:bcf2aa85d7f9 341 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 342
nherriot 0:bcf2aa85d7f9 343 cmd[0] = addr;
nherriot 0:bcf2aa85d7f9 344 cmd[1] = data; // New value of the register
nherriot 0:bcf2aa85d7f9 345 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 346
nherriot 0:bcf2aa85d7f9 347 cmd[0] = MODE_STATUS;
nherriot 0:bcf2aa85d7f9 348 cmd[1] = 0x01; // Active Mode on
nherriot 0:bcf2aa85d7f9 349 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 350
nherriot 0:bcf2aa85d7f9 351 }
nherriot 0:bcf2aa85d7f9 352
nherriot 0:bcf2aa85d7f9 353
nherriot 0:bcf2aa85d7f9 354
nherriot 0:bcf2aa85d7f9 355 // Read from specified MMA7660FC register
nherriot 0:bcf2aa85d7f9 356 char Accelerometer_MMA8452::read_reg(char addr)
nherriot 0:bcf2aa85d7f9 357 {
nherriot 0:bcf2aa85d7f9 358
ashleymills 8:89272163f395 359 m_i2c.start();
ashleymills 8:89272163f395 360 if( m_i2c.write(_writeAddress) == 0)
ashleymills 8:89272163f395 361 {
ashleymills 8:89272163f395 362 return 1; // we failed to write the mcu address on the bus to initiate dialogue
ashleymills 8:89272163f395 363 }
ashleymills 8:89272163f395 364 if( m_i2c.write( addr) == 0)
ashleymills 8:89272163f395 365 {
ashleymills 8:89272163f395 366 return 1; // we failed to write 'status' to the chip
ashleymills 8:89272163f395 367 }
ashleymills 8:89272163f395 368 m_i2c.start();
ashleymills 8:89272163f395 369 if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
ashleymills 8:89272163f395 370 {
ashleymills 8:89272163f395 371 return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
ashleymills 8:89272163f395 372 }
ashleymills 8:89272163f395 373 char c = m_i2c.read(0);
ashleymills 8:89272163f395 374 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 375
nherriot 0:bcf2aa85d7f9 376 return c;
nherriot 0:bcf2aa85d7f9 377
nherriot 0:bcf2aa85d7f9 378 }
nherriot 0:bcf2aa85d7f9 379
nherriot 0:bcf2aa85d7f9 380
nherriot 0:bcf2aa85d7f9 381
nherriot 0:bcf2aa85d7f9 382
nherriot 0:bcf2aa85d7f9 383
nherriot 0:bcf2aa85d7f9 384