Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

I2C with Magnetometer HMC5883L

$
0
0

Hi,

I'm new with Energia but not with Arduino.

I'm not able to communicate with HMC5883L throw I2C. The board is launchpad with msp430g2553 microcontroller.

 

I read some threads about this issue but I didn't find a solution.

#include <Wire.h>
#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup()
{
  Serial.begin(9600);  // start serial for output
  Wire.begin();        // join i2c bus (address optional for master)

  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883

  //Write CRA (00) – send 0x3C 0x00 0x70 (8-average, 15 Hz default, normal measurement)
  Wire.write(0x3c); 
  Wire.write(0x00); 
  Wire.write(0x70); 

  //Write CRB (01) – send 0x3C 0x01 0xA0 (Gain=5, or any other desired gain)
  Wire.write(0x3c); 
  Wire.write(0x00); 
  Wire.write(0x70); 

  //Write Mode (02) – send 0x3C 0x02 0x00 (Continuous-measurement mode)
  Wire.write(0x3c); 
  Wire.write(0x02); 
  Wire.write(0x00); 
  delay(60);


  Wire.endTransmission();
}

void loop()
{
  int x,y,z; //triple axis data

  //Tell the HMC5883 where to begin reading data
  Wire.beginTransmission(address);
  Wire.write(0x03); //select register 3, X MSB register
  Wire.endTransmission();


  //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //X msb
    x |= Wire.read(); //X lsb
    z = Wire.read()<<8; //Z msb
    z |= Wire.read(); //Z lsb
    y = Wire.read()<<8; //Y msb
    y |= Wire.read(); //Y lsb
  }

  //Print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);

  delay(250);
}

The program hangs at this command: "Wire.endTransmission();"

 

Is there a solution?

 

Thank you 

Regards

 

ilpaso


Viewing all articles
Browse latest Browse all 2077

Trending Articles