/branches/V0.82b-Arthur-P/HMC58X3/examples/HMC58X3_basic/HMC58X3_basic.pde |
---|
0,0 → 1,75 |
/* |
HMC5843_basic.pde - Basic reading example for the HMC5843 library |
Copyright (C) 2011 Fabio Varesano <fabio at varesano dot net> |
This program is free software: you can redistribute it and/or modify |
it under the terms of the version 3 GNU General Public License as |
published by the Free Software Foundation. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
You should have received a copy of the GNU General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
*/ |
#include <Wire.h> |
#include <HMC58X3.h> |
HMC58X3 magn; |
void setup(void) { |
Serial.begin(9600); |
Wire.begin(); |
// no delay needed as we have already a delay(5) in HMC5843::init() |
magn.init(false); // Dont set mode yet, we'll do that later on. |
// Calibrate HMC using self test, not recommended to change the gain after calibration. |
magn.calibrate(1); // Use gain 1=default, valid 0-7, 7 not recommended. |
// Single mode conversion was used in calibration, now set continuous mode |
magn.setMode(0); |
} |
void loop() { |
int ix,iy,iz; |
float fx,fy,fz; |
delay(10); |
// Get values, as ints and floats. |
magn.getValues(&ix,&iy,&iz); |
magn.getValues(&fx,&fy,&fz); |
// also available HMC5843::getValues(float *xyz) you can pass to it an array of 3 floats |
// Print int values |
Serial.print("Ints x:"); |
Serial.print(ix); |
Serial.print(","); |
Serial.print(iy); |
Serial.print(","); |
Serial.print(iz); |
Serial.print(","); |
// Print float values |
Serial.print(" Floats x:"); |
Serial.print(fx); |
Serial.print(" y:"); |
Serial.print(fy); |
Serial.print(" z:"); |
Serial.print(fz); |
// a simple heading, assuming it's close to horizontal. See: |
// M.J. Caruso. Applications of magnetic sensors for low cost compass systems. |
// In Position Location and Navigation Symposium, IEEE, 2000. Available from: |
// http://hnc.ru/lib/a%26c%20%28automatic%20%26%20controls%29/sensors/DataSheet/Magnit/Honeywell/lowcost.pdf |
Serial.print(" Heading: "); |
float heading = atan2(fy, fx); |
if(heading < 0) { |
heading += 2 * M_PI; |
} |
Serial.println(heading * 180/M_PI); |
// x and y axis are swapped above with respect to the above paper as our Z axis points to the sky while in the paper it points to the bottom |
} |
/branches/V0.82b-Arthur-P/HMC58X3/examples/HMC58X3_raw/HMC58X3_raw.pde |
---|
0,0 → 1,47 |
/* |
HMC58X3_basic.pde - Basic reading example for the HMC58X3 library |
Copyright (C) 2011 Fabio Varesano <fabio at varesano dot net> |
This program is free software: you can redistribute it and/or modify |
it under the terms of the version 3 GNU General Public License as |
published by the Free Software Foundation. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
You should have received a copy of the GNU General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
*/ |
// Uncomment the following line if you are using the HMC5843 |
#include <Wire.h> |
#include <HMC58X3.h> |
HMC58X3 magn; |
void setup(void) { |
Serial.begin(9600); |
Wire.begin(); |
// no delay needed as we have already a delay(5) in HMC58X3::init() |
magn.init(true); // Dont set mode yet, we'll do that later on. |
} |
void loop() { |
int ix,iy,iz; |
delay(10); |
// Get values, as ints and floats. |
magn.getRaw(&ix,&iy,&iz); |
Serial.print(ix); |
Serial.print(","); |
Serial.print(iy); |
Serial.print(","); |
Serial.print(iz); |
Serial.println(","); |
} |
/branches/V0.82b-Arthur-P/HMC58X3/examples/HMC58X3_registers_scan/HMC58X3_registers_scan.pde |
---|
0,0 → 1,80 |
/*HMC5883LRegisterScanner.pde |
**A sketch that attempts to read every register from a slave device |
**Written by Wayne Truchsess http://dsscircuits.com |
*/ |
#include "Wire.h" |
#define I2C 0x1E |
byte x; |
void setup() { |
Wire.begin(); |
Serial.begin(9600); |
delay(1000); |
Wire.beginTransmission(I2C); // start transmission to device |
Wire.send(0); // send register address |
Wire.send(0x70); // send value to write |
Wire.endTransmission(); // end transmission |
delay(10); |
Wire.beginTransmission(I2C); // start transmission to device |
Wire.send(1); // send register address |
Wire.send(B11000000); // send value to write |
Wire.endTransmission(); // end transmission |
delay(10); |
Wire.beginTransmission(I2C); // start transmission to device |
Wire.send(2); // send register address |
Wire.send(0x00); // send value to write |
Wire.endTransmission(); // end transmission |
delay(100); |
baselineRegisters(); |
} |
void loop() { |
int x,y,z; |
Wire.beginTransmission(I2C); |
Wire.send(3); // will start from DATA X MSB and fetch all the others |
Wire.endTransmission(); |
Wire.beginTransmission(I2C); |
Wire.requestFrom(I2C, 6); |
if(6 == Wire.available()) { |
// read out the 3 values, 2 bytes each. |
x = (Wire.receive() << 8) + Wire.receive(); |
y = (Wire.receive() << 8) + Wire.receive(); |
z = (Wire.receive() << 8) + Wire.receive(); |
// the HMC5843 will automatically wrap around on the next request |
} |
Wire.endTransmission(); |
Serial.print(x); |
Serial.print(" "); |
Serial.print(y); |
Serial.print(" "); |
Serial.println(z); |
delay(100); |
return; |
} |
void baselineRegisters() { |
for(int l = 0x00; l < 0x12; l++){ |
Wire.beginTransmission(I2C); |
Wire.send(l); |
Wire.endTransmission(); |
//delay(100); |
Wire.beginTransmission(I2C); |
Wire.requestFrom(I2C,1); |
x = Wire.receive(); |
Serial.print("Register Address "); |
Serial.print(l,DEC); |
Serial.print(" = "); |
Serial.print(x,BIN); |
Serial.print(" = "); |
Serial.print(x,DEC); |
Serial.println(" "); |
Wire.endTransmission(); |
} |
} |