SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By silic0re
#84548
maybe this will help:

---

int read_mag (unsigned char sample_axis) {
// Sample_axis: x (1), y (2), z (3)
// Connections:
// data ready: RF2 (I)
// SS: RD15 (O)
// RESET: RF3 (O)

int mag_measurement = 0;
unsigned char temp = 0;
unsigned int highbyte = 0;
unsigned int lowbyte = 0;

unsigned int x1 = 0;

unsigned char command_byte = 0;
unsigned char i=0;

TRISDbits.TRISD15 = 0;
LATDbits.LATD15 = 0; // Toggle SS pin high

i = 6; // Set magnetic sensitivity to /2048

// Reset
TRISFbits.TRISF3 = 0;
LATFbits.LATF3 = 1; // Toggle RESET pin high
delay(1000);
LATFbits.LATF3 = 0; // Toggle RESET pin low

// Write Command Byte
command_byte = (i << 4) + sample_axis;
WriteSPI1(command_byte);

// Wait until data is ready (or use data ready pin)
TRISFbits.TRISF2 = 1;
while (PORTFbits.RF2 == 0) {
// Loop until data ready pin is high
}

temp = ReadSPI1(); // Empty SPI buffer
delay(2000);


WriteSPI1(0x00); // High byte
while(DataRdySPI1()==0){}
highbyte = ReadSPI1();
delay(1000);

WriteSPI1(0x00); // Low byte
while(DataRdySPI1()==0){}
lowbyte = ReadSPI1();
delay(1000);


mag_measurement = (int)(256 * highbyte) + (int)lowbyte;

return mag_measurement;

}
By jterickson17
#84681
Thanks, silic0re. I'll see if I can get any futher with you code snippet :)
By jterickson17
#85700
HAHAHA!

For the record, the problem was that the top part of the sensor had been sheared off partially. Must have been one of the UAV trial runs :)