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