Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <Wire.h> //I2C Arduino Library
  2.  
  3. #define address 0x1E //0011110b, I2C 7bit address of HMC5883
  4.  
  5. void setup(){
  6. //Initialize Serial and I2C communications
  7. Serial.begin(9600);
  8. Wire.begin();
  9. //Put the HMC5883 IC into the correct operating mode
  10. Wire.beginTransmission(address); //open communication with HMC5883
  11. Wire.write(0x02); //select mode register
  12. Wire.write(0x00); //continuous measurement mode
  13. Wire.endTransmission();
  14. }
  15.  
  16. void loop(){
  17.  
  18. int x,y,z; //triple axis data
  19. int xmin,xmax,ymin,ymax,zmin,zmax;
  20. xmin=0; xmax=0; ymax=0; ymin = 0; zmin=0;zmax=0;
  21. //Tell the HMC5883 where to begin reading data
  22. Wire.beginTransmission(address);
  23. Wire.write(0x03); //select register 3, X MSB register
  24. Wire.endTransmission();
  25.  
  26.  
  27. //Read data from each axis, 2 registers per axis
  28. Wire.requestFrom(address, 6);
  29. if(6<=Wire.available()){
  30. x = Wire.read()<<8; //X msb
  31. x |= Wire.read(); //X lsb
  32. z = Wire.read()<<8; //Z msb
  33. z |= Wire.read(); //Z lsb
  34. y = Wire.read()<<8; //Y msb
  35. y |= Wire.read(); //Y lsb
  36. }
  37.  
  38. //Print out values of each axis
  39. Serial.print("x: ");
  40. Serial.print(x);
  41. Serial.print(" y: ");
  42. Serial.print(y);
  43. Serial.print(" z: ");
  44. Serial.println(z);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement