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