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