//======================================================================
//Reads the byte from the 74HC165 register
byte read74HC165()
{
digitalWrite (Load, LOW); //load the push button state into the 74HC165
asm("nop\n nop\n"); //some delay
digitalWrite (Load, HIGH);
digitalWrite (SSin, LOW); //enable 74HC165 clock
asm("nop\n nop\n"); //some delay
byte Switches = SPI.transfer(0); //get the position
digitalWrite (SSin, HIGH); //disable 74HC165 clock
return Switches; //switches will have the value read by then 74HC165
}
//>>>>>>>>>>>>>> END of read74HC165() <<<<<<<<<<<<<<<<<
thanks for the fast reply and sorry for my bad english
sorry mate was talking about the pin 6 and 11 of the shift register fist and last input .
forgot to say that with normal shift in it's working like a charm but i need it to work with spi.
also i'm using esp32 so i wired Lach pin on ss , miso on serial out and SClK on CLK.
i think my wiring is good if it worked with shift in code.
My beach buddy is on her way, she must not be kept waiting, so I have no time to clean this up.
I did change the SPI mode.
/*
74HC165 Shift register input example
https://wokwi.com/arduino/projects/306031380875182657
(C) 2021, Uri Shaked
converted to SPI by alto777 I hope...
*/
# include <SPI.h>
#define NUMBER_OF_SHIFT_CHIPS 1
byte dat1,dat2;
byte Grid[NUMBER_OF_SHIFT_CHIPS] = {0};
const int dataPin = 2; /* Q7 */
const int clockPin = 3; /* CP */
const int latchPin = 4; /* PL */
const int numBits = 8; /* Set to 8 * number of shift registers */
//======================================================================
//Reads the byte from the 74HC165 register
byte read74HC165()
{
digitalWrite (latchPin, LOW); //load the push button state into the 74HC165
asm("nop\n nop\n"); //some delay
digitalWrite (latchPin, HIGH);
// digitalWrite (SSin, LOW); //enable 74HC165 clock
// asm("nop\n nop\n"); //some delay
byte Switches = SPI.transfer(0); //get the position
// digitalWrite (SSin, HIGH); //disable 74HC165 clock
return Switches; //switches will have the value read by then 74HC165
}
//>>>>>>>>>>>>>> END of read74HC165() <<<<<<<<<<<<<<<<<
void setup() {
Serial.begin(115200);
SPI.setClockDivider(SPI_CLOCK_DIV128);
SPI.setDataMode(SPI_MODE1);
SPI.setBitOrder(LSBFIRST);
SPI.begin();
}
void loop() {
static unsigned int counter;
Serial.print(counter); counter++;
Serial.print(" ");
Serial.println(read74HC165());
delay(100);
}
The example I worked from did not use the clock enable line on the 'c165. It's just perm-enabled. I do not know if you need to do different in your circumstances.
Please provide an annotated schematic not a frizzy as to how you have wired it. Also post the wire lengths. A picture showing your layout would help. What you are trying is very doable but it has a lot of mechanical nuances. If you have a scope show traces of clock, data and select.