2018.05.16.01

Added FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883 - QMC5883 digital compass support using Mechasolution library at https://github.com/keepworking/Mecha_QMC5883L
      Modified MechaQMC5883.cpp to get rid of compiler warning about ::read
This commit is contained in:
Anthony Good 2018-05-16 19:35:50 -04:00
parent b1be2b7e6b
commit 050aa62ab9
13 changed files with 593 additions and 12 deletions

View File

@ -374,6 +374,10 @@
2018.04.21.01
Added OPTION_STEPPER_MOTOR_USE_TIMER_ONE_INSTEAD_OF_FIVE for FEATURE_STEPPER_MOTOR. Also added TimerOne library to Github.
2018.05.16.01
Added FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883 - QMC5883 digital compass support using Mechasolution library at https://github.com/keepworking/Mecha_QMC5883L
Modified MechaQMC5883.cpp to get rid of compiler warning about ::read
All library files should be placed in directories likes \sketchbook\libraries\library1\ , \sketchbook\libraries\library2\ , etc.
Anything rotator_*.* should be in the ino directory!
@ -384,7 +388,7 @@
*/
#define CODE_VERSION "2018.04.21.01"
#define CODE_VERSION "2018.05.16.01"
#include <avr/pgmspace.h>
#include <EEPROM.h>
@ -445,6 +449,10 @@
#include <DFRobot_QMC5883.h>
#endif
#if defined(FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883)
#include <MechaQMC5883.h>
#endif
#if defined(FEATURE_EL_POSITION_ADXL345_USING_ADAFRUIT_LIB) || defined(FEATURE_AZ_POSITION_ADAFRUIT_LSM303) || defined(FEATURE_EL_POSITION_ADAFRUIT_LSM303)
#include <Adafruit_Sensor.h> // required for any Adafruit sensor libraries
#endif
@ -942,9 +950,13 @@ DebugClass debug;
HMC5883L compass;
#endif //FEATURE_AZ_POSITION_HMC5883L
#if defined(FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883)
MechaQMC5883 compass;
#endif //FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883
#if defined(FEATURE_AZ_POSITION_DFROBOT_QMC5883)
DFRobot_QMC5883 compass;
#endif //FEATURE_AZ_POSITION_DFROBOT_QMC5883
#endif //FEATURE_AZ_POSITION_DFROBOT_QMC5883
#ifdef FEATURE_EL_POSITION_ADXL345_USING_LOVE_ELECTRON_LIB
ADXL345 accel;
@ -5019,6 +5031,53 @@ void read_azimuth(byte force_read){
azimuth = raw_azimuth;
#endif //FEATURE_AZ_POSITION_DFROBOT_QMC5883
#if defined(FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883)
int mecha_x, mecha_y, mecha_z, mecha_azimuth;
compass.read(&mecha_x, &mecha_y, &mecha_z, &mecha_azimuth);
#ifdef DEBUG_QMC5883
debug.print("read_azimuth: QMC5883 x:");
debug.print(mecha_x);
debug.print(" y:");
debug.print(mecha_y);
debug.print(" z:");
debug.print(mecha_z);
debug.print(" mecha_azimuth:");
debug.print(mecha_azimuth);
debug.println("");
#endif //DEBUG_QMC5883
// Correct for heading < 0deg and heading > 360deg
if (mecha_azimuth < 0){
mecha_azimuth += 360;
}
if (mecha_azimuth > 359){
mecha_azimuth -= 360;
}
raw_azimuth = mecha_azimuth;
if (AZIMUTH_SMOOTHING_FACTOR > 0) {
raw_azimuth = (raw_azimuth * (1 - (AZIMUTH_SMOOTHING_FACTOR / 100))) + (previous_raw_azimuth * (AZIMUTH_SMOOTHING_FACTOR / 100));
}
#ifdef FEATURE_AZIMUTH_CORRECTION
raw_azimuth = (correct_azimuth(raw_azimuth / (float) HEADING_MULTIPLIER) * HEADING_MULTIPLIER);
#endif // FEATURE_AZIMUTH_CORRECTION
raw_azimuth = raw_azimuth + (configuration.azimuth_offset * HEADING_MULTIPLIER);
azimuth = raw_azimuth;
#endif //FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883
#ifdef FEATURE_AZ_POSITION_ADAFRUIT_LSM303
lsm.read();
float heading = atan2(lsm.magData.y, lsm.magData.x);
@ -7392,6 +7451,11 @@ void initialize_peripherals(){
#endif //FEATURE_AZ_POSITION_DFROBOT_QMC5883
#if defined(FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883)
compass.init();
//compass.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
#endif //FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883
#ifdef FEATURE_EL_POSITION_ADXL345_USING_LOVE_ELECTRON_LIB
accel = ADXL345();
accel.SetRange(2, true);

View File

@ -46,7 +46,7 @@
#undef FEATURE_EL_PRESET_ENCODER
#endif
#if !defined(FEATURE_AZ_POSITION_POTENTIOMETER) && !defined(FEATURE_AZ_POSITION_ROTARY_ENCODER) && !defined(FEATURE_AZ_POSITION_PULSE_INPUT) && !defined(FEATURE_AZ_POSITION_GET_FROM_REMOTE_UNIT) && !defined(FEATURE_AZ_POSITION_HMC5883L) && !defined(FEATURE_AZ_POSITION_ADAFRUIT_LSM303) && !defined(FEATURE_AZ_POSITION_HH12_AS5045_SSI) &&!defined(FEATURE_AZ_POSITION_INCREMENTAL_ENCODER) &&!defined(FEATURE_AZ_POSITION_POLOLU_LSM303) &&!defined(FEATURE_AZ_POSITION_A2_ABSOLUTE_ENCODER) &&!defined(FEATURE_AZ_POSITION_ROTARY_ENCODER_USE_PJRC_LIBRARY) && !defined(FEATURE_AZ_POSITION_HMC5883L_USING_JARZEBSKI_LIBRARY) && !defined(FEATURE_AZ_POSITION_DFROBOT_QMC5883)
#if !defined(FEATURE_AZ_POSITION_POTENTIOMETER) && !defined(FEATURE_AZ_POSITION_ROTARY_ENCODER) && !defined(FEATURE_AZ_POSITION_PULSE_INPUT) && !defined(FEATURE_AZ_POSITION_GET_FROM_REMOTE_UNIT) && !defined(FEATURE_AZ_POSITION_HMC5883L) && !defined(FEATURE_AZ_POSITION_ADAFRUIT_LSM303) && !defined(FEATURE_AZ_POSITION_HH12_AS5045_SSI) &&!defined(FEATURE_AZ_POSITION_INCREMENTAL_ENCODER) &&!defined(FEATURE_AZ_POSITION_POLOLU_LSM303) &&!defined(FEATURE_AZ_POSITION_A2_ABSOLUTE_ENCODER) &&!defined(FEATURE_AZ_POSITION_ROTARY_ENCODER_USE_PJRC_LIBRARY) && !defined(FEATURE_AZ_POSITION_HMC5883L_USING_JARZEBSKI_LIBRARY) && !defined(FEATURE_AZ_POSITION_DFROBOT_QMC5883) && !defined(FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883)
#error "You must specify one AZ position sensor feature"
#endif
@ -92,7 +92,7 @@
#define FEATURE_RTC
#endif
#if defined(FEATURE_RTC_DS1307) || defined(FEATURE_RTC_PCF8583) || defined(FEATURE_I2C_LCD) || defined(FEATURE_AZ_POSITION_HMC5883L) || defined(FEATURE_EL_POSITION_ADAFRUIT_LSM303) || defined(FEATURE_EL_POSITION_ADXL345_USING_LOVE_ELECTRON_LIB) || defined(FEATURE_EL_POSITION_ADXL345_USING_ADAFRUIT_LIB) || defined(FEATURE_AZ_POSITION_HMC5883L_USING_JARZEBSKI_LIBRARY) || defined(FEATURE_AZ_POSITION_DFROBOT_QMC5883)
#if defined(FEATURE_RTC_DS1307) || defined(FEATURE_RTC_PCF8583) || defined(FEATURE_I2C_LCD) || defined(FEATURE_AZ_POSITION_HMC5883L) || defined(FEATURE_EL_POSITION_ADAFRUIT_LSM303) || defined(FEATURE_EL_POSITION_ADXL345_USING_LOVE_ELECTRON_LIB) || defined(FEATURE_EL_POSITION_ADXL345_USING_ADAFRUIT_LIB) || defined(FEATURE_AZ_POSITION_HMC5883L_USING_JARZEBSKI_LIBRARY) || defined(FEATURE_AZ_POSITION_DFROBOT_QMC5883) || defined(FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883)
#define FEATURE_WIRE_SUPPORT
#endif

View File

@ -51,6 +51,8 @@
// #define FEATURE_AZ_POSITION_HH12_AS5045_SSI
// #define FEATURE_AZ_POSITION_INCREMENTAL_ENCODER
// #define FEATURE_AZ_POSITION_A2_ABSOLUTE_ENCODER
// #define FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883 // QMC5883 digital compass support using Mechasolution library at https://github.com/keepworking/Mecha_QMC5883L
// #define FEATURE_EL_POSITION_POTENTIOMETER
// #define FEATURE_EL_POSITION_ROTARY_ENCODER

View File

@ -50,6 +50,8 @@
//#define FEATURE_AZ_POSITION_POLOLU_LSM303 // Uncomment for azimuth using LSM303 compass and Polulu library
//#define FEATURE_AZ_POSITION_HH12_AS5045_SSI
//#define FEATURE_AZ_POSITION_INCREMENTAL_ENCODER
//#define FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883 // QMC5883 digital compass support using Mechasolution library at https://github.com/keepworking/Mecha_QMC5883L
#define FEATURE_EL_POSITION_POTENTIOMETER
//#define FEATURE_EL_POSITION_ROTARY_ENCODER

View File

@ -12,18 +12,18 @@
*/
#define FEATURE_ELEVATION_CONTROL // uncomment this for AZ/EL rotators
// #define FEATURE_ELEVATION_CONTROL // uncomment this for AZ/EL rotators
#define FEATURE_YAESU_EMULATION // uncomment this for Yaesu GS-232 emulation on control port
// #define FEATURE_EASYCOM_EMULATION // Easycom protocol emulation on control port (undefine FEATURE_YAESU_EMULATION above)
#define FEATURE_MOON_TRACKING
#define FEATURE_SUN_TRACKING
#define FEATURE_CLOCK
#define FEATURE_GPS
#define FEATURE_RTC_DS1307
// #define FEATURE_MOON_TRACKING
// #define FEATURE_SUN_TRACKING
// #define FEATURE_CLOCK
// #define FEATURE_GPS
// #define FEATURE_RTC_DS1307
// #define FEATURE_RTC_PCF8583
// #define FEATURE_ETHERNET
#define FEATURE_STEPPER_MOTOR // Requires TimerFive library to be copied to the Arduino libraries directory (If using OPTION_STEPPER_MOTOR_USE_TIMER_ONE_INSTEAD_OF_FIVE below, copy the TimeOne library)
// #define FEATURE_STEPPER_MOTOR // Requires TimerFive library to be copied to the Arduino libraries directory (If using OPTION_STEPPER_MOTOR_USE_TIMER_ONE_INSTEAD_OF_FIVE below, copy the TimeOne library)
// #define FEATURE_AUTOCORRECT
// #define FEATURE_TEST_DISPLAY_AT_STARTUP
@ -43,7 +43,7 @@
/* position sensors - pick one for azimuth and one for elevation if using an az/el rotator */
#define FEATURE_AZ_POSITION_POTENTIOMETER //this is used for both a voltage from a rotator control or a homebrew rotator with a potentiometer
#define FEATURE_AZ_POSITION_POTENTIOMETER //this is used for both a voltage from a rotator control or a homebrew rotator with a potentiometer
// #define FEATURE_AZ_POSITION_ROTARY_ENCODER
// #define FEATURE_AZ_POSITION_ROTARY_ENCODER_USE_PJRC_LIBRARY // library @ http://www.pjrc.com/teensy/td_libs_Encoder.html
// #define FEATURE_AZ_POSITION_PULSE_INPUT
@ -56,6 +56,8 @@
// #define FEATURE_AZ_POSITION_HH12_AS5045_SSI
// #define FEATURE_AZ_POSITION_INCREMENTAL_ENCODER
// #define FEATURE_AZ_POSITION_A2_ABSOLUTE_ENCODER
// #define FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883 // QMC5883 digital compass support using Mechasolution library at https://github.com/keepworking/Mecha_QMC5883L
#define FEATURE_EL_POSITION_POTENTIOMETER
// #define FEATURE_EL_POSITION_ROTARY_ENCODER

View File

@ -51,6 +51,8 @@
//#define FEATURE_AZ_POSITION_POLOLU_LSM303 // Uncomment for azimuth using LSM303 compass and Polulu library
//#define FEATURE_AZ_POSITION_HH12_AS5045_SSI
#define FEATURE_AZ_POSITION_INCREMENTAL_ENCODER
//#define FEATURE_AZ_POSITION_MECHASOLUTION_QMC5883 // QMC5883 digital compass support using Mechasolution library at https://github.com/keepworking/Mecha_QMC5883L
//#define FEATURE_EL_POSITION_POTENTIOMETER
//#define FEATURE_EL_POSITION_ROTARY_ENCODER

View File

@ -0,0 +1,65 @@
#include "MechaQMC5883.h"
#include <Wire.h>
void MechaQMC5883::setAddress(uint8_t addr){
address = addr;
}
void MechaQMC5883::WriteReg(byte Reg,byte val){
Wire.beginTransmission(address); //start talking
Wire.write(Reg); // Tell the HMC5883 to Continuously Measure
Wire.write(val); // Set the Register
Wire.endTransmission();
}
void MechaQMC5883::init(){
WriteReg(0x0B,0x01);
//Define Set/Reset period
setMode(Mode_Continuous,ODR_200Hz,RNG_8G,OSR_512);
/*
Define
OSR = 512
Full Scale Range = 8G(Gauss)
ODR = 200HZ
set continuous measurement mode
*/
}
void MechaQMC5883::setMode(uint16_t mode,uint16_t odr,uint16_t rng,uint16_t osr){
WriteReg(0x09,mode|odr|rng|osr);
}
void MechaQMC5883::softReset(){
WriteReg(0x0A,0x80);
}
void MechaQMC5883::read(uint16_t* x,uint16_t* y,uint16_t* z){
Wire.beginTransmission(address);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom((int) address, (int) 6); // Modified by Goody 2018-05-16
*x = Wire.read(); //LSB x
*x |= Wire.read() << 8; //MSB x
*y = Wire.read(); //LSB z
*y |= Wire.read() << 8; //MSB z
*z = Wire.read(); //LSB y
*z |= Wire.read() << 8; //MSB y
}
void MechaQMC5883::read(uint16_t* x,uint16_t* y,uint16_t* z,int* a){
read(x,y,z);
*a = azimuth(y,x);
}
void MechaQMC5883::read(uint16_t* x,uint16_t* y,uint16_t* z,float* a){
read(x,y,z);
*a = azimuth(y,x);
}
float MechaQMC5883::azimuth(uint16_t *a, uint16_t *b){
float azimuth = atan2((int)*a,(int)*b) * 180.0/PI;
return azimuth < 0?360 + azimuth:azimuth;
}

View File

@ -0,0 +1,61 @@
#ifndef Mecha_QMC5883
#define Mecha_QMC5883
#include "Arduino.h"
#include "Wire.h"
#define QMC5883_ADDR 0x0D
//REG CONTROL
//0x09
#define Mode_Standby 0b00000000
#define Mode_Continuous 0b00000001
#define ODR_10Hz 0b00000000
#define ODR_50Hz 0b00000100
#define ODR_100Hz 0b00001000
#define ODR_200Hz 0b00001100
#define RNG_2G 0b00000000
#define RNG_8G 0b00010000
#define OSR_512 0b00000000
#define OSR_256 0b01000000
#define OSR_128 0b10000000
#define OSR_64 0b11000000
class MechaQMC5883{
public:
void setAddress(uint8_t addr);
void init(); //init qmc5883
void setMode(uint16_t mode,uint16_t odr,uint16_t rng,uint16_t osr); // setting
void softReset(); //soft RESET
void read(uint16_t* x,uint16_t* y,uint16_t* z); //reading
void read(uint16_t* x,uint16_t* y,uint16_t* z,int* a);
void read(uint16_t* x,uint16_t* y,uint16_t* z,float* a);
float azimuth(uint16_t* a,uint16_t* b);
private:
void WriteReg(uint8_t Reg,uint8_t val);
uint8_t address = QMC5883_ADDR;
};
#endif

View File

@ -0,0 +1,138 @@
# Mechasolution QMC5883L Library
[한글 설명 바로가기](https://github.com/keepworking/Mecha_QMC5883/blob/master/README_KO.md)
## Arduino Code
There are a few simple rules for using that library. Please read the following summary and apply it to your project
### Basic Elements
Required header files (#include ...) and Setup side code.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
void setup(){
Wire.begin();
}
```
### Object Declaration
The object declaration method. It is used outside the setup statement, and a name such as qmc can be changed to any other name you want.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
```
### initialization
QMC5883 Sensor's setting function.
The init function allows you to take advantage of the features of the QMC5883 sensor by default.
```cpp
void setup(){
Wire.begin();
qmc.init();
}
```
If you want more detailed settings, you can use it as follows.
```cpp
void setup(){
Wire.begin();
qmc.init();
qmc.setMode(Mode_Standby,ODR_200Hz,RNG_8G,OSR_512);
}
```
The values used for setMode can take the following values:
```
Mode : Mode_Standby / Mode_Continuous
ODR : ODR_10Hz / ODR_50Hz / ODR_100Hz / ODR_200Hz
ouput data update rate
RNG : RNG_2G / RNG_8G
magneticfield measurement range
OSR : OSR_512 / OSR_256 / OSR_128 / OSR_64
over sampling rate
```
### Read values
How to read the measured sensor value is as follows.
```cpp
void loop(){
int x,y,z;
qmc.read(&x,&y,&z);
}
```
and we can get azimuth too.
```cpp
void loop(){
int x,y,z;
int a;
//float a; //can get float value
qmc.read(&x,&y,&z,&a);
}
```
also can claculate azimuth you want
```cpp
void loop(){
int x,y,z;
int a;
qmc.read(&x,&y,&z);
a = qmc.azimuth(&y,&x);
}
```
## Basic example
It can be seen as a collection of the contents mentioned above.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
delay(100);
}
```

View File

@ -0,0 +1,138 @@
# Mechasolution QMC5883 Library
HMC5883 지자기 나침반 센서의 수명 만료(EOL)로인해 그동안의 HMC5883 센서는 생산이 중단 되었고, 대체 상품인 QMC5883으로 변경이 되었습니다.
## Arduino Code
해당 라이브러리를 이용하기 위한 몇가지 간단한 규칙이 있습니다. 아래 정리된 내용을 읽어주시고 사용하시는 프로젝트에 적용해 주세요
### 기본 요소
필수적으로 필요한 헤더파일(#include...)과 Setup 쪽 코드 입니다.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
void setup(){
Wire.begin();
}
```
### 객체 선언
객체 선언 방식입니다. setup문 밖에서 사용이 되며 qmc와 같은 이름은 사용자가 원하는 다른 이름으로 변경이 가능합니다.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
```
### 사용 설정
QMC5883 센서의 설정 함수 입니다.
init 함수를 사용하면 기본 설정으로 QMC5883센서의 기능을 이용할 수 있습니다.
```cpp
void setup(){
Wire.begin();
qmc.init();
}
```
좀더 세세한 설정을 원한다면 다음과 같이 사용이 가능합니다.
```cpp
void setup(){
Wire.begin();
qmc.init();
qmc.setMode(Mode_Standby,ODR_200Hz,RNG_8G,OSR_512);
}
```
setMode에 사용되는 값들은 다음 값들을 이용할 수 있습니다.
```
Mode : Mode_Standby / Mode_Continuous
ODR : ODR_10Hz / ODR_50Hz / ODR_100Hz / ODR_200Hz
ouput data update rate
RNG : RNG_2G / RNG_8G
magneticfield measurement range
OSR : OSR_512 / OSR_256 / OSR_128 / OSR_64
over sampling rate
```
### 값 읽기
측정한 센서의 값을 읽는 법은 다음과 같습니다.
```cpp
void loop(){
int x,y,z;
qmc.read(&x,&y,&z);
}
```
방위각에 대한 값입니다.
```cpp
void loop(){
int x,y,z;
int a;
//float a; //float 형도 지원됩니다.
qmc.read(&x,&y,&z,&a);
}
```
별도로 원하는 방위각도 구할 수 있습니다.
```cpp
void loop(){
int x,y,z;
int a;
qmc.read(&x,&y,&z);
a = qmc.azimuth(&y,&x);
}
```
### 기본 예제
다음은 라이브러리 기본 예제인 raw입니다.
위에 소개된 내용의 총집합으로 볼 수 있습니다.
```cpp
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
delay(100);
}
```

View File

@ -0,0 +1,29 @@
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x, y, z;
int azimuth;
//float azimuth; //is supporting float too
qmc.read(&x, &y, &z,&azimuth);
//azimuth = qmc.azimuth(&y,&x);//you can get custom azimuth
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.print(" a: ");
Serial.print(azimuth);
Serial.println();
delay(100);
}

View File

@ -0,0 +1,25 @@
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.println();
delay(100);
}

View File

@ -0,0 +1,53 @@
#include <Wire.h> //I2C Arduino Library
#define addr 0x0D //I2C Address for The HMC5883
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(addr); //start talking
Wire.write(0x0B); // Tell the HMC5883 to Continuously Measure
Wire.write(0x01); // Set the Register
Wire.endTransmission();
Wire.beginTransmission(addr); //start talking
Wire.write(0x09); // Tell the HMC5883 to Continuously Measure
Wire.write(0x1D); // Set the Register
Wire.endTransmission();
}
void loop() {
int x, y, z; //triple axis data
//Tell the HMC what regist to begin writing data into
Wire.beginTransmission(addr);
Wire.write(0x00); //start with register 3.
Wire.endTransmission();
//Read the data.. 2 bytes for each axis.. 6 total bytes
Wire.requestFrom(addr, 6);
if (6 <= Wire.available()) {
x = Wire.read(); //MSB x
x |= Wire.read() << 8; //LSB x
z = Wire.read(); //MSB z
z |= Wire.read() << 8; //LSB z
y = Wire.read(); //MSB y
y |= Wire.read() << 8; //LSB y
}
// Show Values
Serial.print("X Value: ");
Serial.println(x);
Serial.print("Y Value: ");
Serial.println(y);
Serial.print("Z Value: ");
Serial.println(z);
Serial.println();
delay(500);
}