I'm trying to interface with a sensor using SPI protocol. The sensor has a data ready pin that turns high when there is data to read. That pin is used to trigger an interrupt that causes the Arduino Uno to read the data. After the data is read it is transmitted to the computer and displayed on the serial output.
For some reason this arrangement never starts up automatically. It starts at random. When it does work it's only for a few seconds. Sometimes I can get it to start by applying a voltage to pin 2.
I've tried adding delays, slowing the sensor acquisition rate but nothing seems to work.
Any ideas on how to make it work or other methods to do the same thing.
An experienced programmer from the sensor company made a library and sketch that works with the Uno. I've already found a few bugs so it wouldn't surprise me if there was more.
void setup()
{
Serial.begin(115200); // Initialize serial output via USB
IMU.configSPI(); // Configure SPI communication
delay(100); // Give the part time to start up
IMU.regWrite(MSC_CTRL,0x06); // Enable Data Ready on IMU
delay(20);
IMU.regWrite(SMPL_PRD,0x201), // Set Decimation on IMU
delay(20);
attachInterrupt(0, setDRFlag, RISING); // Attach interrupt to pin 2. Trigger on the rising edge
}
void grabData()
{
// Put all the Data Registers you want to read here
SPI.setBitOrder(MSBFIRST); // Per the datasheet
SPI.setClockDivider(SPI_CLOCK_DIV8); // Config for 2MHz (ADIS16448 max 2MHz)
SPI.setDataMode(SPI_MODE3); // Clock base at one, sampled on falling edge
MZ = IMU.regRead(ZMAGN_OUT);
}
// Function used to scale all acquired data (scaling functions are included in ADIS16448.cpp)
void scaleData()
{
MZS = IMU.magnetometerScale(MZ); //Scale Z Magnetometer
}
// Data Ready Interrupt Routine
void setDRFlag()
{
validData = !validData;
}
// Main loop. Scale and display registers read using the interrupt
void loop()
{
if (validData) // If data present in the ADIS16448 registers is valid...
{
grabData(); // Grab data from the IMU
scaleData(); // Scale data acquired from the IMU
//Print scaled data
Serial.print("ZMAG: ");
Serial.println(MZS);
//delay(100); // Give the user time to read the data
}
}