Dependencies:   mbed

HMC5883L.cpp

Committer:
Cointepas
Date:
2017-05-18
Revision:
0:8d7791a3b914

File content as of revision 0:8d7791a3b914:

#include "mbed.h"
#include <Fonction.h>

Serial pc(USBTX, USBRX);
I2C hmc(p9, p10); //je déclare mon maitre connécté à des pins spécifique SCL et SDA
const int addr = 0x3C; // adresse d'écriture de ma sonde
char config_hmc[2];
char readhmc[6];
float Bx, By, Bz;

using namespace std;


void HMC5883L() 
{
   int i = 1;
   int a = 0; 
    config_hmc[0]=0x00;
    config_hmc[1]=0x70;
    hmc.write(addr, config_hmc,2);
    
    config_hmc[0]=0x01;
    config_hmc[1]=0xA0;
    hmc.write(addr, config_hmc,2);
    
    config_hmc[0]=0x02;
    config_hmc[1]=0x01;
    hmc.write(addr, config_hmc,2);
    
    wait(0.006);
    do{
       // while(ack!=0){
        hmc.read(addr, readhmc, 6);
        
        /*readhmc[0]=0x03;
        hmc.write(addr, readhmc, 1);*/
      
            Bx=((readhmc[0] << 8)|readhmc[1]);
            By=((readhmc[2] << 8)|readhmc[3]);
            Bz=((readhmc[4] << 8)|readhmc[5]);
            
            pc.printf("\nBx=%f, By=%f, Bz=%f\n",Bx,By,Bz);
            hmc.stop();
            wait(0.7);
            a++;
            i = 0;
            wait(5);}while(a < 1);
            
           // }
}