NA
Fork of L3G4200D by
L3G4200D.cpp@0:6d43e8289cc5, 2012-01-20 (annotated)
- Committer:
- shimniok
- Date:
- Fri Jan 20 23:11:40 2012 +0000
- Revision:
- 0:6d43e8289cc5
- Child:
- 3:14914cd8fdf3
Initial revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:6d43e8289cc5 | 1 | /** |
shimniok | 0:6d43e8289cc5 | 2 | * Copyright (c) 2011 Pololu Corporation. For more information, see |
shimniok | 0:6d43e8289cc5 | 3 | * |
shimniok | 0:6d43e8289cc5 | 4 | * http://www.pololu.com/ |
shimniok | 0:6d43e8289cc5 | 5 | * http://forum.pololu.com/ |
shimniok | 0:6d43e8289cc5 | 6 | * |
shimniok | 0:6d43e8289cc5 | 7 | * Permission is hereby granted, free of charge, to any person |
shimniok | 0:6d43e8289cc5 | 8 | * obtaining a copy of this software and associated documentation |
shimniok | 0:6d43e8289cc5 | 9 | * files (the "Software"), to deal in the Software without |
shimniok | 0:6d43e8289cc5 | 10 | * restriction, including without limitation the rights to use, |
shimniok | 0:6d43e8289cc5 | 11 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
shimniok | 0:6d43e8289cc5 | 12 | * copies of the Software, and to permit persons to whom the |
shimniok | 0:6d43e8289cc5 | 13 | * Software is furnished to do so, subject to the following |
shimniok | 0:6d43e8289cc5 | 14 | * conditions: |
shimniok | 0:6d43e8289cc5 | 15 | * |
shimniok | 0:6d43e8289cc5 | 16 | * The above copyright notice and this permission notice shall be |
shimniok | 0:6d43e8289cc5 | 17 | * included in all copies or substantial portions of the Software. |
shimniok | 0:6d43e8289cc5 | 18 | * |
shimniok | 0:6d43e8289cc5 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
shimniok | 0:6d43e8289cc5 | 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
shimniok | 0:6d43e8289cc5 | 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
shimniok | 0:6d43e8289cc5 | 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
shimniok | 0:6d43e8289cc5 | 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
shimniok | 0:6d43e8289cc5 | 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
shimniok | 0:6d43e8289cc5 | 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
shimniok | 0:6d43e8289cc5 | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
shimniok | 0:6d43e8289cc5 | 27 | */ |
shimniok | 0:6d43e8289cc5 | 28 | |
shimniok | 0:6d43e8289cc5 | 29 | #include "mbed.h" |
shimniok | 0:6d43e8289cc5 | 30 | #include <L3G4200D.h> |
shimniok | 0:6d43e8289cc5 | 31 | #include <math.h> |
shimniok | 0:6d43e8289cc5 | 32 | |
shimniok | 0:6d43e8289cc5 | 33 | // Defines //////////////////////////////////////////////////////////////// |
shimniok | 0:6d43e8289cc5 | 34 | |
shimniok | 0:6d43e8289cc5 | 35 | // The Arduino two-wire interface uses a 7-bit number for the address, |
shimniok | 0:6d43e8289cc5 | 36 | // and sets the last bit correctly based on reads and writes |
shimniok | 0:6d43e8289cc5 | 37 | // mbed I2C libraries take the 7-bit address shifted left 1 bit |
shimniok | 0:6d43e8289cc5 | 38 | // #define GYR_ADDRESS (0xD2 >> 1) |
shimniok | 0:6d43e8289cc5 | 39 | #define GYR_ADDRESS 0xD2 |
shimniok | 0:6d43e8289cc5 | 40 | |
shimniok | 0:6d43e8289cc5 | 41 | // Public Methods ////////////////////////////////////////////////////////////// |
shimniok | 0:6d43e8289cc5 | 42 | |
shimniok | 0:6d43e8289cc5 | 43 | // Constructor |
shimniok | 0:6d43e8289cc5 | 44 | L3G4200D::L3G4200D(PinName sda, PinName scl): |
shimniok | 0:6d43e8289cc5 | 45 | _device(sda, scl) |
shimniok | 0:6d43e8289cc5 | 46 | { |
shimniok | 0:6d43e8289cc5 | 47 | _device.frequency(400000); |
shimniok | 0:6d43e8289cc5 | 48 | // Turns on the L3G4200D's gyro and places it in normal mode. |
shimniok | 0:6d43e8289cc5 | 49 | // 0x0F = 0b00001111 |
shimniok | 0:6d43e8289cc5 | 50 | // Normal power mode, all axes enabled |
shimniok | 0:6d43e8289cc5 | 51 | writeReg(L3G4200D_CTRL_REG1, 0x0F); |
shimniok | 0:6d43e8289cc5 | 52 | } |
shimniok | 0:6d43e8289cc5 | 53 | |
shimniok | 0:6d43e8289cc5 | 54 | // Writes a gyro register |
shimniok | 0:6d43e8289cc5 | 55 | void L3G4200D::writeReg(byte reg, byte value) |
shimniok | 0:6d43e8289cc5 | 56 | { |
shimniok | 0:6d43e8289cc5 | 57 | data[0] = reg; |
shimniok | 0:6d43e8289cc5 | 58 | data[1] = value; |
shimniok | 0:6d43e8289cc5 | 59 | |
shimniok | 0:6d43e8289cc5 | 60 | _device.write(GYR_ADDRESS, data, 2); |
shimniok | 0:6d43e8289cc5 | 61 | } |
shimniok | 0:6d43e8289cc5 | 62 | |
shimniok | 0:6d43e8289cc5 | 63 | // Reads a gyro register |
shimniok | 0:6d43e8289cc5 | 64 | byte L3G4200D::readReg(byte reg) |
shimniok | 0:6d43e8289cc5 | 65 | { |
shimniok | 0:6d43e8289cc5 | 66 | byte value = 0; |
shimniok | 0:6d43e8289cc5 | 67 | |
shimniok | 0:6d43e8289cc5 | 68 | _device.write(GYR_ADDRESS, ®, 1); |
shimniok | 0:6d43e8289cc5 | 69 | _device.read(GYR_ADDRESS, &value, 1); |
shimniok | 0:6d43e8289cc5 | 70 | |
shimniok | 0:6d43e8289cc5 | 71 | return value; |
shimniok | 0:6d43e8289cc5 | 72 | } |
shimniok | 0:6d43e8289cc5 | 73 | |
shimniok | 0:6d43e8289cc5 | 74 | // Reads the 3 gyro channels and stores them in vector g |
shimniok | 0:6d43e8289cc5 | 75 | void L3G4200D::read(int g[3]) |
shimniok | 0:6d43e8289cc5 | 76 | { |
shimniok | 0:6d43e8289cc5 | 77 | // assert the MSB of the address to get the gyro |
shimniok | 0:6d43e8289cc5 | 78 | // to do slave-transmit subaddress updating. |
shimniok | 0:6d43e8289cc5 | 79 | data[0] = L3G4200D_OUT_X_L | (1 << 7); |
shimniok | 0:6d43e8289cc5 | 80 | _device.write(GYR_ADDRESS, data, 1); |
shimniok | 0:6d43e8289cc5 | 81 | |
shimniok | 0:6d43e8289cc5 | 82 | // Wire.requestFrom(GYR_ADDRESS, 6); |
shimniok | 0:6d43e8289cc5 | 83 | // while (Wire.available() < 6); |
shimniok | 0:6d43e8289cc5 | 84 | |
shimniok | 0:6d43e8289cc5 | 85 | _device.read(GYR_ADDRESS, data, 6); |
shimniok | 0:6d43e8289cc5 | 86 | |
shimniok | 0:6d43e8289cc5 | 87 | uint8_t xla = data[0]; |
shimniok | 0:6d43e8289cc5 | 88 | uint8_t xha = data[1]; |
shimniok | 0:6d43e8289cc5 | 89 | uint8_t yla = data[2]; |
shimniok | 0:6d43e8289cc5 | 90 | uint8_t yha = data[3]; |
shimniok | 0:6d43e8289cc5 | 91 | uint8_t zla = data[4]; |
shimniok | 0:6d43e8289cc5 | 92 | uint8_t zha = data[5]; |
shimniok | 0:6d43e8289cc5 | 93 | |
shimniok | 0:6d43e8289cc5 | 94 | g[0] = (short) (xha << 8 | xla); |
shimniok | 0:6d43e8289cc5 | 95 | g[1] = (short) (yha << 8 | yla); |
shimniok | 0:6d43e8289cc5 | 96 | g[2] = (short) (zha << 8 | zla); |
shimniok | 0:6d43e8289cc5 | 97 | } |