Hello Everyone!
I'm new to Arduino and the forum and I'm working on a project involving SPI communications with a gyroscope. I'm following the Barometer example, but am having some trouble understanding how everything works. The sensor is an ADIS16080 from Analog Devices. I know this is a lot to ask- but could I have some help with the questions below.
How do I get 2 pieces of data from the sensor into tempData and velocityData, respectively?
Here's what I have...
[size=8pt]#include <SPI.h>
const int VELOCITY = 0x31; //12 bit velocity reading
const int TEMP = 0x71; //12 bit temperature reading
const byte READ_1 = 0x56; // iSensor set format gyro command offset binary
const byte READ_2 = 0x00; // Second half of read information
// pins used for the connection with the sensor
const int dataReadyPin = 6;
const int chipSelectPin = 7;
void setup() {
Serial.begin(9600);
// start the SPI library:
SPI.begin();
// initalize the data ready and chip select pins:
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
SPI.setBitOrder(MSBFIRST);
// give the sensor time to set up:
delay(100);
}
void loop() {
// don't do anything until the data ready pin is high:
if (digitalRead(dataReadyPin) == HIGH) {
// Read the temperature data
int tempData = readRegister(0x21, 2);
// Read the velocity data
int velocityData = readRegister(0x20, 2);
}
}
// Read from temperature register on iSensor
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
byte inByte = 0; // incoming byte from the SPI
unsigned int result = 0; // result to return
byte dataToSend = READ_T;
Serial.println(thisRegister, BIN);
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(dataToSend);
// send a value of 0 to read the first byte returned:
result = SPI.transfer(0x00);
// decrement the number of bytes left to read:
bytesToRead--;
// if you still have another byte to read:
if (bytesToRead > 0) {
// shift the first byte left, then get the second byte:
result = result << 8;
inByte = SPI.transfer(0x00);
// combine the byte you just got with the previous one:
result = result | inByte;
// decrement the number of bytes left to read:
bytesToRead--;
}
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
// return the result:
return(result);
}
}
//Read from velocity register on iSensor:
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
byte inByte = 0; // incoming byte from the SPI
unsigned int result = 0; // result to return
byte dataToSend = READ_V;
Serial.println(thisRegister, BIN);
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
SPI.transfer(dataToSend);
// send a value of 0 to read the first byte returned:
result = SPI.transfer(0x00);
// decrement the number of bytes left to read:
bytesToRead--;
// if you still have another byte to read:
if (bytesToRead > 0) {
// shift the first byte left, then get the second byte:
result = result << 8;
inByte = SPI.transfer(0x00);
// combine the byte you just got with the previous one:
result = result | inByte;
// decrement the number of bytes left to read:
bytesToRead--;
}
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
// return the result:
return(result);
}[/size]
(code tags added by moderator)