Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.
Dependencies: Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo
HMC5843.cpp
00001 /** 00002 * @author Jose R. Padron 00003 * @author Used HMC6352 library developed by Aaron Berk as template 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 HMC5843digital compass. 00029 * 00030 * Datasheet: 00031 * 00032 * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf 00033 */ 00034 00035 /** 00036 * Includes 00037 */ 00038 #include "HMC5843.h" 00039 00040 HMC5843::HMC5843(PinName sda, PinName scl) 00041 { 00042 i2c_ = new I2C(sda, scl); 00043 //100KHz, as specified by the datasheet. 00044 i2c_->frequency(100000); 00045 setOpMode(HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL, HMC5843_1_0GA); 00046 00047 return; 00048 } 00049 00050 00051 void HMC5843::setOffset(float x, float y, float z) 00052 { 00053 return; 00054 } 00055 00056 00057 void HMC5843::setScale(float x, float y, float z) 00058 { 00059 return; 00060 } 00061 00062 00063 void HMC5843::setSleepMode() 00064 { 00065 write(HMC5843_MODE, HMC5843_SLEEP); 00066 00067 return; 00068 } 00069 00070 00071 void HMC5843::setDefault(void) 00072 { 00073 write(HMC5843_CONFIG_A,HMC5843_10HZ_NORMAL); 00074 write(HMC5843_CONFIG_B,HMC5843_1_0GA); 00075 write(HMC5843_MODE,HMC5843_CONTINUOUS); 00076 wait_ms(100); 00077 00078 return; 00079 } 00080 00081 00082 void HMC5843::setOpMode(int mode, int ConfigA, int ConfigB) 00083 { 00084 write(HMC5843_CONFIG_A,ConfigA); 00085 write(HMC5843_CONFIG_B,ConfigB); 00086 write(HMC5843_MODE,mode); 00087 00088 return; 00089 } 00090 00091 00092 void HMC5843::write(int address, int data) 00093 { 00094 char tx[2]; 00095 00096 tx[0]=address; 00097 tx[1]=data; 00098 00099 i2c_->write(HMC5843_I2C_WRITE,tx,2); 00100 00101 wait_ms(100); 00102 00103 return; 00104 } 00105 00106 00107 void HMC5843::read(int m[3]) 00108 { 00109 readData(m); 00110 00111 return; 00112 } 00113 00114 void HMC5843::readMag(int m[3]) 00115 { 00116 readData(m); 00117 00118 return; 00119 } 00120 00121 void HMC5843::readData(int readings[3]) 00122 { 00123 char tx[1]; 00124 char rx[2]; 00125 00126 00127 tx[0]=HMC5843_X_MSB; 00128 i2c_->write(HMC5843_I2C_READ,tx,1); 00129 i2c_->read(HMC5843_I2C_READ,rx,2); 00130 readings[0]= (short)(rx[0]<<8|rx[1]); 00131 00132 00133 tx[0]=HMC5843_Y_MSB; 00134 i2c_->write(HMC5843_I2C_READ,tx,1); 00135 i2c_->read(HMC5843_I2C_READ,rx,2); 00136 readings[1]= (short) (rx[0]<<8|rx[1]); 00137 00138 tx[0]=HMC5843_Z_MSB; 00139 i2c_->write(HMC5843_I2C_READ,tx,1); 00140 i2c_->read(HMC5843_I2C_READ,rx,2); 00141 readings[2]= (short) (rx[0]<<8|rx[1]); 00142 00143 return; 00144 } 00145 00146 int HMC5843::getMx() 00147 { 00148 char tx[1]; 00149 char rx[2]; 00150 00151 00152 tx[0]=HMC5843_X_MSB; 00153 i2c_->write(HMC5843_I2C_READ,tx,1); 00154 i2c_->read(HMC5843_I2C_READ,rx,2); 00155 00156 return (short) rx[0]<<8|rx[1]; 00157 } 00158 00159 int HMC5843::getMy() 00160 { 00161 char tx[1]; 00162 char rx[2]; 00163 00164 00165 tx[0]=HMC5843_Y_MSB; 00166 i2c_->write(HMC5843_I2C_READ,tx,1); 00167 i2c_->read(HMC5843_I2C_READ,rx,2); 00168 00169 return (short) rx[0]<<8|rx[1]; 00170 } 00171 00172 00173 int HMC5843::getMz(){ 00174 00175 char tx[1]; 00176 char rx[2]; 00177 00178 00179 tx[0]=HMC5843_Z_MSB; 00180 i2c_->write(HMC5843_I2C_READ,tx,1); 00181 i2c_->read(HMC5843_I2C_READ,rx,2); 00182 00183 return (short) rx[0]<<8|rx[1]; 00184 } 00185 00186 00187 int HMC5843::getStatus(void) 00188 { 00189 return 0; 00190 } 00191 00192 00193 void HMC5843::getAddress(char *buffer) 00194 { 00195 char rx[3]; 00196 char tx[1]; 00197 tx[0]=HMC5843_IDENT_A; 00198 00199 00200 i2c_->write(HMC5843_I2C_WRITE, tx,1); 00201 00202 wait_ms(1); 00203 00204 i2c_->read(HMC5843_I2C_READ,rx,3); 00205 00206 buffer[0]=rx[0]; 00207 buffer[1]=rx[1]; 00208 buffer[2]=rx[2]; 00209 00210 return; 00211 } 00212 00213 00214 void HMC5843::frequency(int hz) 00215 { 00216 if (hz == 100000) 00217 i2c_->frequency(100000); 00218 else if (hz == 400000) 00219 i2c_->frequency(400000); 00220 00221 return; 00222 }
Generated on Tue Jul 12 2022 14:09:25 by 1.7.2