I tested the results against a friend's phone that has a built-in compass and they agreed.
http://www.sparkfun....products_id=244
I don't have a diagram to show you but hopefully the pin assignments will help.
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace Magnetometer { public class Program { static Cpu.Pin SCLK = Pins.GPIO_PIN_D7; static Cpu.Pin MISO = Pins.GPIO_PIN_D6; static Cpu.Pin MOSI = Pins.GPIO_PIN_D5; static Cpu.Pin SSNOT = Pins.GPIO_PIN_D4; static Cpu.Pin DRDY = Pins.GPIO_PIN_D3; static Cpu.Pin RESET = Pins.GPIO_PIN_D2; static OutputPort sclk = new OutputPort(SCLK, false); static OutputPort reset = new OutputPort(RESET, false); static OutputPort mosi = new OutputPort(MOSI, false); static OutputPort ssnot = new OutputPort(SSNOT, false); static InputPort miso = new InputPort(MISO, false, Port.ResistorMode.Disabled); static InputPort drdy = new InputPort(DRDY, false, Port.ResistorMode.Disabled); static float x = 0, y = 0, z = 0; static float heading = 0; public static void Main() { // write your code here while (true) { x = QueryMag(0); y = QueryMag(1); z = QueryMag(2); heading = getHeading(x, y, z); Debug.Print("X: " + x.ToString()); Debug.Print("Y: " + y.ToString()); Debug.Print("Z: " + z.ToString()); Debug.Print("Heading: " + heading.ToString()); Thread.Sleep(5000); } } private static float getHeading(float x, float y, float z) { heading = 0; if ((x == 0) && (y < 0)) heading = (float)(System.Math.PI / 2.0); if ((x == 0) && (y > 0)) heading = (float)(3.0 * System.Math.PI / 2.0); if (x < 0) heading = (float)(System.Math.PI - MathLib.Atan(y / x)); if ((x > 0) && (y < 0)) heading = (float)MathLib.Atan(y / x) * -1; if ((x > 0) && (y > 0)) heading = (float)(2.0 * System.Math.PI - MathLib.Atan(y / x)); return (float)(heading * 180 / System.Math.PI); } private static void ResetMag() { reset.Write(false); Thread.Sleep(2); reset.Write(true); Thread.Sleep(2); reset.Write(false); Thread.Sleep(2); Debug.Print("resetted"); } private static void sendBit(bool hilo) { mosi.Write(hilo); Thread.Sleep(2); sclk.Write(true); Thread.Sleep(2); sclk.Write(false); Thread.Sleep(2); } private static float QueryMag(int axis) { ResetMag(); sendBit(false); sendBit(true); sendBit(true); sendBit(false); sendBit(false); sendBit(false); switch (axis) { case 0 : sendBit(false); sendBit(true); break; case 1 : sendBit(true); sendBit(false); break; case 2 : sendBit(true); sendBit(true); break; default: break; } while (drdy.Read() == false) { Debug.Print("waiting for DRDY"); } long total = 0; long sign = recieveBit(); for (int ii = 14; ii >= 0; ii--) { long thisbit = recieveBit(); thisbit = thisbit << ii; total = total | thisbit; Debug.Print("fired"); } if (sign == 1) { total = total - 32768; } return total; } private static long recieveBit() { sclk.Write(true); Thread.Sleep(2); int bit; if (miso.Read() == true) bit = 1; else bit = 0; Thread.Sleep(2); sclk.Write(false); Thread.Sleep(2); return bit; } } }
I needed a math library for the atan, which I got from http://www.microfram...-with-full-net/
I hope others find this useful.