totally useless

Dependents:   Final_Homework1 ee202Homework1 EE202A_Homework1

Fork of MAG3110 by Andrew Lindsay

Files at this revision

API Documentation at this revision

Comitter:
bmdlh
Date:
Sat Feb 08 22:18:07 2014 +0000
Parent:
4:cf40601402b7
Commit message:
useless

Changed in this revision

MAG3110.cpp Show annotated file Show diff for this revision Revisions of this file
MAG3110.h Show annotated file Show diff for this revision Revisions of this file
diff -r cf40601402b7 -r 34cf17e4dbf5 MAG3110.cpp
--- a/MAG3110.cpp	Fri May 24 20:16:24 2013 +0000
+++ b/MAG3110.cpp	Sat Feb 08 22:18:07 2014 +0000
@@ -5,20 +5,29 @@
 /******************************************************************************
  * Constructors
  ******************************************************************************/
+MAG3110::MAG3110(PinName sda, PinName scl,float dateRate, int overSample): _i2c(sda, scl), 
+    _i2c_address(0x0E<<1), _pc(NULL), _debug(false)
+{   
+    Setdr(dateRate);
+    Setosr(overSample); 
+    begin();
+}
+
 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), 
-    _i2c_address(0x1D), _pc(NULL), _debug(false)
+    _i2c_address(0x0E<<1), _pc(NULL), _debug(false),dr(MAG_3110_SAMPLE80),osr(MAG_3110_OVERSAMPLE2)
 {
     begin();
 }
 
 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl), 
-   _i2c_address(0x1D), _pc(pc), _debug(true)
+   _i2c_address(0x0E<<1), _pc(pc), _debug(true),dr(MAG_3110_SAMPLE80),osr(MAG_3110_OVERSAMPLE2)
 {
     begin();
 }
 
 void MAG3110::begin()
 {
+    
     char cmd[2];
 
     cmd[0] = MAG_CTRL_REG2;
@@ -26,7 +35,8 @@
     _i2c.write(_i2c_address, cmd, 2);
 
     cmd[0] = MAG_CTRL_REG1;
-    cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
+//    cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
+    cmd[1] = dr+osr+MAG_3110_ACTIVE;
     _i2c.write(_i2c_address, cmd, 2);
     
     // No adjustment initially
@@ -85,7 +95,46 @@
     _avgY=(maxY+minY)/2;
 }
 
-
+void MAG3110::Setdr(float dateRate)
+{
+        if (dateRate==80)
+            dr=MAG_3110_SAMPLE80;
+        else if(dateRate==40)
+            dr=MAG_3110_SAMPLE40;
+        else if(dateRate==20)
+            dr=MAG_3110_SAMPLE20;
+        else if(dateRate==10)
+            dr=MAG_3110_SAMPLE10;
+        else if(dateRate==5)
+            dr=MAG_3110_SAMPLE5;
+        else if(dateRate==2.5)
+            dr=MAG_3110_SAMPLE2_5;
+        else if(dateRate==1.25)
+            dr=MAG_3110_SAMPLE1_25;
+        else if(dateRate==0.625)
+            dr=MAG_3110_SAMPLE0_625;
+        else 
+            dr=MAG_3110_SAMPLE80;
+}
 
+void MAG3110::Setosr(int overSample)
+{
+       switch(overSample)
+    {
+        case 16:osr=MAG_3110_OVERSAMPLE1;break;
+        case 32:osr=MAG_3110_OVERSAMPLE2;break;
+        case 64:osr=MAG_3110_OVERSAMPLE3;break;
+        case 128:osr=MAG_3110_OVERSAMPLE4;break;
+        default:osr=MAG_3110_OVERSAMPLE2;break;
+    }   
+}
 
-
+void MAG3110::Overwrite_dr_osr(float dateRate,int overSample)
+{
+    char cmd[2];
+    Setdr(dateRate);
+    Setosr(overSample);
+    cmd[0] = MAG_CTRL_REG1;
+    cmd[1] = dr+osr+MAG_3110_ACTIVE;
+    _i2c.write(_i2c_address, cmd, 2);
+}
diff -r cf40601402b7 -r 34cf17e4dbf5 MAG3110.h
--- a/MAG3110.h	Fri May 24 20:16:24 2013 +0000
+++ b/MAG3110.h	Sat Feb 08 22:18:07 2014 +0000
@@ -50,10 +50,10 @@
 #define MAG_3110_SAMPLE0_625 0xE0
 
 // How many samples to average (lowers data rate)
-#define MAG_3110_OVERSAMPLE1 0
-#define MAG_3110_OVERSAMPLE2 0x08
-#define MAG_3110_OVERSAMPLE3 0x10
-#define MAG_3110_OVERSAMPLE4 0x18
+#define MAG_3110_OVERSAMPLE1 0         //16
+#define MAG_3110_OVERSAMPLE2 0x08      //32, sample rate decrease to half
+#define MAG_3110_OVERSAMPLE3 0x10      //64, sample rate decrease to 1/4
+#define MAG_3110_OVERSAMPLE4 0x18      //128 sample rate decrease to 1/8
 
 // read only 1 byte per axis
 #define MAG_3110_FASTREAD 0x04
@@ -99,6 +99,11 @@
      * Setup the Magnetometer
      *
      */
+    MAG3110(PinName sda, PinName scl,float dateRate, int overSample);
+    void Overwrite_dr_osr(float dateRate,int overSample);
+    void Setdr(float dateRate);
+    void Setosr(int overSample);
+    
     void begin();
     /**
      * Read a register, return its value as int
@@ -139,6 +144,8 @@
     Serial *_pc;
     bool _debug;
     int _avgX, _avgY;
+    char dr;       //date rate
+    char osr;      //over sample rate
 
 };
 #endif