HMC5843 Library as part of the 9DOF stick from Sparkfun.com

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC5843.h Source File

HMC5843.h

00001 /**
00002  * @author Uwe Gartmann
00003  * @author Used HMC5843 library developed by Jose R. Padron and Aaron Berk as template
00004  *
00005  * @section LICENSE
00006  *
00007  * Copyright (c) 2010 ARM Limited
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  *
00027  * @section DESCRIPTION
00028  *
00029  * Honeywell HMC5843 digital compass.
00030  *
00031  * Datasheet:
00032  *
00033  * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf
00034  */
00035 
00036 #ifndef HMC5843_H
00037 #define HMC5843_H
00038 
00039 /**
00040  * Includes
00041  */
00042 #include "mbed.h"
00043 
00044 /**
00045  * Defines
00046  */
00047 #define HMC5843_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read.
00048 #define HMC5843_I2C_WRITE   0x3C 
00049 #define HMC5843_I2C_READ    0x3D 
00050 
00051 //Values Config A
00052 #define HMC5843_0_5HZ_NORMAL         0x00
00053 #define HMC5843_0_5HZ_POSITIVE       0x01
00054 #define HMC5843_0_5HZ_NEGATIVE       0x02
00055 
00056 #define HMC5843_1HZ_NORMAL           0x04
00057 #define HMC5843_1HZ_POSITIVE         0x05
00058 #define HMC5843_1HZ_NEGATIVE         0x06
00059 
00060 #define HMC5843_2HZ_NORMAL           0x08
00061 #define HMC5843_2HZ_POSITIVE         0x09
00062 #define HMC5843_2HZ_NEGATIVE         0x0A
00063 
00064 #define HMC5843_5HZ_NORMAL           0x0C
00065 #define HMC5843_5HZ_POSITIVE         0x0D
00066 #define HMC5843_5HZ_NEGATIVE         0x0E
00067 
00068 #define HMC5843_10HZ_NORMAL           0x10
00069 #define HMC5843_10HZ_POSITIVE         0x11
00070 #define HMC5843_10HZ_NEGATIVE         0x12
00071 
00072 #define HMC5843_20HZ_NORMAL           0x14
00073 #define HMC5843_20HZ_POSITIVE         0x15
00074 #define HMC5843_20HZ_NEGATIVE         0x16
00075 
00076 #define HMC5843_50HZ_NORMAL           0x18
00077 #define HMC5843_50HZ_POSITIVE         0x19
00078 #define HMC5843_50HZ_NEGATIVE         0x1A
00079 
00080 //Values Config B
00081 #define HMC5843_0_7GA         0x00
00082 #define HMC5843_1_0GA         0x20
00083 #define HMC5843_1_5GA         0x40
00084 #define HMC5843_2_0GA         0x60
00085 #define HMC5843_3_2GA         0x80
00086 #define HMC5843_3_8GA         0xA0
00087 #define HMC5843_4_5GA         0xC0
00088 #define HMC5843_6_5GA         0xE0
00089 
00090 //Values MODE
00091 #define HMC5843_CONTINUOUS   0x00
00092 #define HMC5843_SINGLE         0x01
00093 #define HMC5843_IDLE         0x02
00094 #define HMC5843_SLEEP         0x03
00095 
00096 
00097 
00098 #define HMC5843_CONFIG_A     0x00
00099 #define HMC5843_CONFIG_B     0x01
00100 #define HMC5843_MODE         0x02
00101 #define HMC5843_X_MSB        0x03
00102 #define HMC5843_X_LSB        0x04
00103 #define HMC5843_Y_MSB        0x05
00104 #define HMC5843_Y_LSB        0x06
00105 #define HMC5843_Z_MSB        0x07
00106 #define HMC5843_Z_LSB        0x08
00107 #define HMC5843_STATUS       0x09
00108 #define HMC5843_IDENT_A      0x0A
00109 #define HMC5843_IDENT_B      0x0B
00110 #define HMC5843_IDENT_C      0x0C
00111 
00112 
00113 
00114 /**
00115  * Honeywell HMC5843 digital compass.
00116  */
00117 class HMC5843 {
00118 
00119 public:
00120 
00121     /**
00122      * Constructor.
00123      *
00124      * @param sda mbed pin to use for SDA line of I2C interface.
00125      * @param scl mbed pin to use for SCL line of I2C interface.
00126      */
00127     HMC5843(PinName sda, PinName scl);
00128 
00129         
00130      /**
00131      * Enter into sleep mode.
00132      *
00133      */
00134     void setSleepMode();
00135     
00136        
00137      /**
00138      * Set Device in Default Mode.
00139      * HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA
00140      */
00141     void setDefault();
00142     
00143        
00144     /**
00145      * Read the memory location on the device which contains the address.
00146      *
00147      * @param Pointer to a buffer to hold the address value
00148      * Expected     H, 4 and 3.
00149      */
00150     void getAddress(char * address);
00151 
00152 
00153     
00154     /**
00155      * Set the operation mode.
00156      *
00157      * @param mode 0x00 -> Continuous
00158      *             0x01 -> Single
00159      *             0x02 -> Idle
00160      * @param ConfigA values
00161     * @param ConfigB values
00162      */
00163     void setOpMode(int mode, int ConfigA, int ConfigB);
00164     
00165      /**
00166      * Write to  on the device.
00167      *
00168      * @param address Address to write to.
00169      * @param data Data to write.
00170      */
00171     
00172     void write(int address, int data);
00173 
00174      /**
00175      * Get the output of all three axes.
00176      *
00177      * @param Pointer to a buffer to hold the magnetics value for the
00178      *        x-axis, y-axis and z-axis [in that order].
00179      */
00180     void readData(int* readings);
00181     
00182     /**
00183      * Get the output of X axis.
00184      *
00185      * @return x-axis magnetic value
00186      */
00187     int getMx();
00188     
00189     /**
00190      * Get the output of Y axis.
00191      *
00192      * @return y-axis magnetic value
00193      */
00194     int getMy();
00195     
00196     /**
00197      * Get the output of Z axis.
00198      *
00199      * @return z-axis magnetic value
00200      */
00201     int getMz();
00202    
00203     
00204     /**
00205      * Get the current operation mode.
00206      *
00207      * @return Status register values
00208      */
00209     int getStatus(void);
00210 
00211   
00212 
00213     I2C* i2c_;
00214 
00215    
00216 
00217 };
00218 
00219 #endif /* HMC5843_H */