简体   繁体   中英

SPI sends same data no matter which register I read

I am working on a robotics project where I have a robot that includes a few different sensors and an Arduino. One of my sensors is a gyro that communicates over SPI. I have figured out how to communicate and get data from the gyro but the problem is that after I get a few bytes of data the gyro then just sends the same bytes over and over, no matter which register I read.

Data from gyro on serial console:

串行控制台上来自陀螺仪的数据

I'm not sure how to continuously get refreshed data. I am pulling the SS pin from HIGH to LOW each time before I read the data, but it doesn't seem to help.

Here is my code:

#include <SPI.h>
const int slavePin = 10;

// set up the speed, data order and data mode
SPISettings settings(3000000, MSBFIRST, SPI_MODE0); //mode_0

void setup() {
  // set the Slave Select Pins as outputs:
  SPI.beginTransaction(settings);

  pinMode (slavePin, OUTPUT);
  Serial.begin(9600);
  // initialize SPI:
  SPI.begin();
}

byte stat, val1, val2, result;
void loop() {
  digitalWrite(slavePin, LOW);//select slave
  SPI.transfer(0x08);//register to read
  stat = SPI.transfer(0);//store data
  val1 = SPI.transfer(0);
  val2 = SPI.transfer(0);

  result = val1 + val2;//combine the input data
  Serial.print(stat);
  Serial.print(":");
  Serial.print(result);

  //  Serial.print(val1); //was sending data to console seperatly
  //  Serial.print(":");
  //  Serial.print(val2);
  Serial.println();

  digitalWrite(slavePin, HIGH);//deselect slave
}

Link to data sheet

Also, as a small side question, I'm not sure in which order I should add the bytes together to get the correct value. And, I'm not sure how to handle signed bytes.

I found a solution on the arduino forums! http://forum.arduino.cc/index.php?topic=95901.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM