Wenn du diesen QMC5883L 'A' version hast (DA 5883), dann ist es noetig die Linie zu entfernen, wo das "Data Ready" bit im Statusregister getestet ist und anstatt ein millis() timer verwenden:
timeMillis = millis(); // Update the current time
if (timeMillis - sampleTime >= QMC5833L_CONVERSION_TIME) // Read the QMC5883L's data every 14 milliseconds
{
// Read the QMC5883L data registers here...
sampleTime = timeMillis; // Update the sample time
}
Hier ist ein Beispiel Sketch, dass mit dem QMC5883L version 'A' funktioniert:
#include <Wire.h>
#define QMC5833L_ADDR 0x1E // QMC5883L version A I2C address
#define QMC5883L_CTRLA_REGISTER 0x00 // QMC5883L control A register sub-address
#define QMC5883L_CTRLB_REGISTER 0x01 // QMC5883L control B register sub-address
#define QMC5883L_MODE_REGISTER 0x02 // QMC5883L mode register sub-address
#define QMC5883L_DATA_X_MSB_REGISTER 0x03 // QMC5883L data output X axis MSB sub-address
#define QMC5833L_CONVERSION_TIME 14 // QMC5883L conversion time in milliseconds
int16_t x, y, z; // QMC5883L data variables
uint32_t timeMillis, sampleTime; // Sample time variables
void setup() {
Serial.begin(115200); // Open serial communications at 115200 bps
Wire.begin(); // Start the wire library
Wire.setClock(400000); // Set the I2C to fast mode at 400kHz
// Put the QMC5833L IC into the correct operating mode:
Wire.beginTransmission(QMC5833L_ADDR); // Select the QMC5883L version A device
Wire.write(QMC5883L_MODE_REGISTER); // Select the mode register
Wire.write(0x00); // Continuous measurement mode
Wire.endTransmission(); // Transmit
Wire.beginTransmission(QMC5833L_ADDR); // Select the QMC5883L version A device
Wire.write(QMC5883L_CTRLA_REGISTER); // Select control register A
Wire.write(0x98); // Select 75Hz sample rate and temperature compensation
Wire.endTransmission(); // Transmit
Wire.beginTransmission(QMC5833L_ADDR); // Select the QMC5883L version A device
Wire.write(QMC5883L_CTRLA_REGISTER); // Select control register B
Wire.write(0x20); // Select +-1.3 Gauss
Wire.endTransmission(); // Transmit
}
void loop() {
timeMillis = millis(); // Update the current time
if (timeMillis - sampleTime >= QMC5833L_CONVERSION_TIME) // Read the QMC5883L's data every 14 milliseconds
{
// Tell the QMC5833L where to begin reading data
Wire.beginTransmission(QMC5833L_ADDR); // Select the QMC5883L version A device
Wire.write(QMC5883L_DATA_X_MSB_REGISTER); // Select register X MSB data register
Wire.endTransmission(false); // Transmit and keep the connection alive with false
// Read data from each axis, 2 registers per axis
Wire.requestFrom(QMC5833L_ADDR, 6); // Request the 6 data bytes, 2 bytes per axis
if (Wire.available() >= 6) // Receive the data
{
x = Wire.read() << 8 | Wire.read(); // Read the X axis data
z = Wire.read() << 8 | Wire.read(); // Read the Z axis data
y = Wire.read() << 8 | Wire.read(); // Read the Y axis data
}
sampleTime = timeMillis; // Update the sample time
Serial.print(x); // Output the results to the console
Serial.print(F(" "));
Serial.print(y);
Serial.print(F(" "));
Serial.println(z);
}
}