Advertisement
Guest User

HMC58X3.h

a guest
Sep 29th, 2011
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. /*!
  2.    \file HMC58X3.h - Interface a Honeywell HMC58X3 magnetometer to an Arduino via i2c.
  3.  
  4.     \author Fabio Varesano** <fvaresano@yahoo.it>**
  5.     Copyright (C) 2011 Fabio Varesano** <fvaresano@yahoo.it>**
  6.  
  7.     Based on:
  8.     http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274748346
  9.      Modification/extension of the following by E.J.Muller
  10.     http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino
  11.      Copyright (c) 2009 Nirav Patel,
  12.    
  13.     The above were based on:
  14.     http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC58X3-v11.c
  15.     http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
  16.    
  17.     This program is free software: you can redistribute it and/or modify
  18.     it under the terms of the version 3 GNU General Public License as
  19.     published by the Free Software Foundation.
  20.    
  21.     This program is distributed in the hope that it will be useful,
  22.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.     GNU General Public License for more details.
  25.    
  26.     You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
  27. */
  28.  
  29. //#define ISHMC5843 (1) // Uncomment this following line if you are using this library with the HMC5843.
  30.  
  31. #include "WProgram.h"
  32. #include <Wire.h>
  33.  
  34. #ifndef HMC58X3_h
  35. #define HMC58X3_h
  36.  
  37. #define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library
  38. #define HMC_POS_BIAS 1
  39. #define HMC_NEG_BIAS 2
  40.  
  41. // HMC58X3 register map. For details see HMC58X3 datasheet
  42. #define HMC58X3_R_CONFA 0
  43. #define HMC58X3_R_CONFB 1
  44. #define HMC58X3_R_MODE 2
  45. #define HMC58X3_R_XM 3
  46. #define HMC58X3_R_XL 4
  47.  
  48. #ifdef ISHMC5843
  49.     #define HMC58X3_R_YM (5)    //!< Register address for YM.
  50.     #define HMC58X3_R_YL (6)    //!< Register address for YL.
  51.     #define HMC58X3_R_ZM (7)    //!< Register address for ZM.
  52.     #define HMC58X3_R_ZL (8)    //!< Register address for ZL.
  53.  
  54.     #define HMC58X3_X_SELF_TEST_GAUSS (+0.55)                       //!< X axis level when bias current is applied.
  55.     #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
  56.     #define HMC58X3_Z_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
  57.  
  58.     /*
  59.         This is my best guess at the LOW, HIGH limit.  The data sheet does not have these values.
  60.     */
  61.     #define SELF_TEST_LOW_LIMIT  (HMC58X3_X_SELF_TEST_GAUSS*0.53)   //!< Low limit 53% of expected value.
  62.     #define SELF_TEST_HIGH_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*1.36)   //!< High limit 136% of expected values.
  63. #else // HMC5883L
  64.     #define HMC58X3_R_YM (7)  //!< Register address for YM.
  65.     #define HMC58X3_R_YL (8)  //!< Register address for YL.
  66.     #define HMC58X3_R_ZM (5)  //!< Register address for ZM.
  67.     #define HMC58X3_R_ZL (6)  //!< Register address for ZL.
  68.  
  69.     #define HMC58X3_X_SELF_TEST_GAUSS (+1.16)                       //!< X axis level when bias current is applied.
  70.     #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
  71.     #define HMC58X3_Z_SELF_TEST_GAUSS (+1.08)                       //!< Y axis level when bias current is applied.
  72.  
  73.     #define SELF_TEST_LOW_LIMIT  (243.0/390.0)   //!< Low limit when gain is 5.
  74.     #define SELF_TEST_HIGH_LIMIT (575.0/390.0)   //!< High limit when gain is 5.
  75. #endif
  76.  
  77. #define HMC58X3_R_STATUS 9
  78. #define HMC58X3_R_IDA 10
  79. #define HMC58X3_R_IDB 11
  80. #define HMC58X3_R_IDC 12
  81.  
  82. class HMC58X3
  83. {
  84.   public:
  85.     HMC58X3();
  86.     void init(bool setmode);
  87.     void init(int address, bool setmode);
  88.     void getValues(int *x,int *y,int *z);
  89.     void getValues(float *x,float *y,float *z);
  90.     void getValues(float *xyz);
  91.     void getRaw(int *x,int *y,int *z);
  92.     void getRaw(int *xyz);
  93.     void calibrate(unsigned char gain);     // Original calibrate with a few weaknesses.
  94.     bool calibrate(unsigned char gain,unsigned int n_samples);
  95.     void setMode(unsigned char mode);
  96.     void setDOR(unsigned char DOR);
  97.     void setGain(unsigned char gain);
  98.     void getID(char id[3]);
  99.   private:
  100.     void writeReg(unsigned char reg, unsigned char val);
  101.     float x_scale,y_scale,z_scale,x_max,y_max,z_max;
  102. };
  103.  
  104. #endif // HMC58X3_h
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement