Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. //Interface a HMC5883L 3-axis digital compass to an Arduino Uno and display
  2. //data on 20x4 LCD
  3.  
  4. #include <Wire.h>
  5. #include <LiquidCrystal.h>
  6.  
  7. //Address map for registers
  8. #define configA 0x00
  9. #define configB 0x01
  10. #define mode 0x02
  11. #define dataOutX_U 0x03
  12. #define dataOutX_L 0x04
  13. #define dataOutZ_L 0x05
  14. #define dataOutZ_L 0x06
  15. #define dataOutY_L 0x07
  16. #define dataOutY_L 0x08
  17. #define statusReg 0x09
  18.  
  19. //Operating modes sent to Mode register (0x02)
  20. #define continuous 0x00
  21. #define single 0x01
  22. #define idle 0x02
  23.  
  24. #define i2c_addr 0x1E
  25. #define gain 1090
  26.  
  27. const int RS = 2, EN = 3, D4 = 4, D5 = 5, D6 = 6, D7 = 7;
  28. LiquidCrystal lcd(RS,EN,D4,D5,D6,D7); //set Uno pins that are connected to LCD, 4-bit mode
  29.  
  30. int16_t x = 0;
  31. int16_t y = 0;
  32. int16_t z = 0;
  33. float heading;
  34. float gaussX;
  35. float gaussY;
  36. float gaussZ;
  37.  
  38. void setup() {
  39. Wire.begin();
  40. lcd.begin(20,4); //set 20 columns and 4 rows of 20x4 LCD
  41. setOperatingMode(continuous);
  42. setSamples();
  43. }
  44.  
  45. void loop() {
  46. getXYZ();
  47. convert(x,y,z);
  48. getHeading(gaussX,gaussY,gaussZ);
  49. writeLCD();
  50. delay(500);
  51. }
  52.  
  53. void writeLCD(void){
  54. lcd.clear();
  55. lcd.print("X: ");
  56. lcd.print(gaussX);
  57. lcd.setCursor(0,1);
  58. lcd.print("Y: ");
  59. lcd.print(gaussY);
  60. lcd.setCursor(0,2);
  61. lcd.print("Z: ");
  62. lcd.print(gaussZ);
  63. lcd.setCursor(0,3);
  64. lcd.print("Heading: ");
  65. lcd.print(heading);
  66. }
  67.  
  68.  
  69. //Convert the raw X, Y, Z counts to Gauss
  70. void convert(int16_t rawX, int16_t rawY, int16_t rawZ){
  71. gaussX = (float)rawX/gain;
  72. gaussY = (float)rawY/gain;
  73. gaussZ = (float)rawZ/gain;
  74. }
  75.  
  76.  
  77. //accounts for declination (error in magnetic field which is dependent on location)
  78. void getHeading(float X, float Y, float Z){
  79. heading = (atan2(Y,X) - 0.1) * 180 / PI;
  80. if (heading < 0) heading += 360;
  81. if (heading > 360) heading -= 360;
  82. }
  83.  
  84. void setSamples(void){
  85. Wire.beginTransmission(i2c_addr);
  86. Wire.write(configA); //write to config A register
  87. Wire.write(0x70); //8 samples averaged, 15Hz output rate, normal measurement
  88. Wire.endTransmission();
  89. delay(10);
  90. }
  91.  
  92. void setOperatingMode(uint8_t addr){
  93. Wire.beginTransmission(i2c_addr);
  94. Wire.write(mode); //write to mode register
  95. Wire.write(addr); //set measurement mode
  96. Wire.endTransmission();
  97. delay(10);
  98. }
  99.  
  100. //get the raw counts of X, Y, Z from registers 0x03 to 0x08
  101. void getXYZ(void){
  102. Wire.beginTransmission(i2c_addr);
  103. Wire.write(0x03);
  104. Wire.endTransmission();
  105.  
  106. Wire.requestFrom(i2c_addr, 6);
  107. if (Wire.available() >= 6){
  108. int16_t temp = Wire.read(); //read upper byte of X
  109. x = temp << 8;
  110. temp = Wire.read(); //read lower byte of X
  111. x = x | temp;
  112.  
  113. temp = Wire.read(); //read upper byte of Z
  114. z = temp << 8;
  115. temp = Wire.read(); //read lower byte of Z
  116. z = z | temp;
  117.  
  118. temp = Wire.read(); //read upper byte of Y
  119. y = temp << 8;
  120. temp = Wire.read(); //read lower byte of Y
  121. y = y | temp;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement