test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC6352.cpp Source File

HMC6352.cpp

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 /**
00036  * Includes
00037  */
00038 #include "HMC6352.h"
00039 
00040 HMC6352::HMC6352(PinName sda, PinName scl) {
00041 
00042     i2c_ = new I2C(sda, scl);
00043     //100KHz, as specified by the datasheet.
00044     i2c_->frequency(100000);
00045 
00046     operationMode_ = getOpMode();
00047 
00048 }
00049 
00050 int HMC6352::sample(void) {
00051 
00052     char tx[1];
00053     char rx[2];
00054 
00055     if (operationMode_ == HMC6352_STANDBY || operationMode_ == HMC6352_QUERY) {
00056         tx[0] = HMC6352_GET_DATA;
00057 
00058         i2c_->write((HMC6352_I2C_ADDRESS << 1) & 0xFE, tx, 1);
00059         wait_ms(1);
00060     }
00061 
00062     i2c_->read((HMC6352_I2C_ADDRESS << 1) | 0x01, rx, 2);
00063     wait_ms(1);
00064 
00065     return (((int)rx[0] << 8) | (int)rx[1]);
00066 
00067 }
00068 
00069 void HMC6352::setSleepMode(int exitOrEnter) {
00070 
00071     char tx[1];
00072 
00073     tx[0] = exitOrEnter;
00074 
00075     i2c_->write((HMC6352_I2C_ADDRESS << 1) & 0xFE, tx, 1);
00076     wait_ms(1);
00077 
00078 }
00079 
00080 void HMC6352::setReset(void) {
00081 
00082     char tx[1];
00083 
00084     tx[0] = HMC6352_SET_RESET;
00085 
00086     i2c_->write((HMC6352_I2C_ADDRESS << 1) & 0xFE, tx, 1);
00087     wait_ms(7);
00088 
00089 }
00090 
00091 void HMC6352::setCalibrationMode(int exitOrEnter) {
00092 
00093     char tx[1];
00094     int delay = 0;
00095 
00096     tx[0] = exitOrEnter;
00097 
00098     if (exitOrEnter == HMC6352_EXIT_CALIB) {
00099         delay = 15;
00100     } else if (exitOrEnter == HMC6352_ENTER_CALIB) {
00101         delay = 1;
00102     }
00103 
00104     i2c_->write((HMC6352_I2C_ADDRESS << 1) & 0xFE, tx, 1);
00105     wait_ms(delay);
00106 
00107 }
00108 
00109 void HMC6352::saveOpMode(void) {
00110 
00111     char tx[1];
00112 
00113     tx[0] = HMC6352_SAVE_OPMODE;
00114 
00115     i2c_->write((HMC6352_I2C_ADDRESS << 1) & 0xFE, tx, 1);
00116     wait_ms(1);
00117 
00118 }
00119 
00120 int HMC6352::getSlaveAddress(void) {
00121 
00122     return read(HMC6352_EEPROM_READ, HMC6352_SLAVE_ADDR);
00123 
00124 }
00125 
00126 int HMC6352::getOffset(int axis) {
00127 
00128     char rx[2] = {0x00, 0x00};
00129 
00130     if (axis == HMC6352_MX_OFFSET) {
00131 
00132         rx[0] = read(HMC6352_EEPROM_READ, HMC6352_MX_OFF_MSB);
00133         rx[1] = read(HMC6352_EEPROM_READ, HMC6352_MX_OFF_LSB);
00134 
00135     } else if (axis == HMC6352_MY_OFFSET) {
00136 
00137         rx[0] = read(HMC6352_EEPROM_READ, HMC6352_MY_OFF_MSB);
00138         rx[1] = read(HMC6352_EEPROM_READ, HMC6352_MY_OFF_LSB);
00139 
00140     }
00141 
00142     return ((rx[0] << 8) | (rx[1]));
00143 
00144 }
00145 
00146 void HMC6352::setOffset(int axis, int offset) {
00147 
00148     char tx[2] = {0x00, 0x00};
00149 
00150     tx[0] = (offset & 0x0000FF00) >> 8;
00151     tx[1] = (offset & 0x000000FF);
00152 
00153     if (axis == HMC6352_MX_OFFSET) {
00154 
00155         write(HMC6352_EEPROM_WRITE, HMC6352_MX_OFF_MSB, tx[0]);
00156         write(HMC6352_EEPROM_WRITE, HMC6352_MX_OFF_MSB, tx[1]);
00157 
00158     } else if (axis == HMC6352_MY_OFFSET) {
00159 
00160         write(HMC6352_EEPROM_WRITE, HMC6352_MY_OFF_MSB, tx[0]);
00161         write(HMC6352_EEPROM_WRITE, HMC6352_MY_OFF_MSB, tx[1]);
00162 
00163     }
00164 
00165 }
00166 
00167 int HMC6352::getTimeDelay(void) {
00168 
00169     return read(HMC6352_EEPROM_READ, HMC6352_TIME_DELAY);
00170 
00171 }
00172 
00173 void HMC6352::setTimeDelay(int delay) {
00174 
00175     write(HMC6352_EEPROM_WRITE, HMC6352_TIME_DELAY, delay);
00176 
00177 }
00178 
00179 int HMC6352::getSumNumber(void) {
00180 
00181     return read(HMC6352_EEPROM_READ, HMC6352_SUMMED);
00182 
00183 }
00184 
00185 void HMC6352::setSumNumber(int sum) {
00186 
00187     write(HMC6352_EEPROM_WRITE, HMC6352_SUMMED, sum);
00188 
00189 }
00190 
00191 int HMC6352::getSoftwareVersion(void) {
00192 
00193     return read(HMC6352_EEPROM_READ, HMC6352_SOFT_VER);
00194 
00195 }
00196 
00197 int HMC6352::getOpMode(void) {
00198 
00199     int response = 0;
00200 
00201     response = read(HMC6352_RAM_READ, HMC6352_RAM_OPMODE);
00202 
00203     return (response & 0x00000003);
00204 
00205 }
00206 
00207 void HMC6352::setOpMode(int mode, int periodicSetReset, int measurementRate) {
00208 
00209     char opModeByte = mode;
00210 
00211     if (periodicSetReset == 1) {
00212         opModeByte |= HMC6352_PERIODIC_SR;
00213     }
00214 
00215     if (measurementRate == 5) {
00216         opModeByte |= HMC6352_CM_MR_5HZ;
00217     } else if (measurementRate == 10) {
00218         opModeByte |= HMC6352_CM_MR_10HZ;
00219     } else if (measurementRate == 20) {
00220         opModeByte |= HMC6352_CM_MR_20HZ;
00221     }
00222 
00223     write(HMC6352_RAM_WRITE, HMC6352_RAM_OPMODE, opModeByte);
00224     write(HMC6352_EEPROM_WRITE, HMC6352_OPMODE, opModeByte);
00225 
00226     operationMode_ = mode;
00227 
00228 }
00229 
00230 int HMC6352::getOutputMode(void) {
00231 
00232     return read(HMC6352_RAM_READ, HMC6352_RAM_OUTPUT);
00233 
00234 }
00235 
00236 void HMC6352::setOutputMode(int mode) {
00237 
00238     write(HMC6352_RAM_WRITE, HMC6352_RAM_OUTPUT, mode);
00239 
00240 }
00241 
00242 void HMC6352::write(int EepromOrRam, int address, int data) {
00243 
00244     char tx[3];
00245 
00246     tx[0] = EepromOrRam;
00247     tx[1] = address;
00248     tx[2] = data;
00249 
00250     i2c_->write((HMC6352_I2C_ADDRESS << 1) & 0xFE, tx, 3);
00251     wait_ms(1);
00252 
00253 }
00254 
00255 int HMC6352::read(int EepromOrRam, int address) {
00256 
00257     char tx[2];
00258     char rx[1];
00259 
00260     tx[0] = EepromOrRam;
00261     tx[1] = address;
00262 
00263     i2c_->write((HMC6352_I2C_ADDRESS << 1) & 0xFE, tx, 2);
00264     wait_ms(1);
00265     i2c_->read((HMC6352_I2C_ADDRESS << 1) | 0x01, rx, 1);
00266     wait_ms(1);
00267 
00268     return (rx[0]);
00269 
00270 }