Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adds initial library and example.

  • Loading branch information...
commit 6530f210c164337f8d7547d0673f33dc8eb5032f 0 parents
@ryantm authored
25 LICENSE.txt
@@ -0,0 +1,25 @@
+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.
75 LSM303DLH.cpp
@@ -0,0 +1,75 @@
+#include <LSM303DLH.h>
+#include <Wire.h>
+
+// Constructors ////////////////////////////////////////////////////////////////
+
+LSM303DLH::LSM303DLH()
+{
+}
+
+// Public Methods //////////////////////////////////////////////////////////////
+
+// Turns on the LSM303DLH's accelerometer and magnetometers and places them in normal
+// mode.
+void LSM303DLH::enable(void)
+{
+ //Enable Accelerometer
+ Wire.beginTransmission(ACC_ADDRESS);
+ Wire.send(CTRL_REG1_A);
+ //0x27 = 0b00100111
+ // Normal power mode, all axes enabled
+ Wire.send(0x27);
+ Wire.endTransmission();
+
+ //Enable Magnetometer
+ Wire.beginTransmission(MAG_ADDRESS);
+ Wire.send(MR_REG_M);
+ //0x00 = 0b00000000
+ // Continuous conversion mode
+ Wire.send(0x00);
+ Wire.endTransmission();
+}
+
+// Reads all 6 channels of the LSM303DLH and stores them in the object variables
+void LSM303DLH::read()
+{
+ //read accelerometer
+ Wire.beginTransmission(ACC_ADDRESS);
+ // assert the MSB of the address to get the accelerometer
+ // to do slave-transmit subaddress updating.
+ Wire.send(OUT_X_L_A | (1 << 7));
+ Wire.endTransmission();
+ Wire.requestFrom(ACC_ADDRESS,6);
+
+ while (Wire.available() < 6);
+
+ uint8_t xla = Wire.receive();
+ uint8_t xha = Wire.receive();
+ uint8_t yla = Wire.receive();
+ uint8_t yha = Wire.receive();
+ uint8_t zla = Wire.receive();
+ uint8_t zha = Wire.receive();
+
+ accelerometerX = (xha << 8 | xla) >> 4;
+ accelerometerY = (yha << 8 | yla) >> 4;
+ accelerometerZ = (zha << 8 | zla) >> 4;
+
+ //read magnetometer
+ Wire.beginTransmission(MAG_ADDRESS);
+ Wire.send(OUT_X_H_M);
+ Wire.endTransmission();
+ Wire.requestFrom(MAG_ADDRESS,6);
+
+ while (Wire.available() < 6);
+
+ uint8_t xhm = Wire.receive();
+ uint8_t xlm = Wire.receive();
+ uint8_t yhm = Wire.receive();
+ uint8_t ylm = Wire.receive();
+ uint8_t zhm = Wire.receive();
+ uint8_t zlm = Wire.receive();
+
+ magnetometerX = (xhm << 8 | xlm);
+ magnetometerY = (yhm << 8 | ylm);
+ magnetometerZ = (zhm << 8 | zlm);
+}
70 LSM303DLH.h
@@ -0,0 +1,70 @@
+#ifndef LSM303DLH_h
+#define LSM303DLH_h
+
+#include <Wire.h>
+
+// The Arduino two-wire interface uses a 7-bit number for the address,
+// and sets the last bit correctly based on reads and writes
+#define ACC_ADDRESS (0x30 >> 1)
+#define MAG_ADDRESS (0x3C >> 1)
+
+#define CTRL_REG1_A (0x20)
+#define CTRL_REG2_A (0x21)
+#define CTRL_REG3_A (0x22)
+#define CTRL_REG4_A (0x23)
+#define CTRL_REG5_A (0x24)
+#define HP_FILTER_RESET_A (0x25)
+#define REFERENCE_A (0x26)
+#define STATUS_REG_A (0x27)
+
+#define OUT_X_L_A (0x28)
+#define OUT_X_H_A (0x29)
+#define OUT_Y_L_A (0x2A)
+#define OUT_Y_H_A (0x2B)
+#define OUT_Z_L_A (0x2C)
+#define OUT_Z_H_A (0x2D)
+
+#define INT1_CFG_A (0x30)
+#define INT1_SOURCE_A (0x31)
+#define INT1_THS_A (0x32)
+#define INT1_DURATION_A (0x33)
+#define INT2_CFG_A (0x34)
+#define INT2_SOURCE_A (0x35)
+#define INT2_THS_A (0x36)
+#define INT2_DURATION_A (0x37)
+
+#define CRA_REG_M (0x00)
+#define CRB_REG_M (0x01)
+#define MR_REG_M (0x02)
+
+#define OUT_X_H_M (0x03)
+#define OUT_X_L_M (0x04)
+#define OUT_Y_H_M (0x05)
+#define OUT_Y_L_M (0x06)
+#define OUT_Z_H_M (0x07)
+#define OUT_Z_L_M (0x08)
+
+#define SR_REG_M (0x09)
+#define IRA_REG_M (0x0A)
+#define IRB_REG_M (0x0B)
+#define IRC_REG_M (0x0C)
+
+class LSM303DLH
+{
+ public:
+ signed int accelerometerX;
+ signed int accelerometerY;
+ signed int accelerometerZ;
+ signed int magnetometerX;
+ signed int magnetometerY;
+ signed int magnetometerZ;
+ //TwoWire wire;
+ LSM303DLH();
+ void enable(void);
+ void read(void);
+};
+
+#endif
+
+
+
0  README.txt
No changes.
30 examples/serial/serial.pde
@@ -0,0 +1,30 @@
+#include <Wire.h>
+#include <LSM303DLH.h>
+
+LSM303DLH compass;
+
+void setup() {
+ Serial.begin(9600);
+ Wire.begin();
+ compass.enable();
+}
+
+void loop() {
+ compass.read();
+
+ Serial.print("A ");
+ Serial.print("X: ");
+ Serial.print(compass.accelerometerX);
+ Serial.print(" Y: ");
+ Serial.print(compass.accelerometerY);
+ Serial.print(" Z: ");
+ Serial.print(compass.accelerometerZ);
+
+ Serial.print(" M ");
+ Serial.print("X: ");
+ Serial.print(compass.magnetometerX);
+ Serial.print(" Y: ");
+ Serial.print(compass.magnetometerY);
+ Serial.print(" Z: ");
+ Serial.println(compass.magnetometerZ);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.