Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1993 - 1
/*
2
HMC58X3.cpp - Interface a Honeywell HMC58X3 magnetometer to an Arduino via i2c
3
Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it>
4
 
5
Based on:
6
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274748346
7
 Modification/extension of the following by E.J.Muller
8
http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino
9
 Copyright (c) 2009 Nirav Patel,
10
 
11
The above were based on:
12
http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC58X3-v11.c
13
http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
14
 
15
 
16
This program is free software: you can redistribute it and/or modify
17
it under the terms of the version 3 GNU General Public License as
18
published by the Free Software Foundation.
19
 
20
This program is distributed in the hope that it will be useful,
21
but WITHOUT ANY WARRANTY; without even the implied warranty of
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
GNU General Public License for more details.
24
 
25
You should have received a copy of the GNU General Public License
26
along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 
28
*/
29
 
30
//uncomment the following line if you are using this library with the HMC5843
31
//#define ISHMC5843
32
 
33
#include "WProgram.h"
34
#include <Wire.h>
35
 
36
#ifndef HMC58X3_h
37
#define HMC58X3_h
38
 
39
#define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library
40
#define HMC_POS_BIAS 1
41
#define HMC_NEG_BIAS 2
42
 
43
// HMC58X3 register map. For details see HMC58X3 datasheet
44
#define HMC58X3_R_CONFA 0
45
#define HMC58X3_R_CONFB 1
46
#define HMC58X3_R_MODE 2
47
#define HMC58X3_R_XM 3
48
#define HMC58X3_R_XL 4
49
 
50
#ifdef ISHMC5843
51
  #define HMC58X3_R_YM 5
52
  #define HMC58X3_R_YL 6
53
  #define HMC58X3_R_ZM 7
54
  #define HMC58X3_R_ZL 8
55
#else // HMC5883L
56
  #define HMC58X3_R_YM 7
57
  #define HMC58X3_R_YL 8
58
  #define HMC58X3_R_ZM 5
59
  #define HMC58X3_R_ZL 6
60
#endif
61
 
62
#define HMC58X3_R_STATUS 9
63
#define HMC58X3_R_IDA 10
64
#define HMC58X3_R_IDB 11
65
#define HMC58X3_R_IDC 12
66
 
67
 
68
class HMC58X3
69
{
70
  public:
71
    HMC58X3();
72
    void init(bool setmode);
73
    void init(int address, bool setmode);
74
    void getValues(int *x,int *y,int *z);
75
    void getValues(float *x,float *y,float *z);
76
    void getValues(float *xyz);
77
    void getRaw(int *x,int *y,int *z);
78
    void calibrate(unsigned char gain);
79
    void setMode(unsigned char mode);
80
    void setDOR(unsigned char DOR);
81
    void setGain(unsigned char gain);
82
  private:
83
    void writeReg(unsigned char reg, unsigned char val);
84
    float x_scale,y_scale,z_scale,x_max,y_max,z_max;
85
};
86
 
87
#endif // HMC58X3_h