Section 4: Flight-Control-Functions.

....... Configure Magnetometer Mag3110 Source Code ....

=== File-Name: 4_Configure_MAG3110_Magnetometer.ino ====


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++ Begin of functions to Configure MAG3110 KOMPASS ++++++
//              Last Update: 28-11-21
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void config_MAG3110(void)
{
 
//Put the Mag-sensor MAG3110 into the correct operating mode   

  I2C.beginTransmission(KOMPASS);  // transmit to device 0x0E
  I2C.write(0x10);                 // cntrl register1
  I2C.write(0x01);                 // send 0x01, active mode
  I2C.endTransmission();           // stop transmitting
 
  delay(10);
 
  I2C.beginTransmission(KOMPASS);  // transmit to device 0x0E
  I2C.write(0x11);                 // cntrl register2
//  0x80 => 0b10000000  
//  I2C.write(0b 10000000);   
  I2C.write(0x80);                 // send 0x80, enable auto resets
//Note: RAW (bit 5) is set to 0: Coordinate axis offset is corrected
//      by user set register values, as implemented below. 
  I2C.endTransmission();           // stop transmitting

//.............................................................
//.............................................................

//Begin Coordinate Axis Offset Setting
//Note: Left-shift by one of the mag.offset is multipy shift with 2.

// Left-shift by one as magnetometer offset registers are 15-bit only, left justified
   Mag_VSx <<= 1;  // val<<=1 means: val=val<<1
   Mag_VSy <<= 1;
   Mag_VSz <<= 1;

 writeTo(KOMPASS, 0x10, 0x00); //cntrl register-1 send 0x00=Standby mode

// Set Offset for X,Y,Z axis of the sensor
 writeTo(KOMPASS, OFF_X_LSB, (uint8_t)(Mag_VSx & 0xFF));
 writeTo(KOMPASS, OFF_X_MSB, (uint8_t)((Mag_VSx >>8) & 0xFF));
 writeTo(KOMPASS, OFF_Y_LSB, (uint8_t)(Mag_VSy & 0xFF));
 writeTo(KOMPASS, OFF_Y_MSB, (uint8_t)((Mag_VSy >>8) & 0xFF));
 writeTo(KOMPASS, OFF_Z_LSB, (uint8_t)(Mag_VSz & 0xFF));
 writeTo(KOMPASS, OFF_Z_MSB, (uint8_t)((Mag_VSz >>8) & 0xFF));

 writeTo(KOMPASS, 0x10, 0x01); //cntrl register-1 send 0x01=Active mode  

//End Coordinate Axis Offset Setting  
//.............................................................


  Serial.println("Sensor MAG3110 has been configured including Offset-Shift.");
}

//=============================================================
//++++++ End of functions to Configure MAG3110 KOMPASS ++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Free-Drones Company 2022