I'm working on creating a bit-banged SPI implementation on an Arduino Pro Mini 3.3V 8MHz. I must dedicate the hardware SPI bus to another device, but want to communicate with another SPI sensor (Analog Devices ADIS16367 IMU).
It uses SPI Mode 3 (clock polarity = 1, clock phase = 1) and I created the simple code below. When I want to read a register, it returns some value, but it is not correct.
When I want to read something such as the Product ID, I always receive the same value, as I should, but it is not the correct value.
When I want to read something such as an accelerometer, I always receive fluctuating values, as I should because of the signal noise, but it is also not a correct value.
Anyone have any thoughts?
Thanks!
//Sends a read command to the ADIS16367:
int readRegisterInt(byte thisRegister ) {
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:
int dataToSend = ((int)thisRegister<<8)&0xFF00;
while(digitalRead(dataReadyPin)); //Wait if the pin is low
// send the device the register you want to read:
spiIntTransfer(dataToSend);
result = spiIntTransfer(0x0000);
// return the result:
return(result);
}
int spiIntTransfer(int data) {
// SCK begins high
for(byte bit=0 ; bit<16 ; bit++) {
digitalWrite(sclkPin,LOW); // SCK fall low
digitalWrite(mosiPin, (data>>15) & 0x0001); // Write data to MOSI pin
data = data << 1;
digitalWrite(sclkPin,HIGH); // SCK rise high
// data |= digitalRead(misoPin); // read data from MISO pin
// Serial.print(bitRead(PIND,5),BIN);
data |= bitRead(PIND,misoPin); // read data from MISO pin
}
return(data);
}