I'm working with the Arduino Pro Mini 3.3V 8MHz and trying to interface via the SPI library to the Analog Devices ADIS16367 inertial measurement unit.
It says to read a register I need to send 16 bits, then I can receive 16 bits of data from the register. Thus, I am using two 8 bit SPI.transfer() calls for each 16 bit transfer.
No matter what register I try to read, it gives me the hex value 0x900.
I have no idea why!!!! Any thoughts would be appreciated.
p.s. I already communicated with an SD card via SPI, so the wiring should be correct.
The entire code is below
Thanks!
/*
ADIS 16367 communication
Beginning communication with the ADIS 16367
Circuit:
Arduino -> ADIS 16367
IRQ: pin 2 -> DIO1: pin 7
CS: pin 7 -> CS: pin 6
DI/MOSI: pin 11 -> DIN: pin 5
DOUT/MISO: pin 12 -> DOUT: pin 4
SCLK: pin 13 -> SCLK: pin 3
created 1 Dec 2010
modified 10 Jan 2011
by Matt Harding
*/
// the sensor communicates using SPI, so include the library:
#include <SPI.h>
//Sensor's memory register addresses:
const byte XGYRO_OFF= 0x1A; // X-gyro offset
const byte YGYRO_OFF= 0x1C; // Y-gyro offset
const byte ZGYRO_OFF= 0x1E; // Z-gyro offset
const byte XACCL_OFF= 0x20; // X-accelerometer offset
const byte YACCL_OFF= 0x22; // Y-accelerometer offset
const byte ZACCL_OFF= 0x24; // Z-accelerometer offset
const byte ALM_MAG1 = 0x26; // Alarm 1 amplitude threshold
const byte ALM_MAG2 = 0x28; // Alarm 2 amplitude threshold
const byte ALM_SMPL1 = 0x2A; // Alarm 1 sample size
const byte ALM_SMPL2 = 0x2C; // Alarm 2 sample size
const byte ALM_CTRL = 0x2E; // Alarm control
const byte GPIO_CTRL = 0x32; // Auxiliary digital I/O control
const byte MSC_CTRL = 0x34; // Data-ready, self-test, miscellaneous
const byte SMPL_PRD = 0x36; // internal sample rate control
const byte SENS_AVG = 0x38; // range and filter control
const byte SLP_CNT = 0x3A; // Sleep mode control
const byte DIAG_STAT = 0x3C; // System status
const byte GLOB_CMD = 0x3E; // System Command
const byte PROD_ID = 0x56; // Product ID number (16367)
const byte SERIAL_NUM = 0x58; // Serial number
//Sensor commands
const byte READ = 0b00000000; // ADIS16367 read command (2 bytes)
const byte WRITE = 0b11111111; // ADIS16367 write command
const byte BURST_READ = 0x3E; // ADIS16367 burst read command
// pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
const int dataReadyPin = 2;
const int chipSelectPin = 7;
volatile int i = 0;
int j = i;
void setup() {
Serial.begin(115200);
// start the SPI library:
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3); // set SPI mode 3
// initalize the data ready and chip select pins:
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH);
// give the sensor time to set up:
delay(1000);
}
void loop() {
while(digitalRead(dataReadyPin)); //Wait if the pin is low
readRegister(); //Read and print stuff
delay(1000);
}
//Sends a read command to the ADIS16367:
unsigned int readRegister(byte thisRegister ) {
unsigned int result = 0; // result to return
// ADIS16367 expects the register address in the lower 7 bits
// now combine the register address and the command into one byte:
byte dataToSend = thisRegister | READ;
Serial.print("Register: ");
Serial.println(thisRegister, HEX);
Serial.print("Data to send: ");
Serial.println(dataToSend, HEX);
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
Serial.println(SPI.transfer(dataToSend),HEX);
// send a value of 0 to read the first byte returned:
Serial.println(SPI.transfer(0x00),HEX);
result = SPI.transfer(0x00);
Serial.print("byte 1: ");
Serial.println(result, HEX);
result = result << 8;
Serial.print("byte 1 shifted: ");
Serial.println(result, HEX);
result |= SPI.transfer(0x00);
Serial.print("Final Result: ");
Serial.println(result, HEX);
Serial.println();
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
// return the result:
return(result);
}