Adafruit 9DOF - L3GD20H Driver
Dependents: Galileo_HoverBoardRider projetinterface MAINTEST Test_gyro
Revision 0:81b60e58e9e1, committed 2016-04-09
- Comitter:
- julioefajardo
- Date:
- Sat Apr 09 04:27:11 2016 +0000
- Commit message:
- Adafruit 9 DOF - L3GD20H Driver
Changed in this revision
L3GD20H.cpp | Show annotated file Show diff for this revision Revisions of this file |
L3GD20H.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 81b60e58e9e1 L3GD20H.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L3GD20H.cpp Sat Apr 09 04:27:11 2016 +0000 @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2011 Pololu Corporation. For more information, see + * + * http://www.pololu.com/ + * http://forum.pololu.com/ + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + + #include "mbed.h" + #include "L3GD20H.h" + + +// Defines //////////////////////////////////////////////////////////////// + +// The Arduino two-wire interface uses a 7-bit number for the address, +// and sets the last bit correctly based on reads and writes +// mbed I2C libraries take the 7-bit address shifted left 1 bit +// #define GYR_ADDRESS (0xD2 >> 1) +#define GYR_ADDRESS 0xD6 + + +// Public Methods ////////////////////////////////////////////////////////////// + +// Constructor +L3GD20H::L3GD20H(PinName sda, PinName scl): + _L3GD20H(sda, scl) +{ + char reg_v; + _L3GD20H.frequency(100000); + + // 0x0F = 0b00001111 + // Normal power mode, all axes enabled + reg_v = 0; + reg_v |= 0x0F; + write_reg(GYR_ADDRESS,L3GD20_CTRL_REG1,reg_v); + + + +} + + + +bool L3GD20H::read(short g[3]) { + char gyr[6]; + + if (recv(GYR_ADDRESS, L3GD20_OUT_X_L, gyr, 6)) { + //scale is 1 dps/digit + g[0] = (gyr[1] << 8 | gyr[0]); + g[1] = (gyr[3] << 8 | gyr[2]); + g[2] = (gyr[5] << 8 | gyr[4]); + + + return true; + } + + return false; +} + + + + +bool L3GD20H::write_reg(int addr_i2c,int addr_reg, char v) +{ + char data[2] = {addr_reg, v}; + return L3GD20H::_L3GD20H.write(addr_i2c, data, 2) == 0; +} + +bool L3GD20H::read_reg(int addr_i2c,int addr_reg, char *v) +{ + char data = addr_reg; + bool result = false; + + __disable_irq(); + if ((_L3GD20H.write(addr_i2c, &data, 1) == 0) && (_L3GD20H.read(addr_i2c, &data, 1) == 0)){ + *v = data; + result = true; + } + __enable_irq(); + return result; +} + + +bool L3GD20H::recv(char sad, char sub, char *buf, int length) { + if (length > 1) sub |= 0x80; + + return _L3GD20H.write(sad, &sub, 1, true) == 0 && _L3GD20H.read(sad, buf, length) == 0; +} \ No newline at end of file
diff -r 000000000000 -r 81b60e58e9e1 L3GD20H.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L3GD20H.h Sat Apr 09 04:27:11 2016 +0000 @@ -0,0 +1,103 @@ +/* Copyright (c) 2011 Pololu Corporation. For more information, see + * + * http://www.pololu.com/ + * http://forum.pololu.com/ + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __L3GD20H_H +#define __L3GD20H_H + +#include "mbed.h" + +// register addresses + +#define L3GD20_WHO_AM_I 0x0F + +#define L3GD20_CTRL_REG1 0x20 +#define L3GD20_CTRL_REG2 0x21 +#define L3GD20_CTRL_REG3 0x22 +#define L3GD20_CTRL_REG4 0x23 +#define L3GD20_CTRL_REG5 0x24 +#define L3GD20_REFERENCE 0x25 +#define L3GD20_OUT_TEMP 0x26 +#define L3GD20_STATUS_REG 0x27 + +#define L3GD20_OUT_X_L 0x28 +#define L3GD20_OUT_X_H 0x29 +#define L3GD20_OUT_Y_L 0x2A +#define L3GD20_OUT_Y_H 0x2B +#define L3GD20_OUT_Z_L 0x2C +#define L3GD20_OUT_Z_H 0x2D + +#define L3GD20_FIFO_CTRL_REG 0x2E +#define L3GD20_FIFO_SRC_REG 0x2F + +#define L3GD20_INT1_CFG 0x30 +#define L3GD20_INT1_SRC 0x31 +#define L3GD20_INT1_THS_XH 0x32 +#define L3GD20_INT1_THS_XL 0x33 +#define L3GD20_INT1_THS_YH 0x34 +#define L3GD20_INT1_THS_YL 0x35 +#define L3GD20_INT1_THS_ZH 0x36 +#define L3GD20_INT1_THS_ZL 0x37 +#define L3GD20_INT1_DURATION 0x38 + +/** Interface library for the ST L3GD20 3-axis gyro + * + * Ported from Pololu L3GD20 library for Arduino by + * + * @code + * #include "mbed.h" + * #include "L3GD20.h" + * L3GD20 gyro(p28, p27); + * ... + * int g[3]; + * gyro.read(g); + * @endcode + */ +class L3GD20H +{ + public: + /** Create a new L3GD20 I2C interface + * @param sda is the pin for the I2C SDA line + * @param scl is the pin for the I2C SCL line + */ + L3GD20H(PinName sda, PinName scl); + + /** Read gyro values + * @param g Array containing x, y, and z gyro values + * @return g Array containing x, y, and z gyro values + */ + bool read(short g[3]); + + private: + I2C _L3GD20H; + short gx, gy, gz; + + bool write_reg(int addr_i2c,int addr_reg, char v); + bool read_reg(int addr_i2c,int addr_reg, char *v); + bool recv(char sad, char sub, char *buf, int length); +}; + +#endif \ No newline at end of file