SPI and 74HC165 problem

hello guys i'm having trouble making spi communication working well.

#include <SPI.h>

#define NUMBER_OF_SHIFT_CHIPS   1


byte dat1,dat2;
byte Grid[NUMBER_OF_SHIFT_CHIPS] = {0};
void setup() {
  Serial.begin(9600);
  SPI.setClockDivider(SPI_CLOCK_DIV128);
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(LSBFIRST);
  SPI.begin();

  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
  SPI.transfer(0x00);
}


void loop() {
    for (int i = 0; i < NUMBER_OF_SHIFT_CHIPS; i++)
  {
    Serial.println(Grid[i],BIN);
  }
  Serial.println("");
  digitalWrite(5, LOW);
  delay(1);
  digitalWrite(5, HIGH);
  for (int i = 0; i < NUMBER_OF_SHIFT_CHIPS; i++)
  {
    Grid[i] = SPI.transfer(0);
    Serial.println(Grid[i]);
  }

  Serial.println("");
  delay(1000);
}

the problem here is that i'm getting 00000010 on pin 11 of the shift register not 00000001 and nothing on pin 6 :confused:

I see you go to some trouble to set all the things that would otherwise default, I assume that what you set them to is correct.

nothing on pin 6

Why would there be anything on pin 6? Are you on the right SPI pins?

Maybe a schematic diagram is called for. No, make that a call for a schematic diagram, please.

This

Recent forum thread has this code, which look similar.

<br

//======================================================================
//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() <<<<<<<<<<<<<<<<<

HTH

a7

thanks for the fast reply and sorry for my bad english :stuck_out_tongue:
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.

Hi, @madblock
Welcome to the forum.

Have you Goolged;

74hc165 arduino library

Unfortunately it has not been optimised for ESP32.

Tom..... :smiley: :+1: :coffee: :australia:

i'm trying to daisychain 40 shift registers but i'm working with only one now to figure out what's the problem.

OK I have the SPI work with the 74c165, see it here


SPI and 74c165 simulkation


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.

HTH

a7

Yikes!

Sry, it half works. Oddly it reports correct switches sometimes, and 0 others.

But this is closer. :wink:

I'll be with the Umbrella Academy soon I hope, I'll let them see what I did wrong.

L8R

a7

thx for everything i'm going to see what can i do with that also thx for the simulator xD

NP. Good luck.

I did try a few things, not expecting them to help and was not therefor disappointed when they did not. Help.

I tried other SPI modes rather than think through what mode is correct. No joy.

I put back in a CE/SS concept, that was certainly not going to change things and certainly did not.

But yeah, gotta love wokwi! Easier than finding a 74c165 around here somewhere and wiring it up.

CU

a7

no luck.
even when it's working on simulation it's not on esp32 same problem it's not reading the last input from the shift register :confused:

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.

sorry mate this is the schematic of connections

I never got it to work in the simulator, and I don't have any 74C165 handy.

Could you post a link to any simulated board and '165 that functions correctly?

I just wanna rule out some infidelity that might be laid off on the simulator.

TIA

a7

good eavening mate yeah you just forgot to declare the latchpin in setup you need to make it non floating :smiley:

here is your simulation corrected

still looking for help for my problem :confused:

Ouch. Noob mistake, don't tell anyone…

THX

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.