// write data out
outByte = 0b00000001; // 1 output low
digitalWrite(ssPin, LOW);
SPI.transfer(outByte); // output 0 goes Low
digitalWrite(ssPin, HIGH); // outputs are updated on this rising edge
// read the data in
digitalWrite (latchPin, LOW);
digitalWrite (latchPin, HIGH);
inByte = SPI.transfer(0); // clock in the sampled data
// find the 1's coming in
inByte = 0b11111111 - inByte; // in this example, 11111111 - 11111110 = 1. if 11101110 came back >00010001
if ( inByte == outByte){
Serial.println ("bytes match");
}
else {
Serial.print ('byte mismatch ");
Serial.print (outByte, BIN);
Serial,print (" vs ");
Serial.println (inByte, BIN);
}
So with 155 pins, 20 devices, do the writes and reads from an array, compare the array data byte by byte to check for errors.
Develop a smart way to walk a 1 thru the out bound data for a 155 loop.
Maybe a loop within a loop, the outer loop goes thru the 20 bytes, the inner loop goes thru the 8 bits of a register.
Or just make a large array that’s 20 bytes long by 155 rows:
byte testArray[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // first wire
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, // 2nd wire
:
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, // 9th wire
:
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, //
159th wire
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, //
160th wire
};
20 bytes x 155 rows = 3100 bytes, so you probably want to store that in PROGMEM.
for (x=0; x<156; x=x+1){ // loop thr 155 rows
digitalWrite (ssPin, LOW);
for (y=0; y<20; y=y+1){ // loop thru a row
SPI.transfer (testArray((x*155)+y); // send a byte out
}
digitalWrite (ssPin, HIGH); // 20 registers get updated
// now read them back
digitalWrite (latchPin, LOW);
digitalWrite (latchPin, HIGH);
for (z=0; z<20; z=z+1){
readArray[x] = SPI.transfer(0);
}
// and compare them
for (z=0; z<20; z=+1){
if (0x11111111 - readArray[z] == testArray((x*155)+z) ){
Serial.print (z);
Serial.print ("ok");
}
}
Losing track of where I am in this little box, but I think you can get the idea. I think having a big array with a bit to check per row is a little easier to manipulate vs finding a way to walk a 1 across the row.