Skip to content

Instantly share code, notes, and snippets.

@andysheen
Created July 18, 2017 13:17
Show Gist options
  • Save andysheen/3687771a08d0f874b9539ebb4c99b753 to your computer and use it in GitHub Desktop.
Save andysheen/3687771a08d0f874b9539ebb4c99b753 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#define SOFT_FAST
#define MAG_ADDRESS 0x30
float magX, magY, magZ;
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
readMag();
delay(100);
}
void write8(byte address, byte reg, byte value)
{
Wire.beginTransmission(address);
Wire.write((uint8_t)reg);
Wire.write((uint8_t)value);
Wire.endTransmission();
}
void readMag() {
write8((byte)MAG_ADDRESS, (byte)0x08, (byte)0x01);
delay(50);
byte done = read8((byte)MAG_ADDRESS, 0x07);
while (done % 2 == 0) {
read8((byte)MAG_ADDRESS, 0x07);
}
Wire.beginTransmission((byte)0x30);
Wire.write((uint8_t)0x00);
Wire.endTransmission();
Wire.requestFrom((byte)0x30, (byte)6);
byte xlo = Wire.read();
byte xhi = Wire.read();
byte ylo = Wire.read();
byte yhi = Wire.read();
byte zlo = Wire.read();
byte zhi = Wire.read();
Serial.print("X: ");
Serial.println((uint8_t)xhi | ((uint8_t)xlo << 8));
}
byte read8(byte address, byte reg)
{
byte value;
Wire.beginTransmission(address);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom(address, (byte)1);
value = Wire.read();
Wire.endTransmission();
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment