library for using LSM303DM chip

Revision:
2:1052b1b97cc2
Parent:
1:322c80f884d3
Child:
3:b2cc1d06e2f5
--- a/LSM303.cpp	Fri Oct 11 12:51:54 2013 +0000
+++ b/LSM303.cpp	Sun Oct 13 02:38:38 2013 +0000
@@ -2,18 +2,20 @@
 #include <math.h>
 #include "LSM303.h"
 
+#define ACCELE_SCALE 2  // accelerometer full-scale, should be 2
+
 I2C i2c(P0_5, P0_4);
 
 int LSM303::setup()
 {
-#if 1
-    m_max.x = 242;
-    m_max.y = 242;
-    m_max.z = 158;
-    m_min.x = -498;
-    m_min.y = -498;
-    m_min.z = -538;
-#else
+#if 1 //set to 0 when calibrating
+    m_max.x = 462;
+    m_max.y = 416;
+    m_max.z = 439;
+    m_min.x = -645;
+    m_min.y = -647;
+    m_min.z = -589;
+#else 
     m_max.x = 1;
     m_max.y = 1;
     m_max.z = 1;
@@ -21,7 +23,11 @@
     m_min.y = 0;
     m_min.z = 0;
 #endif
-    return initLSM303(ACCELE_SCALE);  // Initialize the LSM303, using a SCALE full-scale range
+    LSM303_write(0x00, CTRL_REG4_A);
+    LSM303_write(MAG_SCALE_1_3, CRB_REG_M); //magnetic scale = +/-1.3Gauss
+    LSM303_write(0x00, MR_REG_M);  // 0x00 = continouous conversion mode
+
+    return 1; //success
 }
 int LSM303::testAcc()
 {
@@ -41,22 +47,6 @@
     return 255;
 }
 
-int LSM303::initLSM303(int fs)
-{
-    if (LSM303_write(0x27, CTRL_REG1_A) == 1) {  // 0x27 = normal power mode, all accel axes on
-        //LSM303_write(16, CTRL_REG2_A); // enable internal hp filter
-    }
-    if ((fs==8)||(fs==4))
-        LSM303_write((0x00 | (fs-fs/2-1)<<4), CTRL_REG4_A);  // set full-scale
-    else
-        LSM303_write(0x00, CTRL_REG4_A);
-    //LSM303_write(20, CRA_REG_M);  // 20 = mag 30Hz output rate
-    LSM303_write(MAG_SCALE_1_3, CRB_REG_M); //magnetic scale = +/-1.3Gauss
-    LSM303_write(0x00, MR_REG_M);  // 0x00 = continouous conversion mode
-
-    return 1;
-}
-
 float LSM303::getTiltHeading()
 {
     //shift and scale