Private HMC5843 library

Committer:
atommota
Date:
Fri Jan 07 19:12:42 2011 +0000
Revision:
0:63ccd7376105

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atommota 0:63ccd7376105 1 /**
atommota 0:63ccd7376105 2 * @author Jose R. Padron
atommota 0:63ccd7376105 3 *@author Used HMCHMC6352 library developed by Aaron Berk as template
atommota 0:63ccd7376105 4 * @section LICENSE
atommota 0:63ccd7376105 5 *
atommota 0:63ccd7376105 6 * Copyright (c) 2010 ARM Limited
atommota 0:63ccd7376105 7 *
atommota 0:63ccd7376105 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
atommota 0:63ccd7376105 9 * of this software and associated documentation files (the "Software"), to deal
atommota 0:63ccd7376105 10 * in the Software without restriction, including without limitation the rights
atommota 0:63ccd7376105 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
atommota 0:63ccd7376105 12 * copies of the Software, and to permit persons to whom the Software is
atommota 0:63ccd7376105 13 * furnished to do so, subject to the following conditions:
atommota 0:63ccd7376105 14 *
atommota 0:63ccd7376105 15 * The above copyright notice and this permission notice shall be included in
atommota 0:63ccd7376105 16 * all copies or substantial portions of the Software.
atommota 0:63ccd7376105 17 *
atommota 0:63ccd7376105 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
atommota 0:63ccd7376105 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
atommota 0:63ccd7376105 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
atommota 0:63ccd7376105 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
atommota 0:63ccd7376105 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
atommota 0:63ccd7376105 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
atommota 0:63ccd7376105 24 * THE SOFTWARE.
atommota 0:63ccd7376105 25 *
atommota 0:63ccd7376105 26 * @section DESCRIPTION
atommota 0:63ccd7376105 27 *
atommota 0:63ccd7376105 28 * Honeywell HMC5843digital compass.
atommota 0:63ccd7376105 29 *
atommota 0:63ccd7376105 30 * Datasheet:
atommota 0:63ccd7376105 31 *
atommota 0:63ccd7376105 32 * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf
atommota 0:63ccd7376105 33 */
atommota 0:63ccd7376105 34
atommota 0:63ccd7376105 35 /**
atommota 0:63ccd7376105 36 * Includes
atommota 0:63ccd7376105 37 */
atommota 0:63ccd7376105 38 #include "HMC5843.h"
atommota 0:63ccd7376105 39
atommota 0:63ccd7376105 40 HMC5843::HMC5843(PinName sda, PinName scl) {
atommota 0:63ccd7376105 41
atommota 0:63ccd7376105 42 i2c_ = new I2C(sda, scl);
atommota 0:63ccd7376105 43 //100KHz, as specified by the datasheet.
atommota 0:63ccd7376105 44 i2c_->frequency(100000);
atommota 0:63ccd7376105 45
atommota 0:63ccd7376105 46
atommota 0:63ccd7376105 47 }
atommota 0:63ccd7376105 48
atommota 0:63ccd7376105 49
atommota 0:63ccd7376105 50 void HMC5843::write(int address, int data) {
atommota 0:63ccd7376105 51
atommota 0:63ccd7376105 52 char tx[2];
atommota 0:63ccd7376105 53
atommota 0:63ccd7376105 54 tx[0]=address;
atommota 0:63ccd7376105 55 tx[1]=data;
atommota 0:63ccd7376105 56
atommota 0:63ccd7376105 57 i2c_->write(HMC5843_I2C_WRITE,tx,2);
atommota 0:63ccd7376105 58
atommota 0:63ccd7376105 59 wait_ms(100);
atommota 0:63ccd7376105 60
atommota 0:63ccd7376105 61 }
atommota 0:63ccd7376105 62
atommota 0:63ccd7376105 63
atommota 0:63ccd7376105 64 void HMC5843::setSleepMode() {
atommota 0:63ccd7376105 65
atommota 0:63ccd7376105 66 write(HMC5843_MODE, HMC5843_SLEEP);
atommota 0:63ccd7376105 67 }
atommota 0:63ccd7376105 68
atommota 0:63ccd7376105 69 void HMC5843::setDefault(void) {
atommota 0:63ccd7376105 70
atommota 0:63ccd7376105 71 write(HMC5843_CONFIG_A,HMC5843_10HZ_NORMAL);
atommota 0:63ccd7376105 72 write(HMC5843_CONFIG_B,HMC5843_1_0GA);
atommota 0:63ccd7376105 73 write(HMC5843_MODE,HMC5843_CONTINUOUS);
atommota 0:63ccd7376105 74 wait_ms(100);
atommota 0:63ccd7376105 75 }
atommota 0:63ccd7376105 76
atommota 0:63ccd7376105 77 //void HMC5843::SelfTest() {
atommota 0:63ccd7376105 78
atommota 0:63ccd7376105 79 //Future
atommota 0:63ccd7376105 80
atommota 0:63ccd7376105 81 //}
atommota 0:63ccd7376105 82
atommota 0:63ccd7376105 83
atommota 0:63ccd7376105 84 void HMC5843::getAddress(char *buffer) {
atommota 0:63ccd7376105 85
atommota 0:63ccd7376105 86 char rx[3];
atommota 0:63ccd7376105 87 char tx[1];
atommota 0:63ccd7376105 88 tx[0]=HMC5843_IDENT_A;
atommota 0:63ccd7376105 89
atommota 0:63ccd7376105 90
atommota 0:63ccd7376105 91 i2c_->write(HMC5843_I2C_WRITE, tx,1);
atommota 0:63ccd7376105 92
atommota 0:63ccd7376105 93 wait_ms(1);
atommota 0:63ccd7376105 94
atommota 0:63ccd7376105 95 i2c_->read(HMC5843_I2C_READ,rx,3);
atommota 0:63ccd7376105 96
atommota 0:63ccd7376105 97 buffer[0]=rx[0];
atommota 0:63ccd7376105 98 buffer[1]=rx[1];
atommota 0:63ccd7376105 99 buffer[2]=rx[2];
atommota 0:63ccd7376105 100 }
atommota 0:63ccd7376105 101
atommota 0:63ccd7376105 102
atommota 0:63ccd7376105 103
atommota 0:63ccd7376105 104 void HMC5843::setOpMode(int mode, int ConfigA, int ConfigB) {
atommota 0:63ccd7376105 105
atommota 0:63ccd7376105 106
atommota 0:63ccd7376105 107 write(HMC5843_CONFIG_A,ConfigA);
atommota 0:63ccd7376105 108 write(HMC5843_CONFIG_B,ConfigB);
atommota 0:63ccd7376105 109 write(HMC5843_MODE,mode);
atommota 0:63ccd7376105 110
atommota 0:63ccd7376105 111
atommota 0:63ccd7376105 112 }
atommota 0:63ccd7376105 113
atommota 0:63ccd7376105 114
atommota 0:63ccd7376105 115
atommota 0:63ccd7376105 116
atommota 0:63ccd7376105 117 void HMC5843::readData(int* readings) {
atommota 0:63ccd7376105 118
atommota 0:63ccd7376105 119
atommota 0:63ccd7376105 120 char tx[1];
atommota 0:63ccd7376105 121 char rx[2];
atommota 0:63ccd7376105 122
atommota 0:63ccd7376105 123
atommota 0:63ccd7376105 124 tx[0]=HMC5843_X_MSB;
atommota 0:63ccd7376105 125 i2c_->write(HMC5843_I2C_READ,tx,1);
atommota 0:63ccd7376105 126 i2c_->read(HMC5843_I2C_READ,rx,2);
atommota 0:63ccd7376105 127 readings[0]= (int)rx[0]<<8|(int)rx[1];
atommota 0:63ccd7376105 128
atommota 0:63ccd7376105 129
atommota 0:63ccd7376105 130 tx[0]=HMC5843_Y_MSB;
atommota 0:63ccd7376105 131 i2c_->write(HMC5843_I2C_READ,tx,1);
atommota 0:63ccd7376105 132 i2c_->read(HMC5843_I2C_READ,rx,2);
atommota 0:63ccd7376105 133 readings[1]= (int)rx[0]<<8|(int)rx[1];
atommota 0:63ccd7376105 134
atommota 0:63ccd7376105 135 tx[0]=HMC5843_Z_MSB;
atommota 0:63ccd7376105 136 i2c_->write(HMC5843_I2C_READ,tx,1);
atommota 0:63ccd7376105 137 i2c_->read(HMC5843_I2C_READ,rx,2);
atommota 0:63ccd7376105 138 readings[2]= (int)rx[0]<<8|(int)rx[1];
atommota 0:63ccd7376105 139
atommota 0:63ccd7376105 140
atommota 0:63ccd7376105 141 //readings[0] = (int)buffer[0] << 8 | (int)buffer[1];
atommota 0:63ccd7376105 142 //readings[1] = (int)buffer[2] << 8 | (int)buffer[3];
atommota 0:63ccd7376105 143 //readings[2] = (int)buffer[4] << 8 | (int)buffer[5];
atommota 0:63ccd7376105 144
atommota 0:63ccd7376105 145 // readings[0]=buffer[0];
atommota 0:63ccd7376105 146 //readings[1]=buffer[1];
atommota 0:63ccd7376105 147 // readings[2]=buffer[2];
atommota 0:63ccd7376105 148 // readings[3]=buffer[3];
atommota 0:63ccd7376105 149 // readings[4]=buffer[4];
atommota 0:63ccd7376105 150 // readings[5]=buffer[5];
atommota 0:63ccd7376105 151 }