I have following code to read HC165 shift register, and it get stuck when calling SPI.transfer on second call. The pins are connected like this
D3 = Latch
D12 = QH
D13 = CLK
#include <SPI.h>
bool ENABLE_SPI = true;
int latchPin = 3;
int dataPin = 12;
int clockPin = 13;
void setup() {
Serial.begin(115200);
pinMode(latchPin, OUTPUT);
if (!ENABLE_SPI)
{
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
}
}
void loop() {
if (ENABLE_SPI)
SPI.beginTransaction(SPISettings(10000, MSBFIRST, SPI_MODE0));
digitalWrite(latchPin, 0);
delayMicroseconds(5);
digitalWrite(latchPin, 1);
delayMicroseconds(5);
Serial.println("SPI Transfer Begin");
byte readV;
if (ENABLE_SPI)
{
readV = SPI.transfer(0);
SPI.endTransaction();
}
else
readV = shiftIn(dataPin, clockPin, MSBFIRST);
Serial.println("DATA-");
Serial.println(readV, BIN);
Serial.println("=");
delay(250);
}
And the output is always
SPI Transfer Begin
DATA-
0
=
SPI Transfer Begin
but if I use shiftIn with specified Pin12 and Pin13. It works good.