Pages: [1]   Go Down
Author Topic: Using SPI to get data from Gyro/Accel ADIS16365  (Read 34 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey everyone, I'm trying to use a MEGA 2560 to pull data over SPI from a inertial sensor.  Here's the data sheet for the sensor. I've gotten it to a point where it gives me a constant (wrong) number for a reading when stationary but looks correct when actually moving. For example, reading XGYRO, YGYRO, and ZGYRO give the same numbers (something like >500 deg/s) when not moving but if I wiggle the device around I get ~1-5 deg/s. Same thing for the gyro. Supply reading gives me exactly 4V every time (should be 5). As an added bonus, the error bit is always high but the data sheet doesn't really say much about it. Can someone help me out here?

Code:
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  SPI.begin();                          // begin SPI comms
  pinMode(IRQ_PIN, INPUT);              // initialize Interrupt Request pin
  pinMode (SLAVE_PIN, OUTPUT);          // init slave select pin
  SPI.setDataMode(SPI_MODE3);           // set data mode
  SPI.setBitOrder(MSBFIRST);            // set most significant bit first
  SPI.setClockDivider(SPI_CLOCK_DIV8);  // set timing mode
  digitalWrite(SLAVE_PIN, HIGH);        // default slave select is HIGH

  delay(100);                            // wait for module to start up
}

void loop() {
    int input=Serial.read();
    float result = readRegister( XGYRO_OUT, 14, F_GYRO_READ );
    Serial.println( result, DEC );
    delay(200);
}

// reads data from specified address and returns a scaled value
float readRegister( byte address, int releventBits, double scale )
{
  Serial.print( "Reading Register:\t" );  //
  address &= 0b01111111;                  // make sure MSB bit = 0 (to specify R/W as R only)
  Serial.println( address, HEX );
 
  digitalWrite( SLAVE_PIN, LOW );         // start comm process
  SPI.transfer( address );                // first byte is the address
  SPI.transfer( 0x00 );                   // second byte doesn't matter
 
  digitalWrite( SLAVE_PIN, HIGH );
  delay(10);
  digitalWrite( SLAVE_PIN, LOW );
 
  long unsigned int result = SPI.transfer(0x00);            // transmit 0 to retrieve requested data
 
  if( (result >> 6) & 1 == 1 ) { Serial.println("ERROR"); }
 
  result = result << 8;
  result = result | SPI.transfer(0x00);         // shift over first byte and add on new byte
 
  Serial.print("Combined bytes:\t");
  Serial.println( result, BIN );
 
  result &= (unsigned int)(pow(2, releventBits) - 1);  // cut off the bits that are unrequested
 
  Serial.print("Final result:\t");
  Serial.println( result, BIN );
 
  digitalWrite( SLAVE_PIN, HIGH );       // reset slave to off
 
  return result * scale;                 // multiply by scale to get final result
}
Logged

Pages: [1]   Go Up
Jump to: