test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC6352.h Source File

HMC6352.h

00001 /**
00002  * @author Aaron Berk
00003  * 
00004  * @section LICENSE
00005  *
00006  * Copyright (c) 2010 ARM Limited
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  * @section DESCRIPTION
00027  *
00028  * Honeywell HMC6352 digital compass.
00029  *
00030  * Datasheet:
00031  *
00032  * http://www.ssec.honeywell.com/magnetic/datasheets/HMC6352.pdf
00033  */
00034 
00035 #ifndef HMC6352_H
00036 #define HMC6352_H
00037 
00038 /**
00039  * Includes
00040  */
00041 #include "mbed.h"
00042 
00043 /**
00044  * Defines
00045  */
00046 #define HMC6352_I2C_ADDRESS  (0x21<<1) //7-bit address. 0x43 write, 0x42 read.
00047 
00048 //Commands.
00049 #define HMC6352_EEPROM_WRITE 0x77
00050 #define HMC6352_EEPROM_READ  0x72
00051 #define HMC6352_RAM_WRITE    0x47
00052 #define HMC6352_RAM_READ     0x67
00053 #define HMC6352_ENTER_SLEEP  0x53
00054 #define HMC6352_EXIT_SLEEP   0x57
00055 #define HMC6352_SET_RESET    0x4F
00056 #define HMC6352_ENTER_CALIB  0x43
00057 #define HMC6352_EXIT_CALIB   0x45
00058 #define HMC6352_SAVE_OPMODE  0x4C
00059 #define HMC6352_GET_DATA     0x41
00060 
00061 //EEPROM locations.
00062 #define HMC6352_SLAVE_ADDR   0x00
00063 #define HMC6352_MX_OFF_MSB   0x01
00064 #define HMC6352_MX_OFF_LSB   0x02
00065 #define HMC6352_MY_OFF_MSB   0x03
00066 #define HMC6352_MY_OFF_LSB   0x04
00067 #define HMC6352_TIME_DELAY   0x05
00068 #define HMC6352_SUMMED       0x06
00069 #define HMC6352_SOFT_VER     0x07
00070 #define HMC6352_OPMODE       0x08
00071 
00072 //RAM registers.
00073 #define HMC6352_RAM_OPMODE   0x74
00074 #define HMC6352_RAM_OUTPUT   0x4E
00075 
00076 #define HMC6352_MX_OFFSET    0x00
00077 #define HMC6352_MY_OFFSET    0x01
00078 
00079 #define HMC6352_HEADING_MODE 0x00
00080 #define HMC6352_RAWMAGX_MODE 0x01
00081 #define HMC6352_RAWMAGY_MODE 0x02
00082 #define HMC6352_MAGX_MODE    0x03
00083 #define HMC6352_MAGY_MODE    0x04
00084 
00085 //Operational mode register masks.
00086 #define HMC6352_CM_MR_1HZ    0x00
00087 #define HMC6352_CM_MR_5HZ    0x20
00088 #define HMC6352_CM_MR_10HZ   0x40
00089 #define HMC6352_CM_MR_20HZ   0x60
00090 
00091 #define HMC6352_PERIODIC_SR  0x10
00092 
00093 #define HMC6352_STANDBY      0x00
00094 #define HMC6352_QUERY        0x01
00095 #define HMC6352_CONTINUOUS   0x02
00096 
00097 /**
00098  * Honeywell HMC6352 digital compass.
00099  */
00100 class HMC6352 {
00101 
00102 public:
00103 
00104     /**
00105      * Constructor.
00106      *
00107      * @param sda mbed pin to use for SDA line of I2C interface.
00108      * @param scl mbed pin to use for SCL line of I2C interface.
00109      */
00110     HMC6352(PinName sda, PinName scl);
00111 
00112     /**
00113      * Sample the device and return the result.
00114      *
00115      * @return In heading output mode, the current heading as a number between
00116      *         0-3599, representing 0-359.9 degrees.
00117      *         In raw magnetometer X output mode, the raw output of the X-axis
00118      *         magnetometer.
00119      *         In raw magnetometer Y mode, the raw output of the Y-axis
00120      *         magnetometer.
00121      *         In magnetometer X mode, the corrected output of the X-axis
00122      *         magnetometer.
00123      *         In magnetometer Y mode, the corrected output of the Y-axis
00124      *         magnetometer.
00125      */
00126     int sample(void);
00127 
00128     /**
00129      * Enter into or exit from sleep mode.
00130      *
00131      * @param enterOrExit 0x57 -> Exit sleep mode
00132      *                    0x53 -> Enter
00133      */
00134     void setSleepMode(int enterOrExit);
00135 
00136     /**
00137      * Update bridge offsets.
00138      *
00139      * Performs a set/reset immediately.
00140      */
00141     void setReset(void);
00142 
00143     /**
00144      * Enter into or exit from calibration mode.
00145      *
00146      * @param enterOrExit 0x45 -> Exit
00147      *                    0x43 -> Enter
00148      */
00149     void setCalibrationMode(int enterOrExit);
00150 
00151     /**
00152      * Save the current operation mode byte to EEPROM.
00153      */
00154     void saveOpMode(void);
00155 
00156     /**
00157      * Read the memory location on the device which contains the slave address.
00158      *
00159      * @return The slave address of the device.
00160      */
00161     int getSlaveAddress(void);
00162 
00163     /**
00164      * Read the current offset for X or Y axis magnetometer.
00165      *
00166      * @param axis 0x00 -> X-axis
00167      *             0x01 -> Y-axis
00168      * @return The current offset for the axis as a 16-bit number.
00169      */
00170     int getOffset(int axis);
00171 
00172     /**
00173      * Set the offset for X or Y axis magnetometer.
00174      *
00175      * @param axis 0x00 -> X-axis
00176      *             0x01 -> Y-axis
00177      * @param offset The offset to set for the axis (set to a 16-bit number).
00178      */
00179     void setOffset(int axis, int offset);
00180 
00181     /**
00182      * Get the current time delay.
00183      *
00184      * @return The time delay in milliseconds.
00185      */
00186     int getTimeDelay(void);
00187 
00188     /**
00189      * Set the time delay of the device.
00190      *
00191      * @param delay The time delay to set in milliseconds (between 0-255);
00192      */
00193     void setTimeDelay(int delay);
00194 
00195     /**
00196      * Get the number of measurements being summed each sample.
00197      *
00198      * @return The number of summed measurement.
00199      */
00200     int getSumNumber(void);
00201 
00202     /**
00203      * Set the number of measurements being summed each sample.
00204      *
00205      * @param sum The number of measurements to be summed (between 0-16);
00206      */
00207     void setSumNumber(int sum);
00208 
00209     /**
00210      * Get the software version on the device.
00211      *
00212      * @return The software version number.
00213      */
00214     int getSoftwareVersion(void);
00215 
00216     /**
00217      * Get the current operation mode.
00218      *
00219      * @return 0x00 -> Standby mode
00220      *         0x01 -> Query mode
00221      *         0x02 -> Continuous mode
00222      */
00223     int getOpMode(void);
00224 
00225     /**
00226      * Set the operation mode.
00227      *
00228      * @param mode 0x00 -> Standby mode
00229      *             0x01 -> Query mode
00230      *             0x02 -> Continuous mode
00231      * @param periodicSetReset 0x00 -> No periodic set/reset
00232      *                         0x01 -> Periodic set/reset
00233      * @measurementRate Measurement rate in Hz for continuous rate.
00234      *                  Possible rates: {1, 5, 10, 20}Hz.
00235      */
00236     void setOpMode(int mode, int periodicSetReset, int measurementRate = 0);
00237 
00238     /**
00239      * Get the current output mode of the device.
00240      *
00241      * @return The current output mode.
00242      */
00243     int getOutputMode(void);
00244 
00245     /**
00246      * Set the output mode of the device.
00247      *
00248      * @param mode 0x00 -> Heading mode
00249      *             0x01 -> Raw magnetometer X mode
00250      *             0x02 -> Raw magnetometer Y mode
00251      *             0x03 -> Magnetometer X mode
00252      *             0x04 -> Magnetometer Y mode
00253      */
00254     void setOutputMode(int mode);
00255 
00256 private:
00257 
00258     I2C* i2c_;
00259     int  operationMode_;
00260 
00261     /**
00262      * Write to EEPROM or RAM on the device.
00263      *
00264      * @param EepromOrRam 0x77 -> Writing to EEPROM
00265      *                    0x47 -> Writing to RAM
00266      * @param address Address to write to.
00267      * @param data Data to write.
00268      */
00269     void write(int EepromOrRam, int address, int data);
00270 
00271     /**
00272      * Read EEPROM or RAM on the device.
00273      *
00274      * @param EepromOrRam 0x72 -> Reading from EEPROM
00275      *                    0x67 -> Reading from RAM
00276      * @param address Address to read from.
00277      * @return The contents of the memory address.
00278      */
00279     int read(int EepromOrRam, int address);
00280 
00281 };
00282 
00283 #endif /* HMC6352_H */