/*MAG3110 Breakout Example Codeby: Aaron Weiss, aaron at sparkfun dot comSparkFun Electronics 2011date: 9/6/11license: beerware, if you use this code and happen to meet me, youcan by me a beerThe code reads the raw 16-bit x, y, and z values and prints themout. This sketch does not use the INT1 pin, nor does it poll fornew data.*/#include <Wire.h>#define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't changevoid setup(){Wire.begin(); // join i2c bus (address optional for master)Serial.begin(9600); // start serial for outputconfig(); // turn the MAG3110 on}void loop(){print_values();delay(5);}void config(void){Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x11); // cntrl register2Wire.send(0x80); // send 0x80, enable auto resetsWire.endTransmission(); // stop transmittingdelay(15);Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x10); // cntrl register1Wire.send(1); // send 0x01, active modeWire.endTransmission(); // stop transmitting}void print_values(void){Serial.print(readx());Serial.print(" ");Serial.print(ready());Serial.print(" ");Serial.println(readz());}int readx(void){int xl, xh; //define the MSB and LSBWire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x01); // x MSB regWire.endTransmission(); // stop transmittingdelayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.requestFrom(MAG_ADDR, 1); // request 1 bytewhile(Wire.available()) // slave may send less than requested{xh = Wire.receive(); // receive the byte}delayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x02); // x LSB regWire.endTransmission(); // stop transmittingdelayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.requestFrom(MAG_ADDR, 1); // request 1 bytewhile(Wire.available()) // slave may send less than requested{xl = Wire.receive(); // receive the byte}int xout = (xl|(xh << 8)); //concatenate the MSB and LSBreturn xout;}int ready(void){int yl, yh; //define the MSB and LSBWire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x03); // y MSB regWire.endTransmission(); // stop transmittingdelayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.requestFrom(MAG_ADDR, 1); // request 1 bytewhile(Wire.available()) // slave may send less than requested{yh = Wire.receive(); // receive the byte}delayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x04); // y LSB regWire.endTransmission(); // stop transmittingdelayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.requestFrom(MAG_ADDR, 1); // request 1 bytewhile(Wire.available()) // slave may send less than requested{yl = Wire.receive(); // receive the byte}int yout = (yl|(yh << 8)); //concatenate the MSB and LSBreturn yout;}int readz(void){int zl, zh; //define the MSB and LSBWire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x05); // z MSB regWire.endTransmission(); // stop transmittingdelayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.requestFrom(MAG_ADDR, 1); // request 1 bytewhile(Wire.available()) // slave may send less than requested{zh = Wire.receive(); // receive the byte}delayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.beginTransmission(MAG_ADDR); // transmit to device 0x0EWire.send(0x06); // z LSB regWire.endTransmission(); // stop transmittingdelayMicroseconds(2); //needs at least 1.3us free time between start and stopWire.requestFrom(MAG_ADDR, 1); // request 1 bytewhile(Wire.available()) // slave may send less than requested{zl = Wire.receive(); // receive the byte}int zout = (zl|(zh << 8)); //concatenate the MSB and LSBreturn zout;}