void test_kompas_lsm303(void) { zobraz_text(15); // "CoMPA" Serial.println("15-sekundovy test kompasu a naklonomeru LSM303"); unsigned long konec_testu = millis() + 15000; naklon_setup(); kompas_setup(); while (Serial.available() == false and (konec_testu > millis())) { Wire.beginTransmission(I2C_ADDR_NAKLON); // assert the MSB of the address to get the accelerometer // to do slave-transmit subaddress updating. Wire.write(OUT_X_L_A | (1 << 7)); last_status = Wire.endTransmission(); Wire.requestFrom(I2C_ADDR_NAKLON, (byte)6); delay(5); byte xla = Wire.read(); byte xha = Wire.read(); byte yla = Wire.read(); byte yha = Wire.read(); byte zla = Wire.read(); byte zha = Wire.read(); int ax = (int16_t)(xha << 8 | xla); int ay = (int16_t)(yha << 8 | yla); int az = (int16_t)(zha << 8 | zla); Serial.print("zbyva: "); Serial.print((konec_testu-millis()) / 1000); Serial.print(" zrychleni: aX= "); Serial.print(ax); Serial.print(" aY= "); Serial.print(ay); Serial.print(" aZ= "); Serial.print(az); Wire.beginTransmission(I2C_ADDR_KOMPAS); // assert the MSB of the address to get the accelerometer // to do slave-transmit subaddress updating. Wire.write(DLHC_OUT_X_H_M | (1 << 7)); last_status = Wire.endTransmission(); Wire.requestFrom(I2C_ADDR_KOMPAS, (byte)6); byte xhm = Wire.read(); byte xlm = Wire.read(); byte zhm = Wire.read(); byte zlm = Wire.read(); byte yhm = Wire.read(); byte ylm = Wire.read(); int mx = (int16_t)(xhm << 8 | xlm); int my = (int16_t)(yhm << 8 | ylm); int mz = (int16_t)(zhm << 8 | zlm); Serial.print(" magnetometr: mX= "); Serial.print(mx); Serial.print(" mY= "); Serial.print(my); Serial.print(" mZ= "); Serial.println(mz); } } //---------------------------------------------- void naklon_setup(void) { naklon_write(CTRL_REG4_A, 0x08); // FS = 00 (+/- 2 g full scale); HR = 1 (high resolution enable) naklon_write(CTRL_REG1_A, 0x47); // ODR = 0100 (50 Hz ODR); LPen = 0 (normal mode); Zen = Yen = Xen = 1 (all axes enabled) } //---------------------------------------------- //---------------------------------------------- void kompas_setup(void) { magnet_write(CRA_REG_M, 0x0C); // DO = 011 (7.5 Hz ODR) magnet_write(CRB_REG_M, 0x20); // GN = 001 (+/- 1.3 gauss full scale) magnet_write(MR_REG_M , 0x00); // MD = 00 (continuous-conversion mode) } //---------------------------------------------- //---------------------------------------------- // zapis jednoho bajtu do naklonomeru void naklon_write(byte registr, byte data) { Wire.beginTransmission(I2C_ADDR_NAKLON); Wire.write(registr); Wire.write(data); last_status = Wire.endTransmission(); } //---------------------------------------------- //---------------------------------------------- // zapis jednoho bajtu do magnetometru void magnet_write(byte registr, byte data) { Wire.beginTransmission(I2C_ADDR_KOMPAS); Wire.write(registr); Wire.write(data); last_status = Wire.endTransmission(); } //----------------------------------------------