Подключение HMC5883L

Тема в разделе "Arduino & Shields", создана пользователем Artclonic, 3 мар 2018.

  1. Artclonic

    Artclonic Гик

    Я очень начинающии пользователь... Он у меня есть, но на англиском
     
  2. Artclonic

    Artclonic Гик

    Скажите как мне надо прописать
    эту строчку
    #define addr 0x68 // I2C адрес цифрового компаса HMC5883L
    и эту
    Wire.write(0x68);
     
  3. Artclonic

    Artclonic Гик

    Анализирую ... Код данный мне с выше указал, что адрес устройства 0x68
    Теперь мне надо указать регистр с которого начинать опрос 0x3C ???

    Или адрес останется
    0x1E
    а регистр с которого читать 0x68 ?
    НЕ РАБОТАЕТ....
     
  4. mcureenab

    mcureenab Гуру

    Говорю про HMC5883L.


    Судя по статье и даташиту адрес устройства 0x1E.

    Регистров у него 13 штук с 0 до 12.

    Чтобы получать данные нужно заслать конфигурацию. Но может быть и с дефолтными установками компас что то выдаст в регистры от 3 до 8.

    Сделай по статье. В ней же 2 скетча. Первый некий идентификатор устройства выдает и не очень полезен. Второй уже чтото измеряет. Потом меняй что не подходит.
     
    Последнее редактирование: 3 мар 2018
  5. Artclonic

    Artclonic Гик

    В том то и дело, что первыи код в статье , выдает у меня нули....
    Тогда како адрес и чего Выдал Ваш скетч?
     
  6. Sencis

    Sencis Гик

    Так что-то ту бардак 0х68 это регистр MPU 9250 он нам не нужен. Регистр 0x1E, пока тоже не нужен. Из даташита: Write Mode (02) – send 0x3C 0x02 0x00 (Continuous - measurement mode) необходимо для вывода данных значит попробуйте так если не получится то поставьте значения из и2с сканера :

    Код (C++):
    #include <Wire.h> //I2C Arduino Library
    #define Magnetometer_mX0 0x03
    #define Magnetometer_mX1 0x04
    #define Magnetometer_mZ0 0x05
    #define Magnetometer_mZ1 0x06
    #define Magnetometer_mY0 0x07
    #define Magnetometer_mY1 0x08
    int mX0, mX1, mX_out;
    int mY0, mY1, mY_out;
    int mZ0, mZ1, mZ_out;
    float heading, headingDegrees, headingFiltered, declination;
    float Xm,Ym,Zm;
    #define Magnetometer 0x3C    //I2C 7bit address of HMC5883 !!!!! тут было 0x1E  сейчас значения из датшита
    //Если после загрузки ничего нет проверьте уст-во и2с сканером и измените на полученый //адрес
    void setup(){
      //Initialize Serial and I2C communications
      Serial.begin(115200);
      Wire.begin();
      delay(100);
      Wire.beginTransmission(Magnetometer);
      Wire.write(0x02); // Select mode register
      Wire.write(0x00); // Continuous measurement mode
      Wire.endTransmission();
    }
    void loop(){
      //---- X-Axis
      Wire.beginTransmission(Magnetometer); // transmit to device
      Wire.write(Magnetometer_mX1);
      Wire.endTransmission();
      Wire.requestFrom(Magnetometer,1);
      if(Wire.available()<=1)
      {
        mX0 = Wire.read();
      }
      Wire.beginTransmission(Magnetometer); // transmit to device
      Wire.write(Magnetometer_mX0);
      Wire.endTransmission();
      Wire.requestFrom(Magnetometer,1);
      if(Wire.available()<=1)
      {
        mX1 = Wire.read();
      }
      //---- Y-Axis
      Wire.beginTransmission(Magnetometer); // transmit to device
      Wire.write(Magnetometer_mY1);
      Wire.endTransmission();
      Wire.requestFrom(Magnetometer,1);
      if(Wire.available()<=1)
      {
        mY0 = Wire.read();
      }
      Wire.beginTransmission(Magnetometer); // transmit to device
      Wire.write(Magnetometer_mY0);
      Wire.endTransmission();
      Wire.requestFrom(Magnetometer,1);
      if(Wire.available()<=1)
      {
        mY1 = Wire.read();
      }
      //---- Z-Axis
      Wire.beginTransmission(Magnetometer); // transmit to device
      Wire.write(Magnetometer_mZ1);
      Wire.endTransmission();
      Wire.requestFrom(Magnetometer,1);
      if(Wire.available()<=1)
      {
        mZ0 = Wire.read();
      }
      Wire.beginTransmission(Magnetometer); // transmit to device
      Wire.write(Magnetometer_mZ0);
      Wire.endTransmission();
      Wire.requestFrom(Magnetometer,1);
      if(Wire.available()<=1)
      {
        mZ1 = Wire.read();
      }
      //---- X-Axis
      mX1=mX1<<8;
      mX_out =mX0+mX1; // Raw data
      // From the datasheet: 0.92 mG/digit
      Xm = mX_out*0.00092; // Gauss unit
      //* Earth magnetic field ranges from 0.25 to 0.65 Gauss, so these are the values that we need to get approximately.
      //---- Y-Axis
      mY1=mY1<<8;
      mY_out =mY0+mY1;
      Ym = mY_out*0.00092;
      //---- Z-Axis
      mZ1=mZ1<<8;
      mZ_out =mZ0+mZ1;
      Zm = mZ_out*0.00092;
      // ==============================
      //Calculating Heading
      heading = atan2(Ym, Xm);
      // Correcting the heading with the declination angle depending on your location
      // You can find your declination angle at: http://www.ngdc.noaa.gov/geomag-web/
      // At my location it's 4.2 degrees => 0.073 rad
      declination = 0.073;
      heading += declination;
      // Correcting when signs are reveresed
      if(heading <0) heading += 2*PI;
      // Correcting due to the addition of the declination angle
      if(heading > 2*PI)heading -= 2*PI;
      headingDegrees = heading * 180/PI; // The heading in Degrees unit
      // Smoothing the output angle / Low pass filter
      headingFiltered = headingFiltered*0.85 + headingDegrees*0.15;
      //Sending the heading value through the Serial Port to Processing IDE
      Serial.println(headingFiltered);
      delay(50);
    }
    И2с сканер:

    Код (C++):
    //
    //    FILE: MultiSpeedI2CScanner.ino
    //  AUTHOR: Rob Tillaart
    // VERSION: 0.1.04
    // PURPOSE: I2C scanner @different speeds
    //    DATE: 2013-11-05
    //     URL: http://forum.arduino.cc/index.php?topic=197360
    //
    // Released to the public domain
    //

    #include <Wire.h>
    #include <Arduino.h>

    // scans devices from 50 to 800KHz I2C speeds.
    // lower than 50 is not possible
    // DS3231 RTC works on 800 KHz. TWBR = 2; (?)
    long speed[] = {
      50, 100, 200, 250, 400, 500, 800 };
    const int speeds = sizeof(speed)/sizeof(speed[0]);

    // DELAY BETWEEN TESTS
    #define RESTORE_LATENCY  5    // for delay between tests of found devices.
    bool delayFlag = false;

    // MINIMIZE OUTPUT
    bool printAll = true;
    bool header = true;

    // STATE MACHINE
    enum states {
      STOP, ONCE, CONT, HELP };
    states state = STOP;

    uint32_t startScan;
    uint32_t stopScan;

    void setup()
    {
      Serial.begin(115200);
      Wire.begin();
      displayHelp();


    }


    void loop()
    {
      switch (getCommand())
      {
      case 's':
        state = ONCE;
        break;
      case 'c':
        state = CONT;
        break;
      case 'd':
        delayFlag = !delayFlag;
        Serial.print(F("<delay="));
        Serial.println(delayFlag?F("5>"):F("0>"));
        break;
      case 'e':
        // eeprom test TODO
        break;
      case 'h':
        header = !header;
        Serial.print(F("<header="));
        Serial.println(header?F("yes>"):F("no>"));
        break;
      case '?':
        state = HELP;
        break;
      case 'p':
        printAll = !printAll;
        Serial.print(F("<print="));
        Serial.println(printAll?F("all>"):F("found>"));
        break;
      case 'q':
        state = HELP;
        break;
      default:
        break;
      }

      switch(state)
      {
      case ONCE:
        I2Cscan();
        state = HELP;
        break;
      case CONT:
        I2Cscan();
        delay(1000);
        break;
      case HELP:
        displayHelp();
        state = STOP;
        break;
      case STOP:
        break;
      default: // ignore all non commands
        break;
      }
    }

    char getCommand()
    {
      char c = '\0';
      if (Serial.available())
      {
        c = Serial.read();
      }
      return c;
    }

    void displayHelp()
    {
      Serial.println(F("\nArduino I2C Scanner - 0.1.03\n"));
      Serial.println(F("\ts = single scan"));
      Serial.println(F("\tc = continuous scan - 1 second delay"));
      Serial.println(F("\tq = quit continuous scan"));
      Serial.println(F("\td = toggle latency delay between successful tests."));
      Serial.println(F("\tp = toggle printAll - printFound."));
      Serial.println(F("\th = toggle header - noHeader."));
      Serial.println(F("\t? = help - this page"));
      Serial.println();
    }


    void I2Cscan()
    {
      startScan = millis();
      uint8_t count = 0;

      if (header)
      {
        Serial.print(F("TIME\tDEC\tHEX\t"));
        for (uint8_t s = 0; s < speeds; s++)
        {
          Serial.print(F("\t"));
          Serial.print(speed[s]);
        }
        Serial.println(F("\t[KHz]"));
        for (uint8_t s = 0; s < speeds + 5; s++)
        {
          Serial.print(F("--------"));
        }
        Serial.println();
      }

      // TEST
      // 0.1.04: tests only address range 8..120
      // --------------------------------------------
      // Address  R/W Bit Description
      // 0000 000   0 General call address
      // 0000 000   1 START byte
      // 0000 001   X CBUS address
      // 0000 010   X reserved - different bus format
      // 0000 011   X reserved - future purposes
      // 0000 1XX   X High Speed master code
      // 1111 1XX   X reserved - future purposes
      // 1111 0XX   X 10-bit slave addressing
      for (uint8_t address = 8; address < 120; address++)
      {
        bool printLine = printAll;
        bool found[speeds];
        bool fnd = false;

        for (uint8_t s = 0; s < speeds ; s++)
        {
          TWBR = (F_CPU/(speed[s]*1000) - 16)/2;
          Wire.beginTransmission (address);
          found[s] = (Wire.endTransmission () == 0);
          fnd |= found[s];
          // give device 5 millis
          if (fnd && delayFlag) delay(RESTORE_LATENCY);
        }

        if (fnd) count++;
        printLine |= fnd;

        if (printLine)
        {
          Serial.print(millis());
          Serial.print(F("\t"));
          Serial.print(address, DEC);
          Serial.print(F("\t0x"));
          Serial.print(address, HEX);
          Serial.print(F("\t"));

          for (uint8_t s = 0; s < speeds ; s++)
          {
            Serial.print(F("\t"));
            Serial.print(found[s]? F("V"):F("."));
          }
          Serial.println();
        }
      }

      stopScan = millis();
      if (header)
      {
        Serial.println();
        Serial.print(count);
        Serial.print(F(" devices found in "));
        Serial.print(stopScan - startScan);
        Serial.println(F(" milliseconds."));
      }
    }
     
    Последнее редактирование: 4 мар 2018
  7. Artclonic

    Artclonic Гик

    Попробую через часок, но значения со сканера у меня одно (это я уже про модуль
    HMC5883L) там со сканера выдавал адрес
    Scanning...
    I2C device found at address 0x0D !
     
  8. Sencis

    Sencis Гик

    Ну тогда попробуйте ещё раз пройтись сканером который я вам дал т.к. с него я запускал HMC 5983l а тут какой то не адекватный адрес может потому что ваш сканер не работает во всех диапазонах скоростей но с начало просто залейте скетч в верху он должен вернуть в монитор порта азимут (угол отложенный от северного магнитного полюса до вашего курса).
     
  9. Artclonic

    Artclonic Гик

    Загрузил первыи код
    в мониторе порта сначала увеличиваются числа до 229.18 потом это число не изменяется...
     
  10. Artclonic

    Artclonic Гик

    А вот загрузил второи код
    Arduino I2C Scanner - 0.1.03

    s = single scan
    c = continuous scan - 1 second delay
    q = quit continuous scan
    d = toggle latency delay between successful tests.
    p = toggle printAll - printFound.
    h = toggle header - noHeader.
    ? = help - this page
     
  11. mcureenab

    mcureenab Гуру

    Вы ссылочку давайте на код. Только вам понятно о чём речь.
     
  12. Artclonic

    Artclonic Гик

    В посте №26 два кода (код и сканер)..
    Первыи и второи это сканер...
     
  13. mcureenab

    mcureenab Гуру

    Так нажимай кнопки в мониторе. Например s.

    Только я не пойму к чему это. Адрес HMC5883L в даташите прописан 0x1E. Если монитор выдаёт что то другое, значит прибор неисправен или подключение неправильное, ненадёжное.
     
  14. Artclonic

    Artclonic Гик

    щаз понажимаю
     
  15. Artclonic

    Artclonic Гик

    Вот нажал s
    TIME DEC HEX 50 100 200 250 400 500 800 [KHz]
    ------------------------------------------------------------------------------------------------
    22479 8 0x8 . . . . . . .
    22481 9 0x9 . . . . . . .
    22483 10 0xA . . . . . . .
    22487 11 0xB . . . . . . .
    22489 12 0xC . . . . . . .
    22491 13 0xD V V V V V V V
    22494 14 0xE . . . . . . .
    22496 15 0xF . . . . . . .
    22499 16 0x10 . . . . . . .
    22501 17 0x11 . . . . . . .
    22504 18 0x12 . . . . . . .
    22506 19 0x13 . . . . . . .
    22509 20 0x14 . . . . . . .
    22511 21 0x15 . . . . . . .
    22514 22 0x16 . . . . . . .
    22516 23 0x17 . . . . . . .
    22519 24 0x18 . . . . . . .
    22521 25 0x19 . . . . . . .
    22524 26 0x1A . . . . . . .
    22526 27 0x1B . . . . . . .
    22530 28 0x1C . . . . . . .
    22532 29 0x1D . . . . . . .
    22535 30 0x1E . . . . . . .
    22537 31 0x1F . . . . . . .
    22539 32 0x20 . . . . . . .
    22542 33 0x21 . . . . . . .
    22544 34 0x22 . . . . . . .
    22547 35 0x23 . . . . . . .
    22549 36 0x24 . . . . . . .
    22552 37 0x25 . . . . . . .
    22554 38 0x26 . . . . . . .
    22557 39 0x27 . . . . . . .
    22559 40 0x28 . . . . . . .
    22562 41 0x29 . . . . . . .
    22564 42 0x2A . . . . . . .
    22567 43 0x2B . . . . . . .
    22569 44 0x2C . . . . . . .
    22573 45 0x2D . . . . . . .
    22575 46 0x2E . . . . . . .
    22578 47 0x2F . . . . . . .
    22580 48 0x30 . . . . . . .
    22583 49 0x31 . . . . . . .
    22585 50 0x32 . . . . . . .
    22588 51 0x33 . . . . . . .
    22590 52 0x34 . . . . . . .
    22593 53 0x35 . . . . . . .
    22595 54 0x36 . . . . . . .
    22598 55 0x37 . . . . . . .
    22600 56 0x38 . . . . . . .
    22603 57 0x39 . . . . . . .
    22605 58 0x3A . . . . . . .
    22608 59 0x3B . . . . . . .
    22610 60 0x3C . . . . . . .
    22614 61 0x3D . . . . . . .
    22616 62 0x3E . . . . . . .
    22619 63 0x3F . . . . . . .
    22621 64 0x40 . . . . . . .
    22624 65 0x41 . . . . . . .
    22626 66 0x42 . . . . . . .
    22629 67 0x43 . . . . . . .
    22631 68 0x44 . . . . . . .
    22634 69 0x45 . . . . . . .
    22636 70 0x46 . . . . . . .
    22639 71 0x47 . . . . . . .
    22641 72 0x48 . . . . . . .
    22644 73 0x49 . . . . . . .
    22646 74 0x4A . . . . . . .
    22649 75 0x4B . . . . . . .
    22651 76 0x4C . . . . . . .
    22654 77 0x4D . . . . . . .
    22657 78 0x4E . . . . . . .
    22660 79 0x4F . . . . . . .
    22662 80 0x50 . . . . . . .
    22665 81 0x51 . . . . . . .
    22667 82 0x52 . . . . . . .
    22669 83 0x53 . . . . . . .
    22672 84 0x54 . . . . . . .
    22674 85 0x55 . . . . . . .
    22677 86 0x56 . . . . . . .
    22679 87 0x57 . . . . . . .
    22682 88 0x58 . . . . . . .
    22684 89 0x59 . . . . . . .
    22687 90 0x5A . . . . . . .
    22689 91 0x5B . . . . . . .
    22692 92 0x5C . . . . . . .
    22694 93 0x5D . . . . . . .
    22697 94 0x5E . . . . . . .
    22700 95 0x5F . . . . . . .
    22703 96 0x60 . . . . . . .
    22705 97 0x61 . . . . . . .
    22708 98 0x62 . . . . . . .
    22710 99 0x63 . . . . . . .
    22713 100 0x64 . . . . . . .
    22715 101 0x65 . . . . . . .
    22718 102 0x66 . . . . . . .
    22721 103 0x67 . . . . . . .
    22723 104 0x68 . . . . . . .
    22726 105 0x69 . . . . . . .
    22728 106 0x6A . . . . . . .
    22731 107 0x6B . . . . . . .
    22733 108 0x6C . . . . . . .
    22736 109 0x6D . . . . . . .
    22739 110 0x6E . . . . . . .
    22742 111 0x6F . . . . . . .
    22745 112 0x70 . . . . . . .
    22747 113 0x71 . . . . . . .
    22750 114 0x72 . . . . . . .
    22752 115 0x73 . . . . . . .
    22755 116 0x74 . . . . . . .
    22758 117 0x75 . . . . . . .
    22760 118 0x76 . . . . . . .
    22763 119 0x77 . . . . . . .
     
  16. Artclonic

    Artclonic Гик

    вот c
    1 devices found in 303 milliseconds.
    TIME DEC HEX 50 100 200 250 400 500 800 [KHz]
    ------------------------------------------------------------------------------------------------
    122757 8 0x8 . . . . . . .
    122759 9 0x9 . . . . . . .
    122762 10 0xA . . . . . . .
    122764 11 0xB . . . . . . .
    122767 12 0xC . . . . . . .
    122769 13 0xD V V V V V V V
    122772 14 0xE . . . . . . .
    122774 15 0xF . . . . . . .
    122777 16 0x10 . . . . . . .
    122779 17 0x11 . . . . . . .
    122782 18 0x12 . . . . . . .
    122785 19 0x13 . . . . . . .
    122787 20 0x14 . . . . . . .
    122790 21 0x15 . . . . . . .
    122792 22 0x16 . . . . . . .
    122796 23 0x17 . . . . . . .
    122798 24 0x18 . . . . . . .
    122801 25 0x19 . . . . . . .
    122804 26 0x1A . . . . . . .
    122806 27 0x1B . . . . . . .
    122809 28 0x1C . . . . . . .
    122811 29 0x1D . . . . . . .
    122814 30 0x1E . . . . . . .
    122816 31 0x1F . . . . . . .
    122819 32 0x20 . . . . . . .
    122822 33 0x21 . . . . . . .
    122824 34 0x22 . . . . . . .
    122827 35 0x23 . . . . . . .
    122829 36 0x24 . . . . . . .
    122832 37 0x25 . . . . . . .
    122834 38 0x26 . . . . . . .
    122838 39 0x27 . . . . . . .
    122841 40 0x28 . . . . . . .
    122843 41 0x29 . . . . . . .
    122846 42 0x2A . . . . . . .
    122848 43 0x2B . . . . . . .
    122851 44 0x2C . . . . . . .
    122853 45 0x2D . . . . . . .
    122856 46 0x2E . . . . . . .
    122859 47 0x2F . . . . . . .
    122861 48 0x30 . . . . . . .
    122864 49 0x31 . . . . . . .
    122866 50 0x32 . . . . . . .
    122869 51 0x33 . . . . . . .
    122871 52 0x34 . . . . . . .
    122874 53 0x35 . . . . . . .
    122877 54 0x36 . . . . . . .
    122880 55 0x37 . . . . . . .
    122883 56 0x38 . . . . . . .
    122885 57 0x39 . . . . . . .
    122888 58 0x3A . . . . . . .
    122890 59 0x3B . . . . . . .
    122893 60 0x3C . . . . . . .
    122896 61 0x3D . . . . . . .
    122898 62 0x3E . . . . . . .
    122901 63 0x3F . . . . . . .
    122903 64 0x40 . . . . . . .
    122906 65 0x41 . . . . . . .
    122908 66 0x42 . . . . . . .
    122911 67 0x43 . . . . . . .
    122914 68 0x44 . . . . . . .
    122916 69 0x45 . . . . . . .
    122919 70 0x46 . . . . . . .
    122921 71 0x47 . . . . . . .
    122925 72 0x48 . . . . . . .
    122928 73 0x49 . . . . . . .
    122930 74 0x4A . . . . . . .
    122933 75 0x4B . . . . . . .
    122935 76 0x4C . . . . . . .
    122938 77 0x4D . . . . . . .
    122940 78 0x4E . . . . . . .
    122943 79 0x4F . . . . . . .
    122946 80 0x50 . . . . . . .
    122948 81 0x51 . . . . . . .
    122951 82 0x52 . . . . . . .
    122953 83 0x53 . . . . . . .
    122956 84 0x54 . . . . . . .
    122958 85 0x55 . . . . . . .
    122961 86 0x56 . . . . . . .
    122964 87 0x57 . . . . . . .
    122967 88 0x58 . . . . . . .
    122970 89 0x59 . . . . . . .
    122972 90 0x5A . . . . . . .
    122975 91 0x5B . . . . . . .
    122977 92 0x5C . . . . . . .
    122980 93 0x5D . . . . . . .
    122983 94 0x5E . . . . . . .
    122985 95 0x5F . . . . . . .
    122988 96 0x60 . . . . . . .
    122990 97 0x61 . . . . . . .
    122993 98 0x62 . . . . . . .
    122995 99 0x63 . . . . . . .
    122998 100 0x64 . . . . . . .
    123001 101 0x65 . . . . . . .
    123003 102 0x66 . . . . . . .
    123006 103 0x67 . . . . . . .
    123010 104 0x68 . . . . . . .
    123012 105 0x69 . . . . . . .
    123015 106 0x6A . . . . . . .
    123018 107 0x6B . . . . . . .
    123020 108 0x6C . . . . . . .
    123023 109 0x6D . . . . . . .
    123026 110 0x6E . . . . . . .
    123028 111 0x6F . . . . . . .
    123031 112 0x70 . . . . . . .
    123034 113 0x71 . . . . . . .
    123036 114 0x72 . . . . . . .
    123039 115 0x73 . . . . . . .
    123041 116 0x74 . . . . . . .
    123044 117 0x75 . . . . . . .
    123047 118 0x76 . . . . . . .
    123049 119 0x77 . . . . . . .
     
  17. Artclonic

    Artclonic Гик

    Соответственно
    <delay=5>
    <print=found>
    <header=no>
    на
    d,p,h
     
  18. Artclonic

    Artclonic Гик

    Меняю адрес в коде - и такои и такои, изменени нет.. в мониторе 229.18..
    И MPU уже подключил -результат тот же....229.18
     
  19. mcureenab

    mcureenab Гуру

    Не надо MPU подключать. С ним всё по другому. Если охота с MPU поиграть, откройте отдельную тему про MPU.
     
  20. mcureenab

    mcureenab Гуру

    Похоже модуль неправильно подключен. В этой статье друая схема подключения изображена для Arduino UNO. ( Для других моделей Arduino схема может отличаться ).

    http://privateblog.info/arduino/cifrovoj-kompas-gy-273-hmc5883l/

    На модуле такая маркировка?

    [​IMG]