teensy 4.1 with 74hc165 register need help!

hello there

i want to attach a 74hc165 to a teensy 4.1
i found a tutorial for arduino and there it works, so i tried hooking it up to my teensy 4.1 buth bthere everything goes wrong, i read so much different theads that i completely f*****cked it up
https://www.woolseyworkshop.com/2021/02/18/adding-digital-io-to-your-arduino-part-2-the-74hc165/

this is the scheme i use:
https://i0.wp.com/www.woolseyworksh...o_74HC165_Schematic.png?resize=1140,459&ssl=1

this is my code
Code:
const unsigned long SamplePeriod = 5000;  // sampling period in milliseconds

const uint8_t ISRDataPin = 14;   // connected to 74HC165 QH (9) pin //datapin
const uint8_t ISRLatchPin = 11;  // connected to 74HC165 SH/LD (1) pinMISO0
const uint8_t ISRClockPin = 10;  // connected to 74HC165 CLK (2) pin //clock pin t 35 CS0 10 op41


void setup() {
   Serial.begin(9600);  
   while (!Serial);     
   
   // 74HC165 shift register
   pinMode(ISRDataPin, INPUT);
   pinMode(ISRLatchPin, OUTPUT);
   pinMode(ISRClockPin, OUTPUT);
}

void loop() {
   static unsigned long previousTime = 0;
   unsigned long currentTime = millis();
   if (currentTime - previousTime >= SamplePeriod) {
      //readInputsWithDigitalRead();
      //readInputsWithBinaryValues();
      update();
   //  readShiftRegisters();
      previousTime = currentTime;
   }
}


uint8_t isrReadRegister() {
   uint8_t inputs = 0;
   digitalWrite(ISRClockPin, HIGH);  // preset clock to retrieve first bit
   digitalWrite(ISRLatchPin, HIGH);  // disable input latching and enable shifting
   inputs = shiftIn(ISRDataPin, ISRClockPin, MSBFIRST);  // capture input values
   digitalWrite(ISRLatchPin, LOW);  // disable shifting and enable input latching
   return inputs;
}

void update() {
   static uint8_t previousInputs = 0;
   uint8_t currentInputs = isrReadRegister();  // read all inputs from shift register
   if (currentInputs != previousInputs) {  // print values only if they changed
      Serial.print("Inputs: 0b");  // print all inputs represented as a full byte
      for (int8_t i = 7; i >= 0; i--) {
         Serial.print(bitRead(currentInputs, i));  // print value for each bit
         
      }
      Serial.println();
      previousInputs = currentInputs;
   }
}

first of al i think my wiring the three cables to teensy is wrong?
i dont see it anymore and becoming confused of the different posts on internet
could someone help me to wire it correctly?

and second of al could someone help me to justify the code so that is working correctly
i think i have made some clock mistakes

also i read in some topics thats the teebsy 4.1 would be to fast, tried lowering to 24mhz but stil no results

any help would be gratefully appreciated!
im really lost on this one and need someone who has done this already and could help me

please pm me if you need more information

oh yeh one thing i use a 5volt external adaptor and made the correct adjustments to the teensy for not frying it ;-)
 
Last edited by a moderator:
Sorry it has been long time since I played with shift registers like this.

In most cases, it helps when you maybe show picture of how things are actually wired up, as to maybe see if there is an issue with it.
It helps to see things, like are you using a breadboard? If so, are the Teensy pins soldered to the Teensy, and there are are no short or cold solder joints.
Also things like, do you have the not CE pin connected to GND?

Which 74HC165 chip are you using? Do you have a link to it?

You mentioned that you are running the chip with +5v routed to its power connection. (VCC)? If you then run its output pins to the Teensy, with +5v this could easily damage (destroy) a T4.1
However at least the one datahseet I looked at for this chip(https://www.ti.com/lit/ds/symlink/s...ps%3A%2F%2Fwww.ti.com%2Flit%2Fgpn%2Fsn74hc165, looks like VCC can be in the range from 2v to 6v. So if you run 3.3v to it's VCC, it should be safe for the Teensy.
But not sure if that voltage works with what you are trying to connect to (Pins A-H). But if it is like the dipswitch in the link you showed, it should work, but you should route 3.3v to the dipswitch and not 5v.

I did a quick and dirty run of your code with no hardware. It is hooked up to Logic Analyzer. I added a digitalToggleFast(13) as well as set pin 13 to output, So I could easily check to see if
the code was hung or not.... Not:

Screenshot.jpg

I believe that at least for the spec that I looked at, the chip clock could run at 25mhz in the 3.3v range, so should be close as shown in capture.

The way they do the latch pin may be fine, when I did it, I used code that more closely matched the document:
Screenshot1.png

Something like:
Code:
uint8_t isrReadRegister() {
   uint8_t inputs = 0;
   digitalWrite(ISRClockPin, HIGH);  // preset clock to retrieve first bit
   digitalWrite(ISRLatchPin, LOW);  // disable shifting and enable input latching
   delayNanoseconds(50);
   digitalWrite(ISRLatchPin, HIGH);  // disable input latching and enable shifting
   inputs = shiftIn(ISRDataPin, ISRClockPin, MSBFIRST);  // capture input values
   return inputs;
}
Which gives you an output like:
Screenshot2.png

But the current code may work just fine.

If it turns out that the inputs = shiftIn(ISRDataPin, ISRClockPin, MSBFIRST); // capture input values
is too fast, I would then write my own based off of what the shiftin does:

The implementation is like:
Code:
uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
{
	uint8_t mask, value=0;
	for (mask=0x80; mask; mask >>= 1) {
		digitalWrite(clockPin, HIGH);
		if (digitalRead(dataPin)) value |= mask;
		digitalWrite(clockPin, LOW);
	}
	return value;
}
The shift in is actually a wrapper that calls off to two different functions depending on MSB or LSB First.
Btu you could take this code and for example add in delays to slow it down if necessary.

Hope that helps
 
reply

Sorry it has been long time since I played with shift registers like this.

In most cases, it helps when you maybe show picture of how things are actually wired up, as to maybe see if there is an issue with it.
It helps to see things, like are you using a breadboard? If so, are the Teensy pins soldered to the Teensy, and there are are no short or cold solder joints.
Also things like, do you have the not CE pin connected to GND?
i tried my best for a picture, yes i attached the not CE to ground, the teensy was pre soldered and the joins are looking ok
318855036_3407880766126005_520030535504542054_n.jpg


Which 74HC165 chip are you using? Do you have a link to it?
im using this one https://www.ti.com/lit/ds/symlink/s...76305&ref_url=https%3A%2F%2Fwww.google.com%2F

You mentioned that you are running the chip with +5v routed to its power connection. (VCC)? If you then run its output pins to the Teensy, with +5v this could easily damage (destroy) a T4.1
However at least the one datahseet I looked at for this chip(https://www.ti.com/lit/ds/symlink/s...ps%3A%2F%2Fwww.ti.com%2Flit%2Fgpn%2Fsn74hc165, looks like VCC can be in the range from 2v to 6v. So if you run 3.3v to it's VCC, it should be safe for the Teensy.
But not sure if that voltage works with what you are trying to connect to (Pins A-H). But if it is like the dipswitch in the link you showed, it should work, but you should route 3.3v to the dipswitch and not 5v.
nomally you can run the teensy with 5v without problerm if you cut the usb vin line, i did this several times without problems
https://www.pjrc.com/teensy/external_power.html

I did a quick and dirty run of your code with no hardware. It is hooked up to Logic Analyzer. I added a digitalToggleFast(13) as well as set pin 13 to output, So I could easily check to see if
the code was hung or not.... Not:

View attachment 29931

I believe that at least for the spec that I looked at, the chip clock could run at 25mhz in the 3.3v range, so should be close as shown in capture.

The way they do the latch pin may be fine, when I did it, I used code that more closely matched the document:
View attachment 29932

Something like:
Code:
uint8_t isrReadRegister() {
   uint8_t inputs = 0;
   digitalWrite(ISRClockPin, HIGH);  // preset clock to retrieve first bit
   digitalWrite(ISRLatchPin, LOW);  // disable shifting and enable input latching
   delayNanoseconds(50);
   digitalWrite(ISRLatchPin, HIGH);  // disable input latching and enable shifting
   inputs = shiftIn(ISRDataPin, ISRClockPin, MSBFIRST);  // capture input values
   return inputs;
}
Which gives you an output like:
View attachment 29933

But the current code may work just fine.
i will adapt this code ad set the teensy to 24mhz

If it turns out that the inputs = shiftIn(ISRDataPin, ISRClockPin, MSBFIRST); // capture input values
is too fast, I would then write my own based off of what the shiftin does:

The implementation is like:
Code:
uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
{
	uint8_t mask, value=0;
	for (mask=0x80; mask; mask >>= 1) {
		digitalWrite(clockPin, HIGH);
		if (digitalRead(dataPin)) value |= mask;
		digitalWrite(clockPin, LOW);
	}
	return value;
}
The shift in is actually a wrapper that calls off to two different functions depending on MSB or LSB First.
Btu you could take this code and for example add in delays to slow it down if necessary.
i tought that also tha shiftin was the problem, i will try your code

Hope that helps
You helped me alot one question is the connections from the shift to teensy correct? or can i use any digital pin §i guess not becausde of the clock and miso?

i will try everything this evening and will give you an update, fingfers crossed!
 
kurt im getting lost for shure now

Code:
const unsigned long SamplePeriod = 500;  // sampling period in milliseconds

const uint8_t ISRDataPin = 14;   // connected to 74HC165 QH (9) pin //datapin
const uint8_t ISRLatchPin = 11;  // connected to 74HC165 SH/LD (1) pinMISO0
const uint8_t ISRClockPin = 10;  // connected to 74HC165 CLK (2) pin //clock pin t 35 CS0 10 op41



void setup() {
  Serial.begin(9600);
  while (!Serial);

  // 74HC165 shift register
  pinMode(ISRDataPin, INPUT);
  pinMode(ISRLatchPin, OUTPUT);
  pinMode(ISRClockPin, OUTPUT);
}

void loop() {
  static unsigned long previousTime = 0;
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= SamplePeriod) {

    update();
    //  readShiftRegisters();
    previousTime = currentTime;
  }
}

uint8_t custom_shift()
{
  uint8_t mask, value = 0;
  for (mask = 0x80; mask; mask >>= 1) {
    digitalWrite(ISRClockPin, HIGH);
    delayNanoseconds(50);

    if (digitalRead(ISRDataPin)) value |= mask;
    digitalWrite(ISRClockPin, LOW);
    delayNanoseconds(50);
  }
  return value;
}

uint8_t isrReadRegister() {
  uint8_t inputs = 0;
  digitalWrite(ISRClockPin, HIGH);  // preset clock to retrieve first bit
  digitalWrite(ISRLatchPin, LOW);  // disable shifting and enable input latching
  delayNanoseconds(50);
  digitalWrite(ISRLatchPin, HIGH);  // disable input latching and enable shifting
  //   inputs = shiftIn(ISRDataPin, ISRClockPin, MSBFIRST);  // capture input values
  inputs = custom_shift();
  return inputs;
}

void update() {
  static uint8_t previousInputs = 0;
  uint8_t currentInputs = isrReadRegister();  // read all inputs from shift register
 //  if (currentInputs != previousInputs) {  // print values only if they changed
  Serial.print("Inputs: 0b");  // print all inputs represented as a full byte
  for (int8_t i = 7; i >= 0; i--) {
    Serial.print(bitRead(currentInputs, i));  // print value for each bit

  }
  Serial.println();
  //previousInputs = currentInputs;
  //}
}

i removed the change state so that i could see constantly the bits but no use still all 0
im wondering if i am even using the correct pins
const uint8_t ISRDataPin = 14; // connected to 74HC165 QH (9) pin //datapin
const uint8_t ISRLatchPin = 11; // connected to 74HC165 SH/LD (1) pinMISO0
const uint8_t ISRClockPin = 10; // connected to 74HC165 CLK (2) pin //clock pin t 35 CS0 10 op41


are these the correct pins ore am i messing it up in my confusion?

i played with verious delays and various speeds but still no luck :(
 
kurt im getting lost for shure now

Code:
const unsigned long SamplePeriod = 500;  // sampling period in milliseconds

const uint8_t ISRDataPin = 14;   // connected to 74HC165 QH (9) pin //datapin
const uint8_t ISRLatchPin = 11;  // connected to 74HC165 SH/LD (1) pinMISO0
const uint8_t ISRClockPin = 10;  // connected to 74HC165 CLK (2) pin //clock pin t 35 CS0 10 op41



void setup() {
  Serial.begin(9600);
  while (!Serial);

  // 74HC165 shift register
  pinMode(ISRDataPin, INPUT);
  pinMode(ISRLatchPin, OUTPUT);
  pinMode(ISRClockPin, OUTPUT);
}

void loop() {
  static unsigned long previousTime = 0;
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= SamplePeriod) {

    update();
    //  readShiftRegisters();
    previousTime = currentTime;
  }
}

uint8_t custom_shift()
{
  uint8_t mask, value = 0;
  for (mask = 0x80; mask; mask >>= 1) {
    digitalWrite(ISRClockPin, HIGH);
    delayNanoseconds(50);

    if (digitalRead(ISRDataPin)) value |= mask;
    digitalWrite(ISRClockPin, LOW);
    delayNanoseconds(50);
  }
  return value;
}

uint8_t isrReadRegister() {
  uint8_t inputs = 0;
  digitalWrite(ISRClockPin, HIGH);  // preset clock to retrieve first bit
  digitalWrite(ISRLatchPin, LOW);  // disable shifting and enable input latching
  delayNanoseconds(50);
  digitalWrite(ISRLatchPin, HIGH);  // disable input latching and enable shifting
  //   inputs = shiftIn(ISRDataPin, ISRClockPin, MSBFIRST);  // capture input values
  inputs = custom_shift();
  return inputs;
}

void update() {
  static uint8_t previousInputs = 0;
  uint8_t currentInputs = isrReadRegister();  // read all inputs from shift register
 //  if (currentInputs != previousInputs) {  // print values only if they changed
  Serial.print("Inputs: 0b");  // print all inputs represented as a full byte
  for (int8_t i = 7; i >= 0; i--) {
    Serial.print(bitRead(currentInputs, i));  // print value for each bit

  }
  Serial.println();
  //previousInputs = currentInputs;
  //}
}

i removed the change state so that i could see constantly the bits but no use still all 0
im wondering if i am even using the correct pins
const uint8_t ISRDataPin = 14; // connected to 74HC165 QH (9) pin //datapin
const uint8_t ISRLatchPin = 11; // connected to 74HC165 SH/LD (1) pinMISO0
const uint8_t ISRClockPin = 10; // connected to 74HC165 CLK (2) pin //clock pin t 35 CS0 10 op41


are these the correct pins ore am i messing it up in my confusion?

i played with verious delays and various speeds but still no luck :(
 
Which digital IO pins on the Teensy you use, is totally up to you. They are doing nothing special.

As for hookup to the shifter connections, I could not really see anything in your photo. That is the chip was more or less buried under the wires.
You need to map/trace out what is connected to each of the pins on the chip:

The document you showed has a typical hookup:
Screenshot.jpg

In that diagram, I would probably GND the pin 10 (in from other shift register).
But other important things are things like PIN 15 - they have it grounded, as if it goes high, it will disable the clock pin. So if it is floating, its potluck on what it will do.

But Again +5v on any Teensy 4.x board can cause damage to the board!

Disconnecting VIN from VUSB will cause the board to not be powered when it is plugged into USB. So you need something hooked up to VIN. Something in the 3.6v to 5.5v range
But this does not mean that any of the IO pins are now able to handle anything above 3.3v.

So if you power the level shifter with +5v, then I am assuming it will output +5v signals on it's output pins (7 and 9). SO if pin 9 goes directly to the Teensy pin 14 and it does output +5v then it may no longer work, or worse the whole processor could be killed. If you really do need to handle +5v signals coming into the level shifter, then you may need to go through some TTL level conversion down to 3.3v
 
hi kurt

ok i need to apologise to you
i was so certain about my 5volt topology so in a last try for result i ripped out the 5v , put a usb in that also delivers power, connected te teensy 3v and ground to my breaboard

and YES SIR IT WORKS now, im deeply sorry about the external 5v but i was soooooo shure about it xD

maybe i should tell you why i want to use the 5v now than you will understand why i did this
this project will be part of a bigger one:
- now that one shifter works i will add 2more shifters in serial becasue i have a need of 36 buttons (12 per shifter; tell you later why 12 and not 16)
- this would be just serial attaching and make a little program changes but this should workout quiet easily!
- next up i will change the usb from serial to serial+ midi because the buttons have to control my lightning desk through showcockpit to chamsys; i already have this code so no problem
- next up the ethernet magjackkit wil be connected and i will make a small webserver (for the next step)
- the final step is my probable 5volt thingie=

i want to add 18 oled screens 1.3 inch via i2c and a TCA9548Amux (6 screens per mux)
https://cdn.shopify.com/s/files/1/1...-Delivery_Vertriebs_GmbH_rev.pdf?v=1606164520
the tca can operate on 3.3 volt so thats no problem
also the oleds can operate on 3.3 at max 11ma

so 11ma X 18 = 198 ma and the 3.3 pin is rated 250 max
When running at 600 MHz, Teensy 4.0 consumes approximately 100 mA current
so im questioning adding the buttons the muxes and the shifters = i think this will overthrown the 250ma safe limit

what do you think about this?
i used a 5v power supply at 11amp because it ways laying around here and teensy said in their article it should be no problem, but actually like we see it does whit the shifters

second question; would it be an idea though to use the external 5volt exclusivle for only the poweringen of the oled screens only, maybe with a TTL level conversion to 3.3? and what about the ground than? do i need to combine the teensy or oled ground together to the power supply ground or will this not be necessairy?


and maybe also a questions by you know=
why 12 buttons and 6 oleds per shifter/mux == because im designing 3 pcbs that are identical and will be paired next to ech other , mostly because of the costs at jlc (1 plate of 30X20cm = 40 dollar,n 5 plates of 1/3 this dimensions = 10 dollar)

many thanks for your help you where really hulpfull for me already!
if you cvould answer on my thoughts i writed here that would be wonderful!!
 
On some of my own board (which I only do for my own fun), If I believe I am going to need more current than the Teensy can supply, I simply use an external Voltage Regulator or DC/DC converter. I am a retired software guy, so EE types may have better suggestions.

In some of my boards I use DC/DC converters from Pololu:
I figure out what current I think I might need, and pick one. For example for up to 1 amp, I might use: https://www.pololu.com/product/2830

Now how I might hook it up depends on things like, Am I mainly running this from USB power (+5v) or am I using it with some external power source, llike a 3s lipo, in which case I might have two converters like this, one for +5v and another for 3.3v that both connect to VIN (the battery). If USB, may connect directly to VIN as well.

Other times, I might go cheaper simple route, where I use a simple VR connected up to +5v, so not much of a voltage drop. I have used a few different ones in the past, not sure which ones are best. Currently probably the best ones are the ones you can actually get.
One of the last boards, I did was a carrier board for MicroMod and I used one of these: https://www.digikey.com/en/products...N4IgTCBcDaIIIAUDsBmAbARgMIFoUoGUEcMUACEAXQF8g

Note: more or less same part as Sparkfun uses in their carier boards, other than different form factor. In a previous version I had a different form factor part, but they were out of those, when I was going to have PCBWay fabricate and paritally assemble the carrier board.

Good lcuk
 
Back
Top