prueba comunicación i^2c

Dependencies:   mbed

Committer:
fonz626
Date:
Sat May 16 21:37:52 2015 +0000
Revision:
0:b52c6d67b596
prueba comunicaci?n 1^2c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fonz626 0:b52c6d67b596 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
fonz626 0:b52c6d67b596 2 *
fonz626 0:b52c6d67b596 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
fonz626 0:b52c6d67b596 4 * and associated documentation files (the "Software"), to deal in the Software without
fonz626 0:b52c6d67b596 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
fonz626 0:b52c6d67b596 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
fonz626 0:b52c6d67b596 7 * Software is furnished to do so, subject to the following conditions:
fonz626 0:b52c6d67b596 8 *
fonz626 0:b52c6d67b596 9 * The above copyright notice and this permission notice shall be included in all copies or
fonz626 0:b52c6d67b596 10 * substantial portions of the Software.
fonz626 0:b52c6d67b596 11 *
fonz626 0:b52c6d67b596 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
fonz626 0:b52c6d67b596 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
fonz626 0:b52c6d67b596 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
fonz626 0:b52c6d67b596 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
fonz626 0:b52c6d67b596 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fonz626 0:b52c6d67b596 17 */
fonz626 0:b52c6d67b596 18
fonz626 0:b52c6d67b596 19 #include "MMA8451Q.h"
fonz626 0:b52c6d67b596 20
fonz626 0:b52c6d67b596 21 #define REG_WHO_AM_I 0x0D
fonz626 0:b52c6d67b596 22 #define REG_CTRL_REG_1 0x2A
fonz626 0:b52c6d67b596 23 #define REG_OUT_X_MSB 0x01
fonz626 0:b52c6d67b596 24 #define REG_OUT_Y_MSB 0x03
fonz626 0:b52c6d67b596 25 #define REG_OUT_Z_MSB 0x05
fonz626 0:b52c6d67b596 26
fonz626 0:b52c6d67b596 27 #define UINT14_MAX 16383
fonz626 0:b52c6d67b596 28
fonz626 0:b52c6d67b596 29 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
fonz626 0:b52c6d67b596 30 // activate the peripheral
fonz626 0:b52c6d67b596 31 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
fonz626 0:b52c6d67b596 32 writeRegs(data, 2);
fonz626 0:b52c6d67b596 33 }
fonz626 0:b52c6d67b596 34
fonz626 0:b52c6d67b596 35 MMA8451Q::~MMA8451Q() { }
fonz626 0:b52c6d67b596 36
fonz626 0:b52c6d67b596 37 uint8_t MMA8451Q::getWhoAmI() {
fonz626 0:b52c6d67b596 38 uint8_t who_am_i = 0;
fonz626 0:b52c6d67b596 39 readRegs(REG_WHO_AM_I, &who_am_i, 1);
fonz626 0:b52c6d67b596 40 return who_am_i;
fonz626 0:b52c6d67b596 41 }
fonz626 0:b52c6d67b596 42
fonz626 0:b52c6d67b596 43 float MMA8451Q::getAccX() {
fonz626 0:b52c6d67b596 44 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
fonz626 0:b52c6d67b596 45 }
fonz626 0:b52c6d67b596 46
fonz626 0:b52c6d67b596 47 float MMA8451Q::getAccY() {
fonz626 0:b52c6d67b596 48 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
fonz626 0:b52c6d67b596 49 }
fonz626 0:b52c6d67b596 50
fonz626 0:b52c6d67b596 51 float MMA8451Q::getAccZ() {
fonz626 0:b52c6d67b596 52 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
fonz626 0:b52c6d67b596 53 }
fonz626 0:b52c6d67b596 54
fonz626 0:b52c6d67b596 55 void MMA8451Q::getAccAllAxis(float * res) {
fonz626 0:b52c6d67b596 56 res[0] = getAccX();
fonz626 0:b52c6d67b596 57 res[1] = getAccY();
fonz626 0:b52c6d67b596 58 res[2] = getAccZ();
fonz626 0:b52c6d67b596 59 }
fonz626 0:b52c6d67b596 60
fonz626 0:b52c6d67b596 61 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
fonz626 0:b52c6d67b596 62 int16_t acc;
fonz626 0:b52c6d67b596 63 uint8_t res[2];
fonz626 0:b52c6d67b596 64 readRegs(addr, res, 2);
fonz626 0:b52c6d67b596 65
fonz626 0:b52c6d67b596 66 acc = (res[0] << 6) | (res[1] >> 2);
fonz626 0:b52c6d67b596 67 if (acc > UINT14_MAX/2)
fonz626 0:b52c6d67b596 68 acc -= UINT14_MAX;
fonz626 0:b52c6d67b596 69
fonz626 0:b52c6d67b596 70 return acc;
fonz626 0:b52c6d67b596 71 }
fonz626 0:b52c6d67b596 72
fonz626 0:b52c6d67b596 73 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
fonz626 0:b52c6d67b596 74 char t[1] = {addr};
fonz626 0:b52c6d67b596 75 m_i2c.write(m_addr, t, 1, true);
fonz626 0:b52c6d67b596 76 m_i2c.read(m_addr, (char *)data, len);
fonz626 0:b52c6d67b596 77 }
fonz626 0:b52c6d67b596 78
fonz626 0:b52c6d67b596 79 void MMA8451Q::writeRegs(uint8_t * data, int len) {
fonz626 0:b52c6d67b596 80 m_i2c.write(m_addr, (char *)data, len);
fonz626 0:b52c6d67b596 81 }