I2Cdevlib

I2C device library collection for AVR/Arduino or other C++-based MCUs

HMC5883L/HMC5883L.h
00001 // I2Cdev library collection - HMC5883L I2C device class header file
00002 // Based on Honeywell HMC5883L datasheet, 10/2010 (Form #900405 Rev B)
00003 // 8/22/2011 by Jeff Rowberg <jeff@rowberg.net>
00004 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
00005 //
00006 // Changelog:
00007 //     2011-08-22 - small Doxygen comment fixes
00008 //     2011-07-31 - initial release
00009 
00010 /* ============================================
00011 I2Cdev device library code is placed under the MIT license
00012 Copyright (c) 2011 Jeff Rowberg
00013 
00014 Permission is hereby granted, free of charge, to any person obtaining a copy
00015 of this software and associated documentation files (the "Software"), to deal
00016 in the Software without restriction, including without limitation the rights
00017 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00018 copies of the Software, and to permit persons to whom the Software is
00019 furnished to do so, subject to the following conditions:
00020 
00021 The above copyright notice and this permission notice shall be included in
00022 all copies or substantial portions of the Software.
00023 
00024 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00025 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00026 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00027 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00028 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00029 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00030 THE SOFTWARE.
00031 ===============================================
00032 */
00033 
00034 #ifndef _HMC5883L_H_
00035 #define _HMC5883L_H_
00036 
00037 #include "I2Cdev.h"
00038 
00039 #define HMC5883L_ADDRESS            0x1E // this device only has one address
00040 #define HMC5883L_DEFAULT_ADDRESS    0x1E
00041 
00042 #define HMC5883L_RA_CONFIG_A        0x00
00043 #define HMC5883L_RA_CONFIG_B        0x01
00044 #define HMC5883L_RA_MODE            0x02
00045 #define HMC5883L_RA_DATAX_H         0x03
00046 #define HMC5883L_RA_DATAX_L         0x04
00047 #define HMC5883L_RA_DATAY_H         0x05
00048 #define HMC5883L_RA_DATAY_L         0x06
00049 #define HMC5883L_RA_DATAZ_H         0x07
00050 #define HMC5883L_RA_DATAZ_L         0x08
00051 #define HMC5883L_RA_STATUS          0x09
00052 #define HMC5883L_RA_ID_A            0x0A
00053 #define HMC5883L_RA_ID_B            0x0B
00054 #define HMC5883L_RA_ID_C            0x0C
00055 
00056 #define HMC5883L_CRA_AVERAGE_BIT    6
00057 #define HMC5883L_CRA_AVERAGE_LENGTH 2
00058 #define HMC5883L_CRA_RATE_BIT       4
00059 #define HMC5883L_CRA_RATE_LENGTH    3
00060 #define HMC5883L_CRA_BIAS_BIT       1
00061 #define HMC5883L_CRA_BIAS_LENGTH    2
00062 
00063 #define HMC5883L_AVERAGING_1        0x00
00064 #define HMC5883L_AVERAGING_2        0x01
00065 #define HMC5883L_AVERAGING_4        0x02
00066 #define HMC5883L_AVERAGING_8        0x03
00067 
00068 #define HMC5883L_RATE_0P75          0x00
00069 #define HMC5883L_RATE_1P5           0x01
00070 #define HMC5883L_RATE_3             0x02
00071 #define HMC5883L_RATE_7P5           0x03
00072 #define HMC5883L_RATE_15            0x04
00073 #define HMC5883L_RATE_30            0x05
00074 #define HMC5883L_RATE_75            0x06
00075 
00076 #define HMC5883L_BIAS_NORMAL        0x00
00077 #define HMC5883L_BIAS_POSITIVE      0x01
00078 #define HMC5883L_BIAS_NEGATIVE      0x02
00079 
00080 #define HMC5883L_CRB_GAIN_BIT       7
00081 #define HMC5883L_CRB_GAIN_LENGTH    3
00082 
00083 #define HMC5883L_GAIN_1370          0x00
00084 #define HMC5883L_GAIN_1090          0x01
00085 #define HMC5883L_GAIN_820           0x02
00086 #define HMC5883L_GAIN_660           0x03
00087 #define HMC5883L_GAIN_440           0x04
00088 #define HMC5883L_GAIN_390           0x05
00089 #define HMC5883L_GAIN_330           0x06
00090 #define HMC5883L_GAIN_220           0x07
00091 
00092 #define HMC5883L_MODEREG_BIT        1
00093 #define HMC5883L_MODEREG_LENGTH     2
00094 
00095 #define HMC5883L_MODE_CONTINUOUS    0x00
00096 #define HMC5883L_MODE_SINGLE        0x01
00097 #define HMC5883L_MODE_IDLE          0x02
00098 
00099 #define HMC5883L_STATUS_LOCK_BIT    1
00100 #define HMC5883L_STATUS_READY_BIT   0
00101 
00102 class HMC5883L {
00103     public:
00104         HMC5883L();
00105         HMC5883L(uint8_t address);
00106         
00107         void initialize();
00108         bool testConnection();
00109 
00110         // CONFIG_A register
00111         uint8_t getSampleAveraging();
00112         void setSampleAveraging(uint8_t averaging);
00113         uint8_t getDataRate();
00114         void setDataRate(uint8_t rate);
00115         uint8_t getMeasurementBias();
00116         void setMeasurementBias(uint8_t bias);
00117 
00118         // CONFIG_B register
00119         uint8_t getGain();
00120         void setGain(uint8_t gain);
00121 
00122         // MODE register
00123         uint8_t getMode();
00124         void setMode(uint8_t mode);
00125 
00126         // DATA* registers
00127         void getHeading(int16_t *x, int16_t *y, int16_t *z);
00128         int16_t getHeadingX();
00129         int16_t getHeadingY();
00130         int16_t getHeadingZ();
00131 
00132         // STATUS register
00133         bool getLockStatus();
00134         bool getReadyStatus();
00135 
00136         // ID_* registers
00137         uint8_t getIDA();
00138         uint8_t getIDB();
00139         uint8_t getIDC();
00140 
00141     private:
00142         uint8_t devAddr;
00143         uint8_t buffer[6];
00144         uint8_t mode;
00145 };
00146 
00147 #endif /* _HMC5883L_H_ */
 All Data Structures Functions Variables