Ce site contient essentiellement des notes de travail. Le contenu est en constante évolution, et loin d'être achevé. (+ d'infos)
La plupart des documentations informatiques sont orientées Debian / Ubuntu.

Electronique/Hardware/Sensors/HMC5883/Arduino

De Ordinoscope.net
Sauter à la navigation Sauter à la recherche

Code

Version allégée de l'exemple de Seeedstudio

/*
3-Axis Compass Module (HMC5883) Demo code.
2010 Copyright (c) Seeed Technology Inc.  All right reserved.
... 
For more details about the product please check http://www.seeedstudio.com/depot/
*/
 
#include <Wire.h>
#include <math.h>
 
#define HMC5883_WriteAddress 0x1E //  i.e 0x3C >> 1
#define HMC5883_ModeRegisterAddress 0x02
#define HMC5883_ContinuousModeCommand 0x00
#define HMC5883_DataOutputXMSBAddress  0x03
 
int regb=0x01;
int regbdata=0x40;
int outputData[6];
 
void setup() {  
    Serial.begin(115200);
    Serial.println ("setup");
    Wire.begin();
}
 
void loop() {
 
    int i,x,y,z;
    double angle;
 
    Wire.beginTransmission(HMC5883_WriteAddress);
    Wire.send(regb);
    Wire.send(regbdata);
    Wire.endTransmission();
 
    delay(1000);
    Wire.beginTransmission(HMC5883_WriteAddress); //Initiate a transmission with HMC5883 (Write address).
    Wire.send(HMC5883_ModeRegisterAddress);       //Place the Mode Register Address in send-buffer.
    Wire.send(HMC5883_ContinuousModeCommand);     //Place the command for Continuous operation Mode in send-buffer.
    Wire.endTransmission();                       //Send the send-buffer to HMC5883 and end the I2C transmission.
    delay(100);
 
 
    Wire.beginTransmission(HMC5883_WriteAddress);  //Initiate a transmission with HMC5883 (Write address).
    Wire.requestFrom(HMC5883_WriteAddress,6);      //Request 6 bytes of data from the address specified.
 
    delay(500);
 
 
    if(6 <= Wire.available()) {
        for(i=0;i<6;i++) {
            outputData[i]=Wire.receive();  //Store the data in outputData buffer
        }
    }
 
    x=outputData[0] << 8 | outputData[1]; //Combine MSB and LSB of X Data output register
    z=outputData[2] << 8 | outputData[3]; //Combine MSB and LSB of Z Data output register
    y=outputData[4] << 8 | outputData[5]; //Combine MSB and LSB of Y Data output register
 
 
    angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // angle in degrees
 
    Serial.print(angle,2);
    Serial.println(" Deg");
    delay(100);
}

Références