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:
nherriot
Date:
Wed Oct 16 16:26:26 2013 +0000
Revision:
4:27aa3cd43234
Parent:
3:ffb0b1650ca2
Child:
5:b3d0abd97e55
simplification on several methods.

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
nherriot 0:bcf2aa85d7f9 23
nherriot 0:bcf2aa85d7f9 24
nherriot 0:bcf2aa85d7f9 25
nherriot 0:bcf2aa85d7f9 26
nherriot 0:bcf2aa85d7f9 27 // Connect module at I2C address using I2C port pins sda and scl
nherriot 1:ef026bf28798 28 Accelerometer_MMA8452::Accelerometer_MMA8452(PinName sda, PinName scl,int frequency) : m_i2c(sda, scl) , m_frequency(frequency)
nherriot 0:bcf2aa85d7f9 29 {
nherriot 0:bcf2aa85d7f9 30 //m_i2c.frequency(m_frequency);
nherriot 0:bcf2aa85d7f9 31 }
nherriot 0:bcf2aa85d7f9 32
nherriot 0:bcf2aa85d7f9 33
nherriot 0:bcf2aa85d7f9 34 // Destroys instance
nherriot 0:bcf2aa85d7f9 35 Accelerometer_MMA8452::~Accelerometer_MMA8452()
nherriot 0:bcf2aa85d7f9 36 {
nherriot 0:bcf2aa85d7f9 37
nherriot 0:bcf2aa85d7f9 38 }
nherriot 0:bcf2aa85d7f9 39
nherriot 0:bcf2aa85d7f9 40 // Setting the control register bit 1 to true to activate the MMA8452
nherriot 0:bcf2aa85d7f9 41 int Accelerometer_MMA8452::activate()
nherriot 0:bcf2aa85d7f9 42 {
nherriot 1:ef026bf28798 43 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 0:bcf2aa85d7f9 44 char init[2];
nherriot 0:bcf2aa85d7f9 45 init[0] = CTRL_REG_1; // control register 1
nherriot 3:ffb0b1650ca2 46 init[1] = ACTIVE; // set to active
nherriot 3:ffb0b1650ca2 47
nherriot 3:ffb0b1650ca2 48 if(m_i2c.write(mcu_address,init,2) == 0)
nherriot 3:ffb0b1650ca2 49 {
nherriot 3:ffb0b1650ca2 50 return 0; // return 0 to indicate success
nherriot 3:ffb0b1650ca2 51 }
nherriot 3:ffb0b1650ca2 52 else
nherriot 3:ffb0b1650ca2 53 {
nherriot 3:ffb0b1650ca2 54 return 1; // crumbs it failed!!!
nherriot 3:ffb0b1650ca2 55 }
nherriot 3:ffb0b1650ca2 56
nherriot 3:ffb0b1650ca2 57 }
nherriot 3:ffb0b1650ca2 58
nherriot 3:ffb0b1650ca2 59
nherriot 3:ffb0b1650ca2 60 // Setting the control register bit 1 to true to activate the MMA8452
nherriot 3:ffb0b1650ca2 61 int Accelerometer_MMA8452::standby()
nherriot 3:ffb0b1650ca2 62 {
nherriot 3:ffb0b1650ca2 63 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 64 char init[2];
nherriot 3:ffb0b1650ca2 65 init[0] = CTRL_REG_1; // control register 1
nherriot 3:ffb0b1650ca2 66 init[1] = STANDBY; // set to standby
nherriot 1:ef026bf28798 67
nherriot 0:bcf2aa85d7f9 68 if(m_i2c.write(mcu_address,init,2) == 0)
nherriot 0:bcf2aa85d7f9 69 {
nherriot 0:bcf2aa85d7f9 70 // pc.printf("The initialisation worked");
nherriot 1:ef026bf28798 71 return 0; // return 0 to indicate success
nherriot 0:bcf2aa85d7f9 72 }
nherriot 0:bcf2aa85d7f9 73 else
nherriot 0:bcf2aa85d7f9 74 {
nherriot 0:bcf2aa85d7f9 75 // pc.printf("The initialisation failed");
nherriot 1:ef026bf28798 76 return 1; // crumbs it failed!!!
nherriot 0:bcf2aa85d7f9 77 }
nherriot 0:bcf2aa85d7f9 78
nherriot 0:bcf2aa85d7f9 79 }
nherriot 0:bcf2aa85d7f9 80
nherriot 0:bcf2aa85d7f9 81
nherriot 3:ffb0b1650ca2 82
nherriot 0:bcf2aa85d7f9 83 // Device initialization
nherriot 0:bcf2aa85d7f9 84 void Accelerometer_MMA8452::init()
nherriot 0:bcf2aa85d7f9 85 {
nherriot 0:bcf2aa85d7f9 86
nherriot 0:bcf2aa85d7f9 87 write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement
nherriot 0:bcf2aa85d7f9 88 write_reg(SR_STATUS, 0x00); // 120 Samples/Second
nherriot 0:bcf2aa85d7f9 89 write_reg(MODE_STATUS, 0x01); // Active Mode
nherriot 0:bcf2aa85d7f9 90
nherriot 0:bcf2aa85d7f9 91 }
nherriot 0:bcf2aa85d7f9 92
nherriot 3:ffb0b1650ca2 93 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 94 int Accelerometer_MMA8452::get_SystemMode(int& deviceSystemMode)
nherriot 3:ffb0b1650ca2 95 {
nherriot 3:ffb0b1650ca2 96 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 97 m_i2c.start();
nherriot 3:ffb0b1650ca2 98 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 99 {
nherriot 3:ffb0b1650ca2 100 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 101 }
nherriot 3:ffb0b1650ca2 102 if( m_i2c.write( SYSMOD) == 0)
nherriot 3:ffb0b1650ca2 103 {
nherriot 3:ffb0b1650ca2 104 return 1; // we failed to write 'status' to the chip
nherriot 3:ffb0b1650ca2 105 }
nherriot 3:ffb0b1650ca2 106 m_i2c.start();
nherriot 3:ffb0b1650ca2 107 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 108 {
nherriot 3:ffb0b1650ca2 109 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 110 }
nherriot 3:ffb0b1650ca2 111 deviceSystemMode = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 112 m_i2c.stop();
nherriot 3:ffb0b1650ca2 113 return 0;
nherriot 3:ffb0b1650ca2 114
nherriot 3:ffb0b1650ca2 115
nherriot 3:ffb0b1650ca2 116 }
nherriot 3:ffb0b1650ca2 117
nherriot 3:ffb0b1650ca2 118
nherriot 3:ffb0b1650ca2 119
nherriot 3:ffb0b1650ca2 120 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 121 int Accelerometer_MMA8452::get_Status(int& deviceStatus)
nherriot 3:ffb0b1650ca2 122 {
nherriot 3:ffb0b1650ca2 123 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 124 m_i2c.start();
nherriot 3:ffb0b1650ca2 125 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 126 {
nherriot 3:ffb0b1650ca2 127 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 128 }
nherriot 3:ffb0b1650ca2 129 if( m_i2c.write( STATUS) == 0)
nherriot 3:ffb0b1650ca2 130 {
nherriot 3:ffb0b1650ca2 131 return 1; // we failed to write 'status' to the chip
nherriot 3:ffb0b1650ca2 132 }
nherriot 3:ffb0b1650ca2 133 m_i2c.start();
nherriot 3:ffb0b1650ca2 134 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 135 {
nherriot 3:ffb0b1650ca2 136 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 137 }
nherriot 3:ffb0b1650ca2 138 deviceStatus = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 139 m_i2c.stop();
nherriot 3:ffb0b1650ca2 140 return 0;
nherriot 3:ffb0b1650ca2 141
nherriot 3:ffb0b1650ca2 142
nherriot 3:ffb0b1650ca2 143 }
nherriot 3:ffb0b1650ca2 144
nherriot 0:bcf2aa85d7f9 145
nherriot 1:ef026bf28798 146 // Get device ID
nherriot 2:66db0f91b215 147 int Accelerometer_MMA8452::get_DeviceID(int& deviceID)
nherriot 1:ef026bf28798 148 {
nherriot 1:ef026bf28798 149 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 1:ef026bf28798 150 m_i2c.start();
nherriot 3:ffb0b1650ca2 151 if( m_i2c.write( mcu_address & 0xFE) == 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();
nherriot 3:ffb0b1650ca2 160 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 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
nherriot 3:ffb0b1650ca2 196
nherriot 3:ffb0b1650ca2 197 // Reads x data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 198 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 199 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 200 // the raw data.
nherriot 3:ffb0b1650ca2 201 //int Accelerometer_MMA8452::read_x(int& xaxisLSB)
nherriot 3:ffb0b1650ca2 202 int Accelerometer_MMA8452::read_x_raw(char *xaxis)
nherriot 3:ffb0b1650ca2 203 {
nherriot 3:ffb0b1650ca2 204 char mcu_address = (MMA8452_ADDRESS<<1); // this is the slave address on the bus we want data from
nherriot 3:ffb0b1650ca2 205 char xaxis_buffer[2]; // this will contain data from that register
nherriot 3:ffb0b1650ca2 206 char xaxis_register[1];
nherriot 3:ffb0b1650ca2 207 xaxis_register[0] = OUT_X_MSB; // this is the register we want to get data from
nherriot 3:ffb0b1650ca2 208 //signed short s = 0;
nherriot 0:bcf2aa85d7f9 209
nherriot 3:ffb0b1650ca2 210 if(m_i2c.write(mcu_address,xaxis_register,1) == 0)
nherriot 3:ffb0b1650ca2 211 {
nherriot 3:ffb0b1650ca2 212 if(m_i2c.read(mcu_address,xaxis_buffer,2) == 0)
nherriot 3:ffb0b1650ca2 213 {
nherriot 3:ffb0b1650ca2 214 //strcpy(xaxis, xaxis_buffer);
nherriot 3:ffb0b1650ca2 215 memcpy(xaxis, xaxis_buffer, 2);
nherriot 3:ffb0b1650ca2 216 //xaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 217 //xaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 218 //s = *reinterpret_cast<short*>(&xaxis);
nherriot 3:ffb0b1650ca2 219 return 0; // great we got the two octets
nherriot 3:ffb0b1650ca2 220 }
nherriot 3:ffb0b1650ca2 221 else
nherriot 3:ffb0b1650ca2 222 {
nherriot 3:ffb0b1650ca2 223 xaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 224 xaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 225 return 1; // failed to read the 12 bit x value
nherriot 3:ffb0b1650ca2 226 }
nherriot 3:ffb0b1650ca2 227 }
nherriot 3:ffb0b1650ca2 228 else
nherriot 3:ffb0b1650ca2 229 {
nherriot 3:ffb0b1650ca2 230 xaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 231 xaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 232 return 1; // failed to write and request the OUT_X_MSB bit
nherriot 3:ffb0b1650ca2 233 }
nherriot 0:bcf2aa85d7f9 234 }
nherriot 0:bcf2aa85d7f9 235
nherriot 0:bcf2aa85d7f9 236
nherriot 3:ffb0b1650ca2 237 // Reads y data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 238 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 239 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 240 // the raw data.
nherriot 3:ffb0b1650ca2 241
nherriot 3:ffb0b1650ca2 242 int Accelerometer_MMA8452::read_y_raw(char *yaxis)
nherriot 0:bcf2aa85d7f9 243 {
nherriot 3:ffb0b1650ca2 244 char mcu_address = (MMA8452_ADDRESS<<1); // this is the slave address on the bus we want data from
nherriot 3:ffb0b1650ca2 245 char yaxis_buffer[2]; // this will contain data from that register
nherriot 3:ffb0b1650ca2 246 char yaxis_register[1];
nherriot 3:ffb0b1650ca2 247 yaxis_register[0] = OUT_Y_MSB; // this is the register we want to get data from
nherriot 3:ffb0b1650ca2 248 //signed short s = 0;
nherriot 3:ffb0b1650ca2 249
nherriot 3:ffb0b1650ca2 250 if(m_i2c.write(mcu_address,yaxis_register,1) == 0)
nherriot 3:ffb0b1650ca2 251 {
nherriot 3:ffb0b1650ca2 252 if(m_i2c.read(mcu_address,yaxis_buffer,2) == 0)
nherriot 3:ffb0b1650ca2 253 {
nherriot 3:ffb0b1650ca2 254 //strcpy(yaxis, yaxis_buffer);
nherriot 3:ffb0b1650ca2 255 memcpy(yaxis, yaxis_buffer, 2);
nherriot 3:ffb0b1650ca2 256 //yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 257 //yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 258 //s = *reinterpret_cast<short*>(&xaxis);
nherriot 3:ffb0b1650ca2 259 return 0; // great we got the two octets
nherriot 3:ffb0b1650ca2 260 }
nherriot 3:ffb0b1650ca2 261 else
nherriot 3:ffb0b1650ca2 262 {
nherriot 3:ffb0b1650ca2 263 yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 264 yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 265 return 1; // failed to read the 12 bit y value
nherriot 3:ffb0b1650ca2 266 }
nherriot 3:ffb0b1650ca2 267 }
nherriot 3:ffb0b1650ca2 268 else
nherriot 3:ffb0b1650ca2 269 {
nherriot 3:ffb0b1650ca2 270 yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 271 yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 272 return 1; // failed to write and request the OUT_Y_MSB bit
nherriot 3:ffb0b1650ca2 273 }
nherriot 3:ffb0b1650ca2 274 }
nherriot 3:ffb0b1650ca2 275
nherriot 0:bcf2aa85d7f9 276
nherriot 3:ffb0b1650ca2 277
nherriot 3:ffb0b1650ca2 278 // Reads z data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 279 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 280 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 281 // the raw data.
nherriot 3:ffb0b1650ca2 282
nherriot 3:ffb0b1650ca2 283 int Accelerometer_MMA8452::read_z_raw(char *zaxis)
nherriot 3:ffb0b1650ca2 284 {
nherriot 3:ffb0b1650ca2 285 char mcu_address = (MMA8452_ADDRESS<<1); // this is the slave address on the bus we want data from
nherriot 3:ffb0b1650ca2 286 char zaxis_buffer[2]; // this will contain data from that register
nherriot 3:ffb0b1650ca2 287 char zaxis_register[1];
nherriot 3:ffb0b1650ca2 288 zaxis_register[0] = OUT_Z_MSB; // this is the register we want to get data from
nherriot 3:ffb0b1650ca2 289 //signed short s = 0;
nherriot 0:bcf2aa85d7f9 290
nherriot 3:ffb0b1650ca2 291 if(m_i2c.write(mcu_address,zaxis_register,1) == 0)
nherriot 3:ffb0b1650ca2 292 {
nherriot 4:27aa3cd43234 293 //if(m_i2c.read(mcu_address,zaxis_buffer,2) == 0)
nherriot 4:27aa3cd43234 294 if(m_i2c.read(mcu_address,zaxis,2) == 0)
nherriot 3:ffb0b1650ca2 295 {
nherriot 3:ffb0b1650ca2 296 //strcpy(yaxis, yaxis_buffer);
nherriot 4:27aa3cd43234 297 //memcpy(zaxis, zaxis_buffer, 2);
nherriot 3:ffb0b1650ca2 298 //yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 299 //yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 300 //s = *reinterpret_cast<short*>(&xaxis);
nherriot 3:ffb0b1650ca2 301 return 0; // great we got the two octets
nherriot 3:ffb0b1650ca2 302 }
nherriot 3:ffb0b1650ca2 303 else
nherriot 3:ffb0b1650ca2 304 {
nherriot 3:ffb0b1650ca2 305 zaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 306 zaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 307 return 1; // failed to read the 12 bit y value
nherriot 3:ffb0b1650ca2 308 }
nherriot 3:ffb0b1650ca2 309 }
nherriot 3:ffb0b1650ca2 310 else
nherriot 3:ffb0b1650ca2 311 {
nherriot 3:ffb0b1650ca2 312 zaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 313 zaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 314 return 1; // failed to write and request the OUT_Y_MSB bit
nherriot 3:ffb0b1650ca2 315 }
nherriot 3:ffb0b1650ca2 316 }
nherriot 0:bcf2aa85d7f9 317
nherriot 0:bcf2aa85d7f9 318
nherriot 0:bcf2aa85d7f9 319
nherriot 0:bcf2aa85d7f9 320 // Reads y data
nherriot 0:bcf2aa85d7f9 321 int Accelerometer_MMA8452::read_y()
nherriot 0:bcf2aa85d7f9 322 {
nherriot 1:ef026bf28798 323 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 0:bcf2aa85d7f9 324
nherriot 0:bcf2aa85d7f9 325 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 326 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 327 m_i2c.write(OUT_Y_MSB); // Register to read
nherriot 0:bcf2aa85d7f9 328 m_i2c.start();
nherriot 0:bcf2aa85d7f9 329 m_i2c.write(mcu_address); // Read from device 0x99
nherriot 1:ef026bf28798 330 int y = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 331 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 332
nherriot 1:ef026bf28798 333 return y;
nherriot 0:bcf2aa85d7f9 334
nherriot 0:bcf2aa85d7f9 335 }
nherriot 0:bcf2aa85d7f9 336
nherriot 0:bcf2aa85d7f9 337
nherriot 0:bcf2aa85d7f9 338 // Reads z data
nherriot 0:bcf2aa85d7f9 339 int Accelerometer_MMA8452::read_z()
nherriot 0:bcf2aa85d7f9 340 {
nherriot 1:ef026bf28798 341 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 0:bcf2aa85d7f9 342
nherriot 0:bcf2aa85d7f9 343 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 344 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 345 m_i2c.write(OUT_Z_MSB); // Register to read
nherriot 0:bcf2aa85d7f9 346 m_i2c.start();
nherriot 0:bcf2aa85d7f9 347 m_i2c.write(mcu_address); // Read from device 0x99
nherriot 1:ef026bf28798 348 int z = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 349 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 350
nherriot 1:ef026bf28798 351 return z;
nherriot 0:bcf2aa85d7f9 352
nherriot 0:bcf2aa85d7f9 353 }
nherriot 0:bcf2aa85d7f9 354
nherriot 0:bcf2aa85d7f9 355
nherriot 0:bcf2aa85d7f9 356 // Reads xyz
nherriot 1:ef026bf28798 357 int Accelerometer_MMA8452::read_xyz(char *x, char *y, char *z)
nherriot 1:ef026bf28798 358 {
nherriot 1:ef026bf28798 359
nherriot 1:ef026bf28798 360
nherriot 1:ef026bf28798 361 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 1:ef026bf28798 362 char register_buffer[6] ={0,0,0,0,0,0};
nherriot 1:ef026bf28798 363 const char Addr_X = OUT_X_MSB;
nherriot 1:ef026bf28798 364 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 1:ef026bf28798 365 m_i2c.write(MMA8452_ADDRESS, &Addr_X, 1); // Pointer to the OUT_X_MSB register
nherriot 1:ef026bf28798 366
nherriot 1:ef026bf28798 367 if(m_i2c.write(mcu_address,&Addr_X,1) == 0)
nherriot 1:ef026bf28798 368 {
nherriot 1:ef026bf28798 369 if(m_i2c.read(mcu_address,register_buffer,6) == 0)
nherriot 1:ef026bf28798 370 {
nherriot 1:ef026bf28798 371 *x = register_buffer[1];
nherriot 1:ef026bf28798 372 *y = register_buffer[3];
nherriot 1:ef026bf28798 373 *z = register_buffer[5];
nherriot 1:ef026bf28798 374 return 0; // yahoooooo
nherriot 1:ef026bf28798 375 }
nherriot 1:ef026bf28798 376 else
nherriot 1:ef026bf28798 377 {
nherriot 1:ef026bf28798 378 return 1; // failed oh nooo!
nherriot 1:ef026bf28798 379 }
nherriot 1:ef026bf28798 380 }
nherriot 1:ef026bf28798 381 else
nherriot 1:ef026bf28798 382 {
nherriot 1:ef026bf28798 383 return 1; // failed oh nooo!
nherriot 1:ef026bf28798 384 }
nherriot 0:bcf2aa85d7f9 385
nherriot 1:ef026bf28798 386 }
nherriot 0:bcf2aa85d7f9 387
nherriot 0:bcf2aa85d7f9 388 // Write register (The device must be placed in Standby Mode to change the value of the registers)
nherriot 0:bcf2aa85d7f9 389 void Accelerometer_MMA8452::write_reg(char addr, char data)
nherriot 0:bcf2aa85d7f9 390 {
nherriot 0:bcf2aa85d7f9 391
nherriot 0:bcf2aa85d7f9 392 char cmd[2] = {0, 0};
nherriot 0:bcf2aa85d7f9 393
nherriot 0:bcf2aa85d7f9 394 cmd[0] = MODE_STATUS;
nherriot 0:bcf2aa85d7f9 395 cmd[1] = 0x00; // Standby Mode on
nherriot 0:bcf2aa85d7f9 396 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 397
nherriot 0:bcf2aa85d7f9 398 cmd[0] = addr;
nherriot 0:bcf2aa85d7f9 399 cmd[1] = data; // New value of the register
nherriot 0:bcf2aa85d7f9 400 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 401
nherriot 0:bcf2aa85d7f9 402 cmd[0] = MODE_STATUS;
nherriot 0:bcf2aa85d7f9 403 cmd[1] = 0x01; // Active Mode on
nherriot 0:bcf2aa85d7f9 404 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 405
nherriot 0:bcf2aa85d7f9 406 }
nherriot 0:bcf2aa85d7f9 407
nherriot 0:bcf2aa85d7f9 408
nherriot 0:bcf2aa85d7f9 409
nherriot 0:bcf2aa85d7f9 410 // Read from specified MMA7660FC register
nherriot 0:bcf2aa85d7f9 411 char Accelerometer_MMA8452::read_reg(char addr)
nherriot 0:bcf2aa85d7f9 412 {
nherriot 0:bcf2aa85d7f9 413
nherriot 0:bcf2aa85d7f9 414 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 415 m_i2c.write(0x98); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 416 m_i2c.write(addr); // Register to read
nherriot 0:bcf2aa85d7f9 417 m_i2c.start();
nherriot 0:bcf2aa85d7f9 418 m_i2c.write(0x99); // Read from device 0x99
nherriot 0:bcf2aa85d7f9 419 char c = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 420 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 421
nherriot 0:bcf2aa85d7f9 422 return c;
nherriot 0:bcf2aa85d7f9 423
nherriot 0:bcf2aa85d7f9 424 }
nherriot 0:bcf2aa85d7f9 425
nherriot 0:bcf2aa85d7f9 426
nherriot 0:bcf2aa85d7f9 427
nherriot 0:bcf2aa85d7f9 428
nherriot 0:bcf2aa85d7f9 429
nherriot 0:bcf2aa85d7f9 430