Moin,
ich versuche nun schon seit einigen Tagen den Sensor ISM330DLC mit meinem Arduino über SPI anzusprechen. Leider bekomme ich nur von dem "Who am I" Register eine Antwort, welche 6B lautet aber eigentlich 6A sein sollte.
Hier ist mal der Code und vielleicht fällt euch ja auf was ich falsch gemacht haben könnte:
#include <SPI.h>
const byte READ = 0b01; //ISM330DLC read command
const byte WRITE = 0b00000000; //ISM330DLC write command
const int chipSelectPin = 7;
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(chipSelectPin, OUTPUT);
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE2));
// writeRegister(0x01, 0x80);
unsigned v1;
v1 = readRegister(0x0F, 1); //das Who am I register ansprechen
Serial.println(v1, HEX);
Serial.println(v1, BIN);
SPI.endTransaction();
delay(100);
}
void loop(){
}
//Read from or write to register from the ISM330DLC:
unsigned int readRegister(byte thisRegister, int bytesToRead) {
byte inByte = 0; // incoming byte from the SPI
unsigned int result = 0; // result to return
Serial.print(thisRegister, BIN);
Serial.print("\t");
// ISM330DLC expects the register name in the upper 6 bits
// of the byte. So shift the bits left by one bit:
thisRegister = thisRegister << 1;
// now combine the address and the command into one byte
byte dataToSend = thisRegister | 0x1;
Serial.println(dataToSend, 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);
//Serial.println(r, BIN);
// send a value of 0 to read the first byte returned:
result = SPI.transfer(0x00);
Serial.println(result, HEX);
// 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);
}
//Sends a write command to ISM330DLC
void writeRegister(byte thisRegister, byte thisValue) {
// SCP1000 expects the register address in the upper 6 bits
// of the byte. So shift the bits left by two bits:
thisRegister = thisRegister << 1;
// now combine the register address and the command into one byte:
byte dataToSend = thisRegister;
// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
SPI.transfer(dataToSend); //Send register location
SPI.transfer(thisValue); //Send value to record into register
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
}