I am extremely confused as to why I cannot seem to get good readings via SPI.
I’ve checked wires, read documentation, stared at it intensely but can’t find the problem.
Here is my mapping of what I believe the possible combinations for reading registers should be.
20:20:42.093 -> Sent: 1000000000000000 (Decimal: 32768) , Received: 0000000000000000 (Decimal: 0)
20:20:42.157 -> Sent: 1000100000000000 (Decimal: 34816) , Received: 0000000000000000 (Decimal: 0)
20:20:42.189 -> Sent: 1001000000000000 (Decimal: 36864) , Received: 0000000000000000 (Decimal: 0)
20:20:42.253 -> Sent: 1001100000000000 (Decimal: 38912) , Received: 0000000000000000 (Decimal: 0)
20:20:42.285 -> Sent: 1010000000000000 (Decimal: 40960) , Received: 0000000000000000 (Decimal: 0)
20:20:42.350 -> Sent: 1010100000000000 (Decimal: 43008) , Received: 0000000000000000 (Decimal: 0)
20:20:42.382 -> Sent: 1011000000000000 (Decimal: 45056) , Received: 0000000000000000 (Decimal: 0)
20:20:42.446 -> Sent: 1011100000000000 (Decimal: 47104) , Received: 0000000000000000 (Decimal: 0)
20:20:42.478 -> Sent: 1100000000000000 (Decimal: 49152) , Received: 0000000000000000 (Decimal: 0)
20:20:42.542 -> Sent: 1100100000000000 (Decimal: 51200) , Received: 0000000000000000 (Decimal: 0)
20:20:42.607 -> Sent: 1101000000000000 (Decimal: 53248) , Received: 0000000000000000 (Decimal: 0)
20:20:42.639 -> Sent: 1101100000000000 (Decimal: 55296) , Received: 0000000000000000 (Decimal: 0)
20:20:42.704 -> Sent: 1110000000000000 (Decimal: 57344) , Received: 0000000000000000 (Decimal: 0)
20:20:42.737 -> Sent: 1110100000000000 (Decimal: 59392) , Received: 0000000000000000 (Decimal: 0)
20:20:42.801 -> Sent: 1111000000000000 (Decimal: 61440) , Received: 0000000000000000 (Decimal: 0)
20:20:42.833 -> Sent: 1111100000000000 (Decimal: 63488) , Received: 0000000000000000 (Decimal: 0)
20:20:42.897 -> Sent: 0000000000000001 (Decimal: 1) , Received: 0000000000000000 (Decimal: 0)
20:20:42.929 -> Sent: 0000000000010001 (Decimal: 17) , Received: 0000000000000000 (Decimal: 0)
20:20:42.994 -> Sent: 0000000000001001 (Decimal: 9) , Received: 0000000000000000 (Decimal: 0)
20:20:43.026 -> Sent: 0000000000011001 (Decimal: 25) , Received: 0000000000000011 (Decimal: 3)
20:20:43.091 -> Sent: 0000000000000101 (Decimal: 5) , Received: 0000000000000000 (Decimal: 0)
20:20:43.155 -> Sent: 0000000000010101 (Decimal: 21) , Received: 0000000000000000 (Decimal: 0)
20:20:43.187 -> Sent: 0000000000001101 (Decimal: 13) , Received: 0000000000000000 (Decimal: 0)
20:20:43.252 -> Sent: 0000000000011101 (Decimal: 29) , Received: 0000000000000011 (Decimal: 3)
20:20:43.284 -> Sent: 0000000000000011 (Decimal: 3) , Received: 0000000000000000 (Decimal: 0)
20:20:43.348 -> Sent: 0000000000010011 (Decimal: 19) , Received: 0000000000000000 (Decimal: 0)
20:20:43.380 -> Sent: 0000000000001011 (Decimal: 11) , Received: 0000000000000000 (Decimal: 0)
20:20:43.445 -> Sent: 0000000000011011 (Decimal: 27) , Received: 0000000000000011 (Decimal: 3)
20:20:43.477 -> Sent: 0000000000000111 (Decimal: 7) , Received: 0000000000000000 (Decimal: 0)
20:20:43.542 -> Sent: 0000000000010111 (Decimal: 23) , Received: 0000000000000000 (Decimal: 0)
20:20:43.606 -> Sent: 0000000000001111 (Decimal: 15) , Received: 0000000000000000 (Decimal: 0)
20:20:43.639 -> Sent: 0000000000011111 (Decimal: 31) , Received: 0000000000000011 (Decimal: 3)
20:20:43.704 -> Finished0000000 (Decimal: 43008) , Received: 0000000000000000 (Decimal: 0)
I’m sure its probably something with my code.
//#include <DRV8323S.h>
const int chipSelectPin = 4; // Change this to your CS pin
const int enableDRV8323Pin = 17;
void setup() {
// Start the Serial communication
Serial.begin(115200);
// Initialize SPI
SPI.begin();
// Set the chip select pin as output
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH); // Deselect the device
//Enable the DRV8323
pinMode(enableDRV8323Pin, OUTPUT);
digitalWrite(enableDRV8323Pin, HIGH);
delay(1000);
}
uint16_t valueList[] = {
0b1000000000000000,
0b1000100000000000,
0b1001000000000000,
0b1001100000000000,
0b1010000000000000,
0b1010100000000000,
0b1011000000000000,
0b1011100000000000,
0b1100000000000000,
0b1100100000000000,
0b1101000000000000,
0b1101100000000000,
0b1110000000000000,
0b1110100000000000,
0b1111000000000000,
0b1111100000000000,
0b0000000000000001,
0b0000000000010001,
0b0000000000001001,
0b0000000000011001,
0b0000000000000101,
0b0000000000010101,
0b0000000000001101,
0b0000000000011101,
0b0000000000000011,
0b0000000000010011,
0b0000000000001011,
0b0000000000011011,
0b0000000000000111,
0b0000000000010111,
0b0000000000001111,
0b0000000000011111,
};
uint16_t idx = 0;
void loop() {
if(idx>=32){
Serial.println("Finished");
// Disable the DRV8323
digitalWrite(enableDRV8323Pin, LOW);
delay(1000000000000);
};
//Serial.println(0x8000 | (regAdr & 0x7) << 11);
// Select the SPI device
digitalWrite(chipSelectPin, LOW);
// Begin SPI transaction with specified settings
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1));
// Write to the SPI device
uint16_t response = SPI.transfer(valueList[idx]);
// Deselect the device
digitalWrite(chipSelectPin, HIGH);
// Print the response to the Serial Monitor
Serial.print("Sent: ");
Serial.print(printBinary(valueList[idx], 16)); // Print in binary
Serial.print(" (Decimal: ");
Serial.print(valueList[idx]);
Serial.print(")\t, Received: ");
Serial.print(printBinary(response, 16)); // Print response in binary
Serial.print(" (Decimal: ");
Serial.print(response);
Serial.println(")");
// End the transaction
SPI.endTransaction();
// Prevent another loop
delay(50);
idx++;
}
String printBinary(int num, int bits) {
String binaryString = "";
// Loop through each bit
for (int i = bits - 1; i >= 0; i--) {
// Check if the bit at position i is set
if (num & (1 << i)) {
binaryString += "1";
} else {
binaryString += "0";
}
}
return binaryString;
}```
If I don't power the DRV8323 with an exernal power supply and just have vref connected, I get 255 as a response.
Any ideas?