2011-12-28 05:28:31 -04:00
|
|
|
/*******************************************
|
2012-08-17 03:22:38 -03:00
|
|
|
* Sample sketch that configures an HMC5883L 3 axis
|
|
|
|
* magnetometer to continuous mode and reads back
|
|
|
|
* the three axis of data.
|
|
|
|
* Code compiles to a size of 1500 bytes
|
|
|
|
* Equivalent Wire Library code compiles to 2032 bytes
|
|
|
|
*******************************************/
|
2011-12-28 05:28:31 -04:00
|
|
|
|
|
|
|
#include <I2C.h>
|
|
|
|
|
2012-08-17 03:22:38 -03:00
|
|
|
#define HMC5883L 0x1E
|
2011-12-28 05:28:31 -04:00
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
int z = 0;
|
|
|
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
2012-08-17 03:22:38 -03:00
|
|
|
I2c.begin();
|
|
|
|
I2c.write(HMC5883L,0x02,0x00); //configure device for continuous mode
|
2011-12-28 05:28:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2012-08-17 03:22:38 -03:00
|
|
|
I2c.read(HMC5883L,0x03,6); //read 6 bytes (x,y,z) from the device
|
|
|
|
x = I2c.receive() << 8;
|
|
|
|
x |= I2c.receive();
|
|
|
|
y = I2c.receive() << 8;
|
|
|
|
y |= I2c.receive();
|
|
|
|
z = I2c.receive() << 8;
|
|
|
|
z |= I2c.receive();
|
2011-12-28 05:28:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-17 03:22:38 -03:00
|
|
|
/* Wire library equivalent would be this
|
|
|
|
*
|
|
|
|
* //#include <Wire.h>
|
|
|
|
*
|
|
|
|
* #define HMC5883L 0x1E
|
|
|
|
*
|
|
|
|
* int x = 0;
|
|
|
|
* int y = 0;
|
|
|
|
* int z = 0;
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* void setup()
|
|
|
|
* {
|
|
|
|
* Wire.begin();
|
|
|
|
* Wire.beginTransmission(HMC5883L);
|
|
|
|
* Wire.send(0x02);
|
|
|
|
* Wire.send(0x00);
|
|
|
|
* Wire.endTransmission();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* void loop()
|
|
|
|
* {
|
|
|
|
* Wire.beginTransmission(HMC5883L);
|
|
|
|
* Wire.send(0x03);
|
|
|
|
* Wire.endTransmission();
|
|
|
|
* Wire.requestFrom(HMC5883L,6);
|
|
|
|
* x = Wire.receive() << 8;
|
|
|
|
* x |= Wire.receive();
|
|
|
|
* y = Wire.receive() << 8;
|
|
|
|
* y |= Wire.receive();
|
|
|
|
* z = Wire.receive() << 8;
|
|
|
|
* z |= Wire.receive();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
********************************************/
|