library for using LSM303DM chip

Revision:
2:1052b1b97cc2
Parent:
0:4d358fbeab6e
Child:
3:b2cc1d06e2f5
--- a/LSM303.h	Fri Oct 11 12:51:54 2013 +0000
+++ b/LSM303.h	Sun Oct 13 02:38:38 2013 +0000
@@ -1,63 +1,59 @@
 #ifndef LSM303_h
 #define LSM303_h
 #include "mbed.h"
-/* LSM303DLM Example Code base on LSM303DLH example code by Jim Lindblom SparkFun Electronics
-
-   date: 9/6/11
-   license: Creative commons share-alike v3.0
+/* LSM303DLM  mbed code based on AN3192 Application note and LSM303DLH example code by Jim Lindblom SparkFun Electronics 
+  modified by Frankie.Chu to arduino in year 2012.
 
-   Modified by:Frankie.Chu
-
-   Summary:
-   Show how to calculate level and tilt-compensated heading using
-   the snazzy LSM303DLH 3-axis magnetometer/3-axis accelerometer.
+   date: 13/10/13
+   license: Use this with your own risk:-)
 
-   Firmware:
-   You can set the accelerometer's full-scale range by setting
-   the SCALE constant to either 2, 4, or 8. This value is used
-   in the initLSM303() function. For the most part, all other
-   registers in the LSM303 will be at their default value.
+   Calibration is a must to make your compass to work:
+   
+   //displays integer value with sign in 4 digit led display and waits delay seconds
+   void displayInt(TM1637 &tm, int value, float delay)...
+   // displays a digit in position of 4 digit led display
+   void TM1637::display(uint8_t position,int8_t digit)...
+   //calibration loop
+   for(int i = 0; i <200; i++) {
 
-   Use the LSM303_write() and LSM303_read() functions to write
-   to and read from the LSM303's internal registers.
+        lsm.getLSM303_mag();
 
-   Use getLSM303_accel() and getLSM303_mag() to get the acceleration
-   and magneto values from the LSM303. You'll need to pass each of
-   those functions an array, where the data will be stored upon
-   return from the void.
+// Mmin handler
+        if(lsm.m.x  <  lsm.m_min.x)
+            lsm.m_min.x  =  lsm.m.x;
+
+        if(lsm.m.y  <  lsm.m_min.y)
+            lsm.m_min.y  =  lsm.m.y;
 
-   getHeading() calculates a heading assuming the sensor is level.
-   A float between 0 and 360 is returned. You need to pass it a
-   array with magneto values.
+        if(lsm.m.z  <  lsm.m_min.z)
+            lsm.m_min.z  =  lsm.m.z;
 
-   getTiltHeading() calculates a tilt-compensated heading.
-   A float between 0 and 360 degrees is returned. You need
-   to pass this function both a magneto and acceleration array.
+// Mmax handler
+        if(lsm.m.x  >  lsm.m_max.x)
+            lsm.m_max.x  =  lsm.m.x ;
 
-   Headings are calculated as specified in AN3192:
-   http://www.sparkfun.com/datasheets/Sensors/Magneto/Tilt%20Compensated%20Compass.pdf
+        if(lsm.m.y  >  lsm.m_max.y)
+            lsm.m_max.y =  lsm.m.y;
 
-   Hardware:
-   I'm using SparkFun's LSM303 breakout. Only power and the two
-   I2C lines are connected:
-   LSM303 Breakout ---------- Arduino
-         Vin                   5V
-         GND                   GND
-         SDA                   A4
-         SCL                   A5
+        if(lsm.m.z  >  lsm.m_max.z)
+            lsm.m_max.z  =  lsm.m.z;
+
+        tm.clearDisplay();
+        tm.display(i%4,0);
+        wait(0.1);
+    }
+    displayInt(tm, lsm.m_min.x, 8);
+    displayInt(tm, lsm.m_min.y, 8);
+    displayInt(tm, lsm.m_min.z, 8);
+    displayInt(tm, lsm.m_max.x, 8);
+    displayInt(tm, lsm.m_max.y, 8);
+    displayInt(tm, lsm.m_max.z, 8);
 */
 
-
-
-#define ACCELE_SCALE 2  // accelerometer full-scale, should be 2, 4, or 8
-
 /* LSM303 Address definitions */
 #define LSM303_MAG  0x3C  // assuming SA0 grounded
 #define LSM303_ACC  0x30  // assuming SA0 grounded
 
-#define X 0
-#define Y 1
-#define Z 2
 
 /* LSM303 Register definitions */
 #define CTRL_REG1_A 0x20
@@ -136,8 +132,6 @@
 private:
  
     int _i2c_address;
-
-    int initLSM303(int fs); // accelerometer full-scale, should be 2, 4, or 8
 };
 
 #endif