Works with 8g

Fork of MMA8452 by Ashley Mills

Committer:
nherriot
Date:
Wed Oct 16 18:55:16 2013 +0000
Revision:
5:b3d0abd97e55
Parent:
4:27aa3cd43234
Child:
6:f6bde04bf8be
Child:
7:8aa5123d403f
adding method for getting control reg 1.;

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 5:b3d0abd97e55 60 // 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 61 // resisters from 0x01 to 0x06. When F_READ is '0' then all 6 registers will be read.
nherriot 5:b3d0abd97e55 62
nherriot 5:b3d0abd97e55 63 int Accelerometer_MMA8452::get_CTRL_Reg1(int& CTRL_Reg)
nherriot 5:b3d0abd97e55 64 {
nherriot 5:b3d0abd97e55 65 {
nherriot 5:b3d0abd97e55 66 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 5:b3d0abd97e55 67 m_i2c.start();
nherriot 5:b3d0abd97e55 68 if( m_i2c.write( mcu_address & 0xFE) == 0) // just good practice to force bit 1 to a '0' by ANDing with 0xFE
nherriot 5:b3d0abd97e55 69 {
nherriot 5:b3d0abd97e55 70 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 5:b3d0abd97e55 71 }
nherriot 5:b3d0abd97e55 72 if( m_i2c.write( CTRL_REG_1) == 0)
nherriot 5:b3d0abd97e55 73 {
nherriot 5:b3d0abd97e55 74 return 1; // we failed to write 'status' to the chip
nherriot 5:b3d0abd97e55 75 }
nherriot 5:b3d0abd97e55 76 m_i2c.start();
nherriot 5:b3d0abd97e55 77 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 5:b3d0abd97e55 78 {
nherriot 5:b3d0abd97e55 79 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 80 }
nherriot 5:b3d0abd97e55 81 CTRL_Reg = m_i2c.read(0);
nherriot 5:b3d0abd97e55 82 m_i2c.stop();
nherriot 5:b3d0abd97e55 83 return 0;
nherriot 5:b3d0abd97e55 84
nherriot 5:b3d0abd97e55 85
nherriot 5:b3d0abd97e55 86 }
nherriot 5:b3d0abd97e55 87
nherriot 5:b3d0abd97e55 88
nherriot 5:b3d0abd97e55 89
nherriot 5:b3d0abd97e55 90
nherriot 5:b3d0abd97e55 91 }
nherriot 5:b3d0abd97e55 92
nherriot 3:ffb0b1650ca2 93 // Setting the control register bit 1 to true to activate the MMA8452
nherriot 3:ffb0b1650ca2 94 int Accelerometer_MMA8452::standby()
nherriot 3:ffb0b1650ca2 95 {
nherriot 3:ffb0b1650ca2 96 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 97 char init[2];
nherriot 3:ffb0b1650ca2 98 init[0] = CTRL_REG_1; // control register 1
nherriot 3:ffb0b1650ca2 99 init[1] = STANDBY; // set to standby
nherriot 1:ef026bf28798 100
nherriot 0:bcf2aa85d7f9 101 if(m_i2c.write(mcu_address,init,2) == 0)
nherriot 0:bcf2aa85d7f9 102 {
nherriot 0:bcf2aa85d7f9 103 // pc.printf("The initialisation worked");
nherriot 1:ef026bf28798 104 return 0; // return 0 to indicate success
nherriot 0:bcf2aa85d7f9 105 }
nherriot 0:bcf2aa85d7f9 106 else
nherriot 0:bcf2aa85d7f9 107 {
nherriot 0:bcf2aa85d7f9 108 // pc.printf("The initialisation failed");
nherriot 1:ef026bf28798 109 return 1; // crumbs it failed!!!
nherriot 0:bcf2aa85d7f9 110 }
nherriot 0:bcf2aa85d7f9 111
nherriot 0:bcf2aa85d7f9 112 }
nherriot 0:bcf2aa85d7f9 113
nherriot 0:bcf2aa85d7f9 114
nherriot 3:ffb0b1650ca2 115
nherriot 0:bcf2aa85d7f9 116 // Device initialization
nherriot 0:bcf2aa85d7f9 117 void Accelerometer_MMA8452::init()
nherriot 0:bcf2aa85d7f9 118 {
nherriot 0:bcf2aa85d7f9 119
nherriot 0:bcf2aa85d7f9 120 write_reg(INTSU_STATUS, 0x10); // automatic interrupt after every measurement
nherriot 0:bcf2aa85d7f9 121 write_reg(SR_STATUS, 0x00); // 120 Samples/Second
nherriot 0:bcf2aa85d7f9 122 write_reg(MODE_STATUS, 0x01); // Active Mode
nherriot 0:bcf2aa85d7f9 123
nherriot 0:bcf2aa85d7f9 124 }
nherriot 0:bcf2aa85d7f9 125
nherriot 3:ffb0b1650ca2 126 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 127 int Accelerometer_MMA8452::get_SystemMode(int& deviceSystemMode)
nherriot 3:ffb0b1650ca2 128 {
nherriot 3:ffb0b1650ca2 129 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 130 m_i2c.start();
nherriot 3:ffb0b1650ca2 131 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 132 {
nherriot 3:ffb0b1650ca2 133 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 134 }
nherriot 3:ffb0b1650ca2 135 if( m_i2c.write( SYSMOD) == 0)
nherriot 3:ffb0b1650ca2 136 {
nherriot 3:ffb0b1650ca2 137 return 1; // we failed to write 'status' to the chip
nherriot 3:ffb0b1650ca2 138 }
nherriot 3:ffb0b1650ca2 139 m_i2c.start();
nherriot 3:ffb0b1650ca2 140 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 141 {
nherriot 3:ffb0b1650ca2 142 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 143 }
nherriot 3:ffb0b1650ca2 144 deviceSystemMode = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 145 m_i2c.stop();
nherriot 3:ffb0b1650ca2 146 return 0;
nherriot 3:ffb0b1650ca2 147
nherriot 3:ffb0b1650ca2 148
nherriot 3:ffb0b1650ca2 149 }
nherriot 3:ffb0b1650ca2 150
nherriot 3:ffb0b1650ca2 151
nherriot 3:ffb0b1650ca2 152
nherriot 3:ffb0b1650ca2 153 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
nherriot 3:ffb0b1650ca2 154 int Accelerometer_MMA8452::get_Status(int& deviceStatus)
nherriot 3:ffb0b1650ca2 155 {
nherriot 3:ffb0b1650ca2 156 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 157 m_i2c.start();
nherriot 3:ffb0b1650ca2 158 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 159 {
nherriot 3:ffb0b1650ca2 160 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 161 }
nherriot 3:ffb0b1650ca2 162 if( m_i2c.write( STATUS) == 0)
nherriot 3:ffb0b1650ca2 163 {
nherriot 3:ffb0b1650ca2 164 return 1; // we failed to write 'status' to the chip
nherriot 3:ffb0b1650ca2 165 }
nherriot 3:ffb0b1650ca2 166 m_i2c.start();
nherriot 3:ffb0b1650ca2 167 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 168 {
nherriot 3:ffb0b1650ca2 169 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 170 }
nherriot 3:ffb0b1650ca2 171 deviceStatus = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 172 m_i2c.stop();
nherriot 3:ffb0b1650ca2 173 return 0;
nherriot 3:ffb0b1650ca2 174
nherriot 3:ffb0b1650ca2 175
nherriot 3:ffb0b1650ca2 176 }
nherriot 3:ffb0b1650ca2 177
nherriot 0:bcf2aa85d7f9 178
nherriot 1:ef026bf28798 179 // Get device ID
nherriot 2:66db0f91b215 180 int Accelerometer_MMA8452::get_DeviceID(int& deviceID)
nherriot 1:ef026bf28798 181 {
nherriot 1:ef026bf28798 182 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 1:ef026bf28798 183 m_i2c.start();
nherriot 3:ffb0b1650ca2 184 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 185 {
nherriot 3:ffb0b1650ca2 186 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 1:ef026bf28798 187 }
nherriot 1:ef026bf28798 188 if( m_i2c.write( WHO_AM_I) == 0)
nherriot 1:ef026bf28798 189 {
nherriot 3:ffb0b1650ca2 190 return 1; // we failed to write 'who am i' to the chip
nherriot 1:ef026bf28798 191 }
nherriot 1:ef026bf28798 192 m_i2c.start();
nherriot 3:ffb0b1650ca2 193 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 194 {
nherriot 3:ffb0b1650ca2 195 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 196 }
nherriot 2:66db0f91b215 197 deviceID = m_i2c.read(0);
nherriot 1:ef026bf28798 198 m_i2c.stop();
nherriot 1:ef026bf28798 199 return 0;
nherriot 1:ef026bf28798 200
nherriot 1:ef026bf28798 201 }
nherriot 1:ef026bf28798 202
nherriot 1:ef026bf28798 203
nherriot 3:ffb0b1650ca2 204 /*
nherriot 3:ffb0b1650ca2 205 // Reads x data
nherriot 3:ffb0b1650ca2 206 int Accelerometer_MMA8452::read_x(int& xaxisLSB)
nherriot 0:bcf2aa85d7f9 207 {
nherriot 3:ffb0b1650ca2 208 char mcu_address = (MMA8452_ADDRESS<<1);
nherriot 3:ffb0b1650ca2 209 m_i2c.start();
nherriot 3:ffb0b1650ca2 210 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 211 {
nherriot 3:ffb0b1650ca2 212 return 1; // we failed to write the mcu address on the bus to initiate dialogue
nherriot 3:ffb0b1650ca2 213 }
nherriot 3:ffb0b1650ca2 214 if( m_i2c.write( OUT_X_MSB) == 0)
nherriot 3:ffb0b1650ca2 215 {
nherriot 3:ffb0b1650ca2 216 return 1; // we failed to write 'X axis LSB' to the chip
nherriot 3:ffb0b1650ca2 217 }
nherriot 3:ffb0b1650ca2 218 m_i2c.start();
nherriot 3:ffb0b1650ca2 219 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 220 {
nherriot 3:ffb0b1650ca2 221 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 222 }
nherriot 3:ffb0b1650ca2 223 xaxisLSB = m_i2c.read(0);
nherriot 3:ffb0b1650ca2 224 m_i2c.stop();
nherriot 3:ffb0b1650ca2 225 return 0;
nherriot 3:ffb0b1650ca2 226 }
nherriot 3:ffb0b1650ca2 227 */
nherriot 0:bcf2aa85d7f9 228
nherriot 3:ffb0b1650ca2 229
nherriot 3:ffb0b1650ca2 230 // Reads x data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 231 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 232 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 233 // the raw data.
nherriot 3:ffb0b1650ca2 234 //int Accelerometer_MMA8452::read_x(int& xaxisLSB)
nherriot 3:ffb0b1650ca2 235 int Accelerometer_MMA8452::read_x_raw(char *xaxis)
nherriot 3:ffb0b1650ca2 236 {
nherriot 3:ffb0b1650ca2 237 char mcu_address = (MMA8452_ADDRESS<<1); // this is the slave address on the bus we want data from
nherriot 3:ffb0b1650ca2 238 char xaxis_buffer[2]; // this will contain data from that register
nherriot 3:ffb0b1650ca2 239 char xaxis_register[1];
nherriot 3:ffb0b1650ca2 240 xaxis_register[0] = OUT_X_MSB; // this is the register we want to get data from
nherriot 3:ffb0b1650ca2 241 //signed short s = 0;
nherriot 0:bcf2aa85d7f9 242
nherriot 3:ffb0b1650ca2 243 if(m_i2c.write(mcu_address,xaxis_register,1) == 0)
nherriot 3:ffb0b1650ca2 244 {
nherriot 3:ffb0b1650ca2 245 if(m_i2c.read(mcu_address,xaxis_buffer,2) == 0)
nherriot 3:ffb0b1650ca2 246 {
nherriot 3:ffb0b1650ca2 247 //strcpy(xaxis, xaxis_buffer);
nherriot 3:ffb0b1650ca2 248 memcpy(xaxis, xaxis_buffer, 2);
nherriot 3:ffb0b1650ca2 249 //xaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 250 //xaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 251 //s = *reinterpret_cast<short*>(&xaxis);
nherriot 3:ffb0b1650ca2 252 return 0; // great we got the two octets
nherriot 3:ffb0b1650ca2 253 }
nherriot 3:ffb0b1650ca2 254 else
nherriot 3:ffb0b1650ca2 255 {
nherriot 3:ffb0b1650ca2 256 xaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 257 xaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 258 return 1; // failed to read the 12 bit x value
nherriot 3:ffb0b1650ca2 259 }
nherriot 3:ffb0b1650ca2 260 }
nherriot 3:ffb0b1650ca2 261 else
nherriot 3:ffb0b1650ca2 262 {
nherriot 3:ffb0b1650ca2 263 xaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 264 xaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 265 return 1; // failed to write and request the OUT_X_MSB bit
nherriot 3:ffb0b1650ca2 266 }
nherriot 0:bcf2aa85d7f9 267 }
nherriot 0:bcf2aa85d7f9 268
nherriot 0:bcf2aa85d7f9 269
nherriot 3:ffb0b1650ca2 270 // Reads y data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 271 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 272 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 273 // the raw data.
nherriot 3:ffb0b1650ca2 274
nherriot 3:ffb0b1650ca2 275 int Accelerometer_MMA8452::read_y_raw(char *yaxis)
nherriot 0:bcf2aa85d7f9 276 {
nherriot 3:ffb0b1650ca2 277 char mcu_address = (MMA8452_ADDRESS<<1); // this is the slave address on the bus we want data from
nherriot 3:ffb0b1650ca2 278 char yaxis_buffer[2]; // this will contain data from that register
nherriot 3:ffb0b1650ca2 279 char yaxis_register[1];
nherriot 3:ffb0b1650ca2 280 yaxis_register[0] = OUT_Y_MSB; // this is the register we want to get data from
nherriot 3:ffb0b1650ca2 281 //signed short s = 0;
nherriot 3:ffb0b1650ca2 282
nherriot 3:ffb0b1650ca2 283 if(m_i2c.write(mcu_address,yaxis_register,1) == 0)
nherriot 3:ffb0b1650ca2 284 {
nherriot 3:ffb0b1650ca2 285 if(m_i2c.read(mcu_address,yaxis_buffer,2) == 0)
nherriot 3:ffb0b1650ca2 286 {
nherriot 3:ffb0b1650ca2 287 //strcpy(yaxis, yaxis_buffer);
nherriot 3:ffb0b1650ca2 288 memcpy(yaxis, yaxis_buffer, 2);
nherriot 3:ffb0b1650ca2 289 //yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 290 //yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 291 //s = *reinterpret_cast<short*>(&xaxis);
nherriot 3:ffb0b1650ca2 292 return 0; // great we got the two octets
nherriot 3:ffb0b1650ca2 293 }
nherriot 3:ffb0b1650ca2 294 else
nherriot 3:ffb0b1650ca2 295 {
nherriot 3:ffb0b1650ca2 296 yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 297 yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 298 return 1; // failed to read the 12 bit y value
nherriot 3:ffb0b1650ca2 299 }
nherriot 3:ffb0b1650ca2 300 }
nherriot 3:ffb0b1650ca2 301 else
nherriot 3:ffb0b1650ca2 302 {
nherriot 3:ffb0b1650ca2 303 yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 304 yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 305 return 1; // failed to write and request the OUT_Y_MSB bit
nherriot 3:ffb0b1650ca2 306 }
nherriot 3:ffb0b1650ca2 307 }
nherriot 3:ffb0b1650ca2 308
nherriot 0:bcf2aa85d7f9 309
nherriot 3:ffb0b1650ca2 310
nherriot 3:ffb0b1650ca2 311 // Reads z data. This method reads two registers containing the x-axis values from the accelerometer.
nherriot 3:ffb0b1650ca2 312 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
nherriot 3:ffb0b1650ca2 313 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
nherriot 3:ffb0b1650ca2 314 // the raw data.
nherriot 3:ffb0b1650ca2 315
nherriot 3:ffb0b1650ca2 316 int Accelerometer_MMA8452::read_z_raw(char *zaxis)
nherriot 3:ffb0b1650ca2 317 {
nherriot 3:ffb0b1650ca2 318 char mcu_address = (MMA8452_ADDRESS<<1); // this is the slave address on the bus we want data from
nherriot 3:ffb0b1650ca2 319 char zaxis_buffer[2]; // this will contain data from that register
nherriot 3:ffb0b1650ca2 320 char zaxis_register[1];
nherriot 3:ffb0b1650ca2 321 zaxis_register[0] = OUT_Z_MSB; // this is the register we want to get data from
nherriot 3:ffb0b1650ca2 322 //signed short s = 0;
nherriot 0:bcf2aa85d7f9 323
nherriot 3:ffb0b1650ca2 324 if(m_i2c.write(mcu_address,zaxis_register,1) == 0)
nherriot 3:ffb0b1650ca2 325 {
nherriot 4:27aa3cd43234 326 //if(m_i2c.read(mcu_address,zaxis_buffer,2) == 0)
nherriot 4:27aa3cd43234 327 if(m_i2c.read(mcu_address,zaxis,2) == 0)
nherriot 3:ffb0b1650ca2 328 {
nherriot 3:ffb0b1650ca2 329 //strcpy(yaxis, yaxis_buffer);
nherriot 4:27aa3cd43234 330 //memcpy(zaxis, zaxis_buffer, 2);
nherriot 3:ffb0b1650ca2 331 //yaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 332 //yaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 333 //s = *reinterpret_cast<short*>(&xaxis);
nherriot 3:ffb0b1650ca2 334 return 0; // great we got the two octets
nherriot 3:ffb0b1650ca2 335 }
nherriot 3:ffb0b1650ca2 336 else
nherriot 3:ffb0b1650ca2 337 {
nherriot 3:ffb0b1650ca2 338 zaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 339 zaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 340 return 1; // failed to read the 12 bit y value
nherriot 3:ffb0b1650ca2 341 }
nherriot 3:ffb0b1650ca2 342 }
nherriot 3:ffb0b1650ca2 343 else
nherriot 3:ffb0b1650ca2 344 {
nherriot 3:ffb0b1650ca2 345 zaxis[0] = 0x00; // make sure the array is set to zero
nherriot 3:ffb0b1650ca2 346 zaxis[1] = 0x00;
nherriot 3:ffb0b1650ca2 347 return 1; // failed to write and request the OUT_Y_MSB bit
nherriot 3:ffb0b1650ca2 348 }
nherriot 3:ffb0b1650ca2 349 }
nherriot 0:bcf2aa85d7f9 350
nherriot 0:bcf2aa85d7f9 351
nherriot 0:bcf2aa85d7f9 352
nherriot 0:bcf2aa85d7f9 353 // Reads y data
nherriot 0:bcf2aa85d7f9 354 int Accelerometer_MMA8452::read_y()
nherriot 0:bcf2aa85d7f9 355 {
nherriot 1:ef026bf28798 356 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 0:bcf2aa85d7f9 357
nherriot 0:bcf2aa85d7f9 358 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 359 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 360 m_i2c.write(OUT_Y_MSB); // Register to read
nherriot 0:bcf2aa85d7f9 361 m_i2c.start();
nherriot 0:bcf2aa85d7f9 362 m_i2c.write(mcu_address); // Read from device 0x99
nherriot 1:ef026bf28798 363 int y = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 364 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 365
nherriot 1:ef026bf28798 366 return y;
nherriot 0:bcf2aa85d7f9 367
nherriot 0:bcf2aa85d7f9 368 }
nherriot 0:bcf2aa85d7f9 369
nherriot 0:bcf2aa85d7f9 370
nherriot 0:bcf2aa85d7f9 371 // Reads z data
nherriot 0:bcf2aa85d7f9 372 int Accelerometer_MMA8452::read_z()
nherriot 0:bcf2aa85d7f9 373 {
nherriot 1:ef026bf28798 374 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 0:bcf2aa85d7f9 375
nherriot 0:bcf2aa85d7f9 376 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 377 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 378 m_i2c.write(OUT_Z_MSB); // Register to read
nherriot 0:bcf2aa85d7f9 379 m_i2c.start();
nherriot 0:bcf2aa85d7f9 380 m_i2c.write(mcu_address); // Read from device 0x99
nherriot 1:ef026bf28798 381 int z = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 382 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 383
nherriot 1:ef026bf28798 384 return z;
nherriot 0:bcf2aa85d7f9 385
nherriot 0:bcf2aa85d7f9 386 }
nherriot 0:bcf2aa85d7f9 387
nherriot 0:bcf2aa85d7f9 388
nherriot 0:bcf2aa85d7f9 389 // Reads xyz
nherriot 1:ef026bf28798 390 int Accelerometer_MMA8452::read_xyz(char *x, char *y, char *z)
nherriot 1:ef026bf28798 391 {
nherriot 1:ef026bf28798 392
nherriot 1:ef026bf28798 393
nherriot 1:ef026bf28798 394 char mcu_address = (MMA8452_ADDRESS <<1);
nherriot 1:ef026bf28798 395 char register_buffer[6] ={0,0,0,0,0,0};
nherriot 1:ef026bf28798 396 const char Addr_X = OUT_X_MSB;
nherriot 1:ef026bf28798 397 m_i2c.write(mcu_address); // A write to device 0x98
nherriot 1:ef026bf28798 398 m_i2c.write(MMA8452_ADDRESS, &Addr_X, 1); // Pointer to the OUT_X_MSB register
nherriot 1:ef026bf28798 399
nherriot 1:ef026bf28798 400 if(m_i2c.write(mcu_address,&Addr_X,1) == 0)
nherriot 1:ef026bf28798 401 {
nherriot 1:ef026bf28798 402 if(m_i2c.read(mcu_address,register_buffer,6) == 0)
nherriot 1:ef026bf28798 403 {
nherriot 1:ef026bf28798 404 *x = register_buffer[1];
nherriot 1:ef026bf28798 405 *y = register_buffer[3];
nherriot 1:ef026bf28798 406 *z = register_buffer[5];
nherriot 1:ef026bf28798 407 return 0; // yahoooooo
nherriot 1:ef026bf28798 408 }
nherriot 1:ef026bf28798 409 else
nherriot 1:ef026bf28798 410 {
nherriot 1:ef026bf28798 411 return 1; // failed oh nooo!
nherriot 1:ef026bf28798 412 }
nherriot 1:ef026bf28798 413 }
nherriot 1:ef026bf28798 414 else
nherriot 1:ef026bf28798 415 {
nherriot 1:ef026bf28798 416 return 1; // failed oh nooo!
nherriot 1:ef026bf28798 417 }
nherriot 0:bcf2aa85d7f9 418
nherriot 1:ef026bf28798 419 }
nherriot 0:bcf2aa85d7f9 420
nherriot 0:bcf2aa85d7f9 421 // Write register (The device must be placed in Standby Mode to change the value of the registers)
nherriot 0:bcf2aa85d7f9 422 void Accelerometer_MMA8452::write_reg(char addr, char data)
nherriot 0:bcf2aa85d7f9 423 {
nherriot 0:bcf2aa85d7f9 424
nherriot 0:bcf2aa85d7f9 425 char cmd[2] = {0, 0};
nherriot 0:bcf2aa85d7f9 426
nherriot 0:bcf2aa85d7f9 427 cmd[0] = MODE_STATUS;
nherriot 0:bcf2aa85d7f9 428 cmd[1] = 0x00; // Standby Mode on
nherriot 0:bcf2aa85d7f9 429 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 430
nherriot 0:bcf2aa85d7f9 431 cmd[0] = addr;
nherriot 0:bcf2aa85d7f9 432 cmd[1] = data; // New value of the register
nherriot 0:bcf2aa85d7f9 433 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 434
nherriot 0:bcf2aa85d7f9 435 cmd[0] = MODE_STATUS;
nherriot 0:bcf2aa85d7f9 436 cmd[1] = 0x01; // Active Mode on
nherriot 0:bcf2aa85d7f9 437 m_i2c.write(MMA8452_ADDRESS, cmd, 2);
nherriot 0:bcf2aa85d7f9 438
nherriot 0:bcf2aa85d7f9 439 }
nherriot 0:bcf2aa85d7f9 440
nherriot 0:bcf2aa85d7f9 441
nherriot 0:bcf2aa85d7f9 442
nherriot 0:bcf2aa85d7f9 443 // Read from specified MMA7660FC register
nherriot 0:bcf2aa85d7f9 444 char Accelerometer_MMA8452::read_reg(char addr)
nherriot 0:bcf2aa85d7f9 445 {
nherriot 0:bcf2aa85d7f9 446
nherriot 0:bcf2aa85d7f9 447 m_i2c.start(); // Start
nherriot 0:bcf2aa85d7f9 448 m_i2c.write(0x98); // A write to device 0x98
nherriot 0:bcf2aa85d7f9 449 m_i2c.write(addr); // Register to read
nherriot 0:bcf2aa85d7f9 450 m_i2c.start();
nherriot 0:bcf2aa85d7f9 451 m_i2c.write(0x99); // Read from device 0x99
nherriot 0:bcf2aa85d7f9 452 char c = m_i2c.read(0); // Read the data
nherriot 0:bcf2aa85d7f9 453 m_i2c.stop();
nherriot 0:bcf2aa85d7f9 454
nherriot 0:bcf2aa85d7f9 455 return c;
nherriot 0:bcf2aa85d7f9 456
nherriot 0:bcf2aa85d7f9 457 }
nherriot 0:bcf2aa85d7f9 458
nherriot 0:bcf2aa85d7f9 459
nherriot 0:bcf2aa85d7f9 460
nherriot 0:bcf2aa85d7f9 461
nherriot 0:bcf2aa85d7f9 462
nherriot 0:bcf2aa85d7f9 463