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 #ifndef MMA8451Q_H
fonz626 0:b52c6d67b596 20 #define MMA8451Q_H
fonz626 0:b52c6d67b596 21
fonz626 0:b52c6d67b596 22 #include "mbed.h"
fonz626 0:b52c6d67b596 23
fonz626 0:b52c6d67b596 24 /**
fonz626 0:b52c6d67b596 25 * MMA8451Q accelerometer example
fonz626 0:b52c6d67b596 26 *
fonz626 0:b52c6d67b596 27 * @code
fonz626 0:b52c6d67b596 28 * #include "mbed.h"
fonz626 0:b52c6d67b596 29 * #include "MMA8451Q.h"
fonz626 0:b52c6d67b596 30 *
fonz626 0:b52c6d67b596 31 * #define MMA8451_I2C_ADDRESS (0x1d<<1)
fonz626 0:b52c6d67b596 32 *
fonz626 0:b52c6d67b596 33 * int main(void) {
fonz626 0:b52c6d67b596 34 *
fonz626 0:b52c6d67b596 35 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
fonz626 0:b52c6d67b596 36 * PwmOut rled(LED_RED);
fonz626 0:b52c6d67b596 37 * PwmOut gled(LED_GREEN);
fonz626 0:b52c6d67b596 38 * PwmOut bled(LED_BLUE);
fonz626 0:b52c6d67b596 39 *
fonz626 0:b52c6d67b596 40 * while (true) {
fonz626 0:b52c6d67b596 41 * rled = 1.0 - abs(acc.getAccX());
fonz626 0:b52c6d67b596 42 * gled = 1.0 - abs(acc.getAccY());
fonz626 0:b52c6d67b596 43 * bled = 1.0 - abs(acc.getAccZ());
fonz626 0:b52c6d67b596 44 * wait(0.1);
fonz626 0:b52c6d67b596 45 * }
fonz626 0:b52c6d67b596 46 * }
fonz626 0:b52c6d67b596 47 * @endcode
fonz626 0:b52c6d67b596 48 */
fonz626 0:b52c6d67b596 49 class MMA8451Q
fonz626 0:b52c6d67b596 50 {
fonz626 0:b52c6d67b596 51 public:
fonz626 0:b52c6d67b596 52 /**
fonz626 0:b52c6d67b596 53 * MMA8451Q constructor
fonz626 0:b52c6d67b596 54 *
fonz626 0:b52c6d67b596 55 * @param sda SDA pin
fonz626 0:b52c6d67b596 56 * @param sdl SCL pin
fonz626 0:b52c6d67b596 57 * @param addr addr of the I2C peripheral
fonz626 0:b52c6d67b596 58 */
fonz626 0:b52c6d67b596 59 MMA8451Q(PinName sda, PinName scl, int addr);
fonz626 0:b52c6d67b596 60
fonz626 0:b52c6d67b596 61 /**
fonz626 0:b52c6d67b596 62 * MMA8451Q destructor
fonz626 0:b52c6d67b596 63 */
fonz626 0:b52c6d67b596 64 ~MMA8451Q();
fonz626 0:b52c6d67b596 65
fonz626 0:b52c6d67b596 66 /**
fonz626 0:b52c6d67b596 67 * Get the value of the WHO_AM_I register
fonz626 0:b52c6d67b596 68 *
fonz626 0:b52c6d67b596 69 * @returns WHO_AM_I value
fonz626 0:b52c6d67b596 70 */
fonz626 0:b52c6d67b596 71 uint8_t getWhoAmI();
fonz626 0:b52c6d67b596 72
fonz626 0:b52c6d67b596 73 /**
fonz626 0:b52c6d67b596 74 * Get X axis acceleration
fonz626 0:b52c6d67b596 75 *
fonz626 0:b52c6d67b596 76 * @returns X axis acceleration
fonz626 0:b52c6d67b596 77 */
fonz626 0:b52c6d67b596 78 float getAccX();
fonz626 0:b52c6d67b596 79
fonz626 0:b52c6d67b596 80 /**
fonz626 0:b52c6d67b596 81 * Get Y axis acceleration
fonz626 0:b52c6d67b596 82 *
fonz626 0:b52c6d67b596 83 * @returns Y axis acceleration
fonz626 0:b52c6d67b596 84 */
fonz626 0:b52c6d67b596 85 float getAccY();
fonz626 0:b52c6d67b596 86
fonz626 0:b52c6d67b596 87 /**
fonz626 0:b52c6d67b596 88 * Get Z axis acceleration
fonz626 0:b52c6d67b596 89 *
fonz626 0:b52c6d67b596 90 * @returns Z axis acceleration
fonz626 0:b52c6d67b596 91 */
fonz626 0:b52c6d67b596 92 float getAccZ();
fonz626 0:b52c6d67b596 93
fonz626 0:b52c6d67b596 94 /**
fonz626 0:b52c6d67b596 95 * Get XYZ axis acceleration
fonz626 0:b52c6d67b596 96 *
fonz626 0:b52c6d67b596 97 * @param res array where acceleration data will be stored
fonz626 0:b52c6d67b596 98 */
fonz626 0:b52c6d67b596 99 void getAccAllAxis(float * res);
fonz626 0:b52c6d67b596 100
fonz626 0:b52c6d67b596 101 private:
fonz626 0:b52c6d67b596 102 I2C m_i2c;
fonz626 0:b52c6d67b596 103 int m_addr;
fonz626 0:b52c6d67b596 104 void readRegs(int addr, uint8_t * data, int len);
fonz626 0:b52c6d67b596 105 void writeRegs(uint8_t * data, int len);
fonz626 0:b52c6d67b596 106 int16_t getAccAxis(uint8_t addr);
fonz626 0:b52c6d67b596 107
fonz626 0:b52c6d67b596 108 };
fonz626 0:b52c6d67b596 109
fonz626 0:b52c6d67b596 110 #endif