hige dura
/
IMU
HMC5883L.cpp@0:b61f317f452e, 2012-01-30 (annotated)
- Committer:
- higedura
- Date:
- Mon Jan 30 08:02:30 2012 +0000
- Revision:
- 0:b61f317f452e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
higedura | 0:b61f317f452e | 1 | /** |
higedura | 0:b61f317f452e | 2 | * @author Jose R. Padron |
higedura | 0:b61f317f452e | 3 | * @author Used HMC6352 library developed by Aaron Berk as template |
higedura | 0:b61f317f452e | 4 | * @section LICENSE |
higedura | 0:b61f317f452e | 5 | * |
higedura | 0:b61f317f452e | 6 | * Copyright (c) 2010 ARM Limited |
higedura | 0:b61f317f452e | 7 | * |
higedura | 0:b61f317f452e | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
higedura | 0:b61f317f452e | 9 | * of this software and associated documentation files (the "Software"), to deal |
higedura | 0:b61f317f452e | 10 | * in the Software without restriction, including without limitation the rights |
higedura | 0:b61f317f452e | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
higedura | 0:b61f317f452e | 12 | * copies of the Software, and to permit persons to whom the Software is |
higedura | 0:b61f317f452e | 13 | * furnished to do so, subject to the following conditions: |
higedura | 0:b61f317f452e | 14 | * |
higedura | 0:b61f317f452e | 15 | * The above copyright notice and this permission notice shall be included in |
higedura | 0:b61f317f452e | 16 | * all copies or substantial portions of the Software. |
higedura | 0:b61f317f452e | 17 | * |
higedura | 0:b61f317f452e | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
higedura | 0:b61f317f452e | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
higedura | 0:b61f317f452e | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
higedura | 0:b61f317f452e | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
higedura | 0:b61f317f452e | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
higedura | 0:b61f317f452e | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
higedura | 0:b61f317f452e | 24 | * THE SOFTWARE. |
higedura | 0:b61f317f452e | 25 | * |
higedura | 0:b61f317f452e | 26 | * @section DESCRIPTION |
higedura | 0:b61f317f452e | 27 | * |
higedura | 0:b61f317f452e | 28 | * Honeywell HMC5883Ldigital compass. |
higedura | 0:b61f317f452e | 29 | * |
higedura | 0:b61f317f452e | 30 | * Datasheet: |
higedura | 0:b61f317f452e | 31 | * |
higedura | 0:b61f317f452e | 32 | * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5883L.pdf |
higedura | 0:b61f317f452e | 33 | */ |
higedura | 0:b61f317f452e | 34 | |
higedura | 0:b61f317f452e | 35 | /** |
higedura | 0:b61f317f452e | 36 | * Includes |
higedura | 0:b61f317f452e | 37 | */ |
higedura | 0:b61f317f452e | 38 | #include "HMC5883L.h" |
higedura | 0:b61f317f452e | 39 | |
higedura | 0:b61f317f452e | 40 | HMC5883L::HMC5883L(PinName sda, PinName scl) { |
higedura | 0:b61f317f452e | 41 | |
higedura | 0:b61f317f452e | 42 | i2c_ = new I2C(sda, scl); |
higedura | 0:b61f317f452e | 43 | //100KHz, as specified by the datasheet. |
higedura | 0:b61f317f452e | 44 | i2c_->frequency(100000); |
higedura | 0:b61f317f452e | 45 | |
higedura | 0:b61f317f452e | 46 | |
higedura | 0:b61f317f452e | 47 | } |
higedura | 0:b61f317f452e | 48 | |
higedura | 0:b61f317f452e | 49 | |
higedura | 0:b61f317f452e | 50 | void HMC5883L::write(int address, int data) { |
higedura | 0:b61f317f452e | 51 | |
higedura | 0:b61f317f452e | 52 | char tx[2]; |
higedura | 0:b61f317f452e | 53 | |
higedura | 0:b61f317f452e | 54 | tx[0]=address; |
higedura | 0:b61f317f452e | 55 | tx[1]=data; |
higedura | 0:b61f317f452e | 56 | |
higedura | 0:b61f317f452e | 57 | i2c_->write(HMC5883L_I2C_WRITE,tx,2); |
higedura | 0:b61f317f452e | 58 | |
higedura | 0:b61f317f452e | 59 | wait_ms(100); |
higedura | 0:b61f317f452e | 60 | |
higedura | 0:b61f317f452e | 61 | } |
higedura | 0:b61f317f452e | 62 | |
higedura | 0:b61f317f452e | 63 | |
higedura | 0:b61f317f452e | 64 | void HMC5883L::setSleepMode() { |
higedura | 0:b61f317f452e | 65 | |
higedura | 0:b61f317f452e | 66 | write(HMC5883L_MODE, HMC5883L_SLEEP); |
higedura | 0:b61f317f452e | 67 | } |
higedura | 0:b61f317f452e | 68 | |
higedura | 0:b61f317f452e | 69 | void HMC5883L::setDefault(void) { |
higedura | 0:b61f317f452e | 70 | |
higedura | 0:b61f317f452e | 71 | write(HMC5883L_CONFIG_A,HMC5883L_10HZ_NORMAL); |
higedura | 0:b61f317f452e | 72 | write(HMC5883L_CONFIG_B,HMC5883L_1_0GA); |
higedura | 0:b61f317f452e | 73 | write(HMC5883L_MODE,HMC5883L_CONTINUOUS); |
higedura | 0:b61f317f452e | 74 | wait_ms(100); |
higedura | 0:b61f317f452e | 75 | } |
higedura | 0:b61f317f452e | 76 | |
higedura | 0:b61f317f452e | 77 | |
higedura | 0:b61f317f452e | 78 | void HMC5883L::getAddress(char *buffer) { |
higedura | 0:b61f317f452e | 79 | |
higedura | 0:b61f317f452e | 80 | char rx[3]; |
higedura | 0:b61f317f452e | 81 | char tx[1]; |
higedura | 0:b61f317f452e | 82 | tx[0]=HMC5883L_IDENT_A; |
higedura | 0:b61f317f452e | 83 | |
higedura | 0:b61f317f452e | 84 | |
higedura | 0:b61f317f452e | 85 | i2c_->write(HMC5883L_I2C_WRITE, tx,1); |
higedura | 0:b61f317f452e | 86 | |
higedura | 0:b61f317f452e | 87 | wait_ms(1); |
higedura | 0:b61f317f452e | 88 | |
higedura | 0:b61f317f452e | 89 | i2c_->read(HMC5883L_I2C_READ,rx,3); |
higedura | 0:b61f317f452e | 90 | |
higedura | 0:b61f317f452e | 91 | buffer[0]=rx[0]; |
higedura | 0:b61f317f452e | 92 | buffer[1]=rx[1]; |
higedura | 0:b61f317f452e | 93 | buffer[2]=rx[2]; |
higedura | 0:b61f317f452e | 94 | } |
higedura | 0:b61f317f452e | 95 | |
higedura | 0:b61f317f452e | 96 | |
higedura | 0:b61f317f452e | 97 | |
higedura | 0:b61f317f452e | 98 | void HMC5883L::setOpMode(int mode, int ConfigA, int ConfigB) { |
higedura | 0:b61f317f452e | 99 | |
higedura | 0:b61f317f452e | 100 | |
higedura | 0:b61f317f452e | 101 | write(HMC5883L_CONFIG_A,ConfigA); |
higedura | 0:b61f317f452e | 102 | write(HMC5883L_CONFIG_B,ConfigB); |
higedura | 0:b61f317f452e | 103 | write(HMC5883L_MODE,mode); |
higedura | 0:b61f317f452e | 104 | |
higedura | 0:b61f317f452e | 105 | |
higedura | 0:b61f317f452e | 106 | } |
higedura | 0:b61f317f452e | 107 | |
higedura | 0:b61f317f452e | 108 | |
higedura | 0:b61f317f452e | 109 | |
higedura | 0:b61f317f452e | 110 | |
higedura | 0:b61f317f452e | 111 | void HMC5883L::readData(int* getMag) { |
higedura | 0:b61f317f452e | 112 | |
higedura | 0:b61f317f452e | 113 | |
higedura | 0:b61f317f452e | 114 | char tx[1]; |
higedura | 0:b61f317f452e | 115 | char rx[2]; |
higedura | 0:b61f317f452e | 116 | |
higedura | 0:b61f317f452e | 117 | |
higedura | 0:b61f317f452e | 118 | tx[0]=HMC5883L_X_MSB; |
higedura | 0:b61f317f452e | 119 | i2c_->write(HMC5883L_I2C_READ,tx,1); |
higedura | 0:b61f317f452e | 120 | i2c_->read(HMC5883L_I2C_READ,rx,2); |
higedura | 0:b61f317f452e | 121 | getMag[0]= (int)rx[0]<<8|(int)rx[1]; |
higedura | 0:b61f317f452e | 122 | |
higedura | 0:b61f317f452e | 123 | |
higedura | 0:b61f317f452e | 124 | tx[0]=HMC5883L_Y_MSB; |
higedura | 0:b61f317f452e | 125 | i2c_->write(HMC5883L_I2C_READ,tx,1); |
higedura | 0:b61f317f452e | 126 | i2c_->read(HMC5883L_I2C_READ,rx,2); |
higedura | 0:b61f317f452e | 127 | getMag[1]= (int)rx[0]<<8|(int)rx[1]; |
higedura | 0:b61f317f452e | 128 | |
higedura | 0:b61f317f452e | 129 | tx[0]=HMC5883L_Z_MSB; |
higedura | 0:b61f317f452e | 130 | i2c_->write(HMC5883L_I2C_READ,tx,1); |
higedura | 0:b61f317f452e | 131 | i2c_->read(HMC5883L_I2C_READ,rx,2); |
higedura | 0:b61f317f452e | 132 | getMag[2]= (int)rx[0]<<8|(int)rx[1]; |
higedura | 0:b61f317f452e | 133 | |
higedura | 0:b61f317f452e | 134 | } |
higedura | 0:b61f317f452e | 135 | |
higedura | 0:b61f317f452e | 136 | int HMC5883L::getMx() { |
higedura | 0:b61f317f452e | 137 | |
higedura | 0:b61f317f452e | 138 | char tx[1]; |
higedura | 0:b61f317f452e | 139 | char rx[2]; |
higedura | 0:b61f317f452e | 140 | |
higedura | 0:b61f317f452e | 141 | |
higedura | 0:b61f317f452e | 142 | tx[0]=HMC5883L_X_MSB; |
higedura | 0:b61f317f452e | 143 | i2c_->write(HMC5883L_I2C_READ,tx,1); |
higedura | 0:b61f317f452e | 144 | i2c_->read(HMC5883L_I2C_READ,rx,2); |
higedura | 0:b61f317f452e | 145 | return ((int)rx[0]<<8|(int)rx[1]); |
higedura | 0:b61f317f452e | 146 | |
higedura | 0:b61f317f452e | 147 | } |
higedura | 0:b61f317f452e | 148 | |
higedura | 0:b61f317f452e | 149 | int HMC5883L::getMy() { |
higedura | 0:b61f317f452e | 150 | |
higedura | 0:b61f317f452e | 151 | char tx[1]; |
higedura | 0:b61f317f452e | 152 | char rx[2]; |
higedura | 0:b61f317f452e | 153 | |
higedura | 0:b61f317f452e | 154 | |
higedura | 0:b61f317f452e | 155 | tx[0]=HMC5883L_Y_MSB; |
higedura | 0:b61f317f452e | 156 | i2c_->write(HMC5883L_I2C_READ,tx,1); |
higedura | 0:b61f317f452e | 157 | i2c_->read(HMC5883L_I2C_READ,rx,2); |
higedura | 0:b61f317f452e | 158 | return ((int)rx[0]<<8|(int)rx[1]); |
higedura | 0:b61f317f452e | 159 | |
higedura | 0:b61f317f452e | 160 | } |
higedura | 0:b61f317f452e | 161 | |
higedura | 0:b61f317f452e | 162 | |
higedura | 0:b61f317f452e | 163 | int HMC5883L::getMz(){ |
higedura | 0:b61f317f452e | 164 | |
higedura | 0:b61f317f452e | 165 | char tx[1]; |
higedura | 0:b61f317f452e | 166 | char rx[2]; |
higedura | 0:b61f317f452e | 167 | |
higedura | 0:b61f317f452e | 168 | |
higedura | 0:b61f317f452e | 169 | tx[0]=HMC5883L_Z_MSB; |
higedura | 0:b61f317f452e | 170 | i2c_->write(HMC5883L_I2C_READ,tx,1); |
higedura | 0:b61f317f452e | 171 | i2c_->read(HMC5883L_I2C_READ,rx,2); |
higedura | 0:b61f317f452e | 172 | return ((int)rx[0]<<8|(int)rx[1]); |
higedura | 0:b61f317f452e | 173 | |
higedura | 0:b61f317f452e | 174 | } |