I have been trying to read 3D magnetic data from a Sparkfun MicroMag3 magnetic sensor. I am working on building a tilt adjusted compass to go on my 15x80 binoculars. I was able to get an 8 bit read from a LIS3LV02DQ accelerometer running in one evening. But I am a now stumped how to get 2 bytes back from my MicroMag3 instead of a single byte returned from my accelerometer.
The MicroMag3 documentation lists 6 easy (?) steps.
- Bring SlaveSelect low (Pin 4)
- Pulse the RESET (Pin 6) high and back to low. You must RESET the chip for every reading
- Send 8 bits on the MOSI line (Pin 3). The MicroMag3 then executes the command. (Note: I use the SPDR = 0x01 statement to send a command to the sensor. 0x01 is the command for measuring the X axis.)
- The chip makes the measurement. We just wait.
- The chip DRDY (Pin 5) is set high to indicate data ready
- In response to the next 16 SCKL pulses, data is shifted out the MISO (Pin 2) line.
Repeat as desired.
Step 6 has me stumped. I modified the code from my accelerometer project but did not have a lot of luck with the MicroMag3. I am receiving some data back from the sensor that changes slowly when I rotate and tilt the sensor. It returns the same value if I leave it on the table, so it appears to be doing something. I have a guess I am getting the first byte back.
I need two specific pieces of advice:
- I used the idiom:
while (!(SPSR & (1<<SPIF)))
{};
return SPDR; // received data appears in the SPDR register
to check to see if the data was ready. Should I read the READY pin directly and ignore the ATMega SPI registers?
- How can I send 1 byte and get back 2 bytes? It looks like I am getting one byte back. I can not modify SPDR because it a a physical register on the ATMega chip.
Jim Kraemer
AD5UY