James Watanabe / HMC5843

Fork of HMC5843 by Jose R Padron

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC5843.cpp Source File

HMC5843.cpp

Go to the documentation of this file.
00001 /**
00002  * @file HMC5843.cpp
00003  * @author Jose R. Padron
00004  * @author Used HMC6352 library  developed by Aaron Berk as template
00005  * @section LICENSE
00006  *
00007  * Copyright (c) 2010 ARM Limited
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  *
00027  * @section DESCRIPTION
00028  *
00029  * Honeywell HMC5843digital compass.
00030  *
00031  * Datasheet:
00032  *
00033  * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf
00034  */
00035 
00036 
00037 #include "HMC5843.h "
00038 
00039 HMC5843::HMC5843(PinName sda, PinName scl) : i2c_(*(new I2C(sda, scl))), myI2c(&i2c_){
00040 
00041     //100KHz, as specified by the datasheet.
00042     i2c_.frequency(100000);
00043 
00044 
00045 }
00046 
00047 
00048 void HMC5843::write(int address, int data) {
00049    
00050     char tx[2];
00051    
00052     tx[0]=address;
00053     tx[1]=data;
00054 
00055     i2c_.write(HMC5843_I2C_WRITE,tx,2);
00056    
00057     wait_ms(100);
00058 
00059 }
00060 
00061 
00062 void HMC5843::setSleepMode() {
00063     
00064     write(HMC5843_MODE, HMC5843_SLEEP);
00065 }
00066 
00067 void HMC5843::setDefault(void) {
00068    
00069    write(HMC5843_CONFIG_A,HMC5843_10HZ_NORMAL);
00070    write(HMC5843_CONFIG_B,HMC5843_1_0GA);
00071    write(HMC5843_MODE,HMC5843_CONTINUOUS);
00072    wait_ms(100);
00073 }
00074 
00075 
00076 void HMC5843::getAddress(char *buffer) {
00077     
00078    char rx[3];
00079    char tx[1];
00080    tx[0]=HMC5843_IDENT_A;
00081     
00082        
00083     i2c_.write(HMC5843_I2C_WRITE, tx,1);
00084    
00085     wait_ms(1);
00086     
00087     i2c_.read(HMC5843_I2C_READ,rx,3);
00088     
00089     buffer[0]=rx[0];
00090     buffer[1]=rx[1];
00091     buffer[2]=rx[2];
00092 }
00093 
00094 
00095 
00096 void HMC5843::setOpMode(int mode, int ConfigA, int ConfigB) {
00097     
00098     
00099     write(HMC5843_CONFIG_A,ConfigA);
00100     write(HMC5843_CONFIG_B,ConfigB);
00101     write(HMC5843_MODE,mode);
00102     
00103 
00104 }
00105 
00106 
00107 
00108 
00109 void HMC5843::readData(int* readings) {
00110     char tx[1] = {HMC5843_X_MSB};
00111     i2c_.write(HMC5843_I2C_READ, tx, 1);
00112 
00113     char rx[6];
00114     // Burst read all registers to enhance bus speed.
00115     i2c_.read(HMC5843_I2C_READ, rx, 6);
00116 
00117     readings[0] = swapExtend(&rx[0]);
00118     readings[1] = swapExtend(&rx[2]);
00119     readings[2] = swapExtend(&rx[4]);
00120 
00121 }
00122 
00123 int HMC5843::getWord(int regi){
00124 
00125     char tx = regi;
00126     char rx[2];
00127     
00128     i2c_.write(I2C_ADDRESS, &tx, 1);
00129     
00130     i2c_.read(I2C_ADDRESS, rx, 2);
00131     
00132     return swapExtend(rx);
00133 }