74hc595 cascading question

nickpatts

Member
Hi All,

just a quick question thats been puzzling me, when you are cascading two or more 74hc595's together if I read it right the last one in the cascade gets its data first and so forth till the first one gets its data then you pulse the latch for the resuts, what i wanted to know is how does the first chip know its got another chip connected to it and needs to pass the data straight through to the next one?. and also then one chip is used on its own, whats to stop it passing the data out its pin 9 to nowhere. I've search the forum for all 595 topics and nothing mentions how the cascade part works, i would of thought you would of had to tie the pin 9 low or high so the chip knows its the last?.

Kind Regards.


Nick
 

moxhamj

New Member
If you consider some HC595s numbered 1, 2 and 3. Number 1 is the first one.

Feed the data into number 1. When 8 bits have gone into number 1, they start coming out the other end. These then feed into number 2. And so on.

So it is the first one in the cascade that gets the data first, not the last one. But that data ends up being passed along to the end of the cascade, so it ends up in the last chip.

And if you only have one chip, the data comes out of pin9 but pin 9 doesn't have to be connected to anything. Outputs of any chip never have to be connected to anything. Sometimes you do and sometimes not but they don't have to be. It is only inputs that need to be definitely high or low.

Keep asking questions!
 

westaust55

Moderator
cascading 74hc595's

Additionally, when transferring the data from the PICAXE to the 595 shift registers, you need to be careful about the order in which the bits are sent.

A byte has 8 bits from most significant bit (bit 7) to least significant bit (bit 0)

A word has 16 bits from most significant bit (bit 15) to least significant bit (bit 0)

A word comprises two bytes so for example w0 = b1 (high byte) and b0 (low byte)

If you are going to transfer w0 into two 595 shift resisters and lests say you deem 595 No 2 as the high byte and595 No 1 as the low byte then as Dr Acula says the bits first go thru 595 No 1 and the top 8 are shifted through into 595 No 2.

For some PICAXE you may need to “bit bang” to do this transfer - a method shown in many 595 related posts.
The SHIFTOUT command (works at least for the X1 chips) – see manual 2 page 159.

With this command to get the bits in the right order in the right 595 IC outputs you need to select the right mode of shifting. In the example I give above, you would need to use the mode 1 (using the keyword MSBFirst_L)
 

nickpatts

Member
Thank you for your responce, after reading the datasheet it does mention the data cascading after the 8th bit sent, made it much clearer in my head now.

I was getting confused as all the chips in a cascade are tied on the shift/latch clock, so I thought as your sending data to the first chip the second chip will still be shifting registers on the first run but only getting a low data signal. I presume when the 8th bit is sent and it starts outputting to next chip it sends a signal to clear the next chips registers?.

Nick
 

inglewoodpete

Senior Member
Normally, you'd connect 3 lines to the PICAXE. Data, Shift Clock & Store Clock.

The Shift Clock connects to the SH_CP pin on both (all) chips. Similarly, the Store Clock will connect to the ST_CP pin on both (all) 595s.

The Data line is connected to the DS pin of the first chip. The Q7' ouput from the first 595 will connect to the DS pin of the 2nd 595.

Further 595s can be cascaded with the 2 clock leads connecting to the appropriate pine on each chip and the Q7' cascading to the DS pin of the nect chip in the chain.

It is not normally necessary to use the reset pin on the chips. Once all bits have been clocked into the 595s (Usually 8, 16, 24,.... bits) using the DS and SH_CP pins, the ST_CP line is strobed high to latch the data to the output pins 0-7. Reset and Enable Output pins should be held high.

Remember, the shift resister simply takes a digital copy (0 or 1) of whatever its 'sees' on its data pin whhen the clock pin rises from 0 to 1. On the 9th rising edge of the clock, Q7' will be 'showing' the first sample value to the data pin of the 2nd 595, which will 'record' that value.

Download the 74HC595 datasheet and start experimenting!
 
Last edited:

nickpatts

Member
cheers guys, got out the breadboard and sucessfully have 4 hc595's cascading together via a 8m picaxe with led outputs on the 32 channels, seeing it in front of me really helps understanding what the shifting data is doing in what directing.

:)

one happy picaxey


Nick.
 

Haku

Senior Member
I needed 16 more outputs to drive LEDs so I got some 74HC565N's, thanks for the code and the wiring diagram in the link Dr_Acula gave, I managed to get a couple of chips running fine.

However I'm having trouble with one aspect, the shift registers seem to remember the last output they had when they get switched off, so when it powers back up it causes LED(s) to light up which I don't want, how can I always make it so the outputs are off when it (picaxe+shift registers) powers up?
 

westaust55

Moderator
If you have the LEDs from the 595 outputs to 0volts then they should be off when the 595 is first energized.
Suggest post your schematic and code.
Alternatively search for a past 3 digit display project I posted here with diagrams photos and program code as an example
 

william47316

New Member
i have multiplexed multiple shift registers to drive 64 outputs for my 8 digit displays, if you use common data lines and clock lines and dont need the output enable you can use 4 outputs to drive the two registers, using one for clock, data, and storage clock one and two
 

westaust55

Moderator
The problem as I read it, is not that there is a desire to have lots of extra LED’s which could benefit by multiplexing, but that Haku is having problems with the 74HC595 seemingly having the outputs energised at power-up of the circuit.

We need to know how Haku has connected his LED’s to the ‘595 and what his program is doing (is it quickly setting up the outputs at the start of the PICAXE program.
Hence the suggestion for Haku to post the schematic and program code for the project.
At this moment we are waiting for Haku to return with more information.
 

Michael 2727

Senior Member
Shift Registers and Counters can quite often pick up Garbage or Unwanted noise
which turns into false data or digits on powerup. Some inputs are very fast, MHz +.
Not all chips power up at the same rate.

The simplest way to avoid this is to insert blank data or Zeros to clear everything
after a powerup is done.
 

Haku

Senior Member

Code:
#picaxe 18m2
#no_data

symbol clk=c.0
symbol stor=c.1
symbol ser=c.2

output clk,ser,stor

symbol pz=20

pause 2000

do
 b0=%00000001:gosub SingleHC595Output:pause pz
 b0=%00000010:gosub SingleHC595Output:pause pz
 b0=%00000100:gosub SingleHC595Output:pause pz
 b0=%00001000:gosub SingleHC595Output:pause pz
 b0=%00010000:gosub SingleHC595Output:pause pz
 b0=%00100000:gosub SingleHC595Output:pause pz
 b0=%01000000:gosub SingleHC595Output:pause pz
 b0=%10000000:gosub SingleHC595Output:pause pz
 b0=%10000000:gosub SingleHC595Output:pause pz
 b0=%01000000:gosub SingleHC595Output:pause pz
 b0=%00100000:gosub SingleHC595Output:pause pz
 b0=%00010000:gosub SingleHC595Output:pause pz
 b0=%00001000:gosub SingleHC595Output:pause pz
 b0=%00000100:gosub SingleHC595Output:pause pz
 b0=%00000010:gosub SingleHC595Output:pause pz
 b0=%00000001:gosub SingleHC595Output:pause pz
loop


SingleHC595Output:
 low clk
 low stor
 for b2=0 to 7
  if bit0=1 then:high ser:else:low ser:endif
  pulsout clk,1
  b0=b0/2
 next
 pulsout stor,1
 return
It's breadboarded, they're white LEDs on the outputs with a rating of 3.4v so I didn't bother with resistors as it's only in test phase right now, the final outputs will go to transistors that will drive 3 LEDs each.

I'm thinking a possible work around would be to use another Picaxe line to control a mosfet (or darlington pair) that turns off the GND line to the LEDs but not the shift register, so the Picaxe can set the shift register's outputs to off before switching the mosfet (or darlington pair) on ?
 
Last edited:

hippy

Ex-Staff (retired)
Simply clocking out eight zero bits would seem to be the easiest way as westaust55 suggested. There are a number of ways to do that, the easiest being to set b0=0 then gosub SingleHC595Output at the start of your program.

You can also optimise for faster speed ...

SingleHC595Output:
low clk
low stor
for b2=0 to 7
pinC.2 = bit0
pulsout clk,1
b0=b0/2
next
pulsout stor,1
return

SingleHC595Output:
low clk
low stor
pinC.2 = bit0 : pulsout clk,1
pinC.2 = bit1 : pulsout clk,1
pinC.2 = bit2 : pulsout clk,1
pinC.2 = bit3 : pulsout clk,1
pinC.2 = bit4 : pulsout clk,1
pinC.2 = bit5 : pulsout clk,1
pinC.2 = bit6 : pulsout clk,1
pinC.2 = bit7 : pulsout clk,1
pulsout stor,1
return
 

Andrew Cowan

Senior Member
74HC595s also have a clear pin - if you don't want inputs initially on, you can use that to set everything to zero on power up.

Andrew
 

Haku

Senior Member
Thanks for the optimised code hippy, but the speed of the program operation isn't an issue and I'll have two shift registers in series for the final version so the b0=b0/2 will become w0=w0/2.

Andrew, I see 'master reset' and 'output enable' pins, do you mean the 'output enable' pin?
 

hippy

Ex-Staff (retired)
He probably means "master reset" but I haven't read the data sheet - how does that datasheet describe master reset as working ? Does it say it clears the outputs ?
 

Andrew Cowan

Senior Member
I have a circuit that uses it - it uses pin 10 (master reset). Pull that low, and all the shift registers get set to zero. You then need to clock the latches to transfer that to the outputs.

Andrew
 
Last edited:

John West

Senior Member
I like the idea of using a FET to kill the common power line. Then there's never any random garbage on the displays, and you can blink them or use the FET with PWM as a dimmer as well.

But then, I'm a hardware guy (who has lots of FETs lying around.)
 

Haku

Senior Member
I tried hooking a pushbutton to the 'output enable' with a pulldown resistor to GND so it gets 3.3v when pushed, when I press the button the shift register's outputs turn off even though it's still running and accepting data from the Picaxe.

I think I can figure it out from here, cheers guys :)


Edit: great point there John, thanks, I'd totally forgotten about the brightness issue as I'll be running 48 white LEDs all at once and at full brightness it'll suck the life out of the batteries. I wonder if the 'output enable' line will take a PWM signal, hmm...
 
Last edited:

techElder

Well-known member
Put a resistor from 'master reset' to the +V. Put a capacitor from 'master reset' to GND.

There is an RC time constant that will allow proper operation in your case. I don't have time to figure it right now.

Basically, the capacitor is fully discharged before power is applied to the circuit. The resistor charges the capacitor, but at a slow enough pace that the pin is still digitally LOW when the circuit powers up fully. This provides the LOW for RESET function at power up. After a short period (depending on RC), the capacitor is fully charged and effectively out of the circuit.
 

Haku

Senior Member
Problem now sorted.

I put a pullup resistor from +V to the 'output enable' line, and connected a PWM of the Picaxe to the 'output enable' line - I can dim the output using PWM :)

Now when the circuit is powered up the output of the shift register is always off until I issue a "pwmout" command, leaving the Picaxe as much time as it needs to set the output lines of the shift register to whatever it needs to be before turning its outputs on.
 

hippy

Ex-Staff (retired)
@ Haku : Novel solution and useful functionality - well done. You'll need pull-down / pull-up resistors to default any drive transistor bases if added for when the outputs are disabled ( high-impedence / floating ) but should be easy enough to add.
 

westaust55

Moderator
@Haku,

If you have a look at the scheamtic diagram for the cascaded 74HC595 project I referenced earlier. See post 2 here:http://www.picaxeforum.co.uk/showthread.php?t=13687
you will see that I have a pull up resistors for the CLR (Clear) line and two pull-down resistors for the output OE (Output Enable) and Latch signals.

These hold the 74HC595's stable with outputs off until the PICAXE boots and the program gains control.
 

Haku

Senior Member
Thanks hippy, I wouldn't have spotted that (the floating input to the transistor when the output is off) without you mentioning it, I'll be sure to do some breadboard testing before breaking out the soldering iron.

westaust, thanks also, this thread is one of two I found when searching for shift registers & Picaxes, it looked easy to impliment so I bought some, got as far as I could then called for help on the last bit.

Here's my revised setup that uses the one of the PWM outs of the 18m2 for dimming the outputs, it works great with one shift register and no doubt shouldn't be a problem to add more into the mix:



and modified code to make sure the outputs are cleared before the main program starts:
Code:
#picaxe 18m2
#no_data

symbol clk=c.0
symbol stor=c.1
symbol ser=c.2

output clk,ser,stor

symbol pz=20

b0=0:gosub SingleHC595Output

pause 2000

pwmout B.6, 99, 200 ' 50% brightness

do
 b0=%00000001:gosub SingleHC595Output:pause pz
 b0=%00000010:gosub SingleHC595Output:pause pz
 b0=%00000100:gosub SingleHC595Output:pause pz
 b0=%00001000:gosub SingleHC595Output:pause pz
 b0=%00010000:gosub SingleHC595Output:pause pz
 b0=%00100000:gosub SingleHC595Output:pause pz
 b0=%01000000:gosub SingleHC595Output:pause pz
 b0=%10000000:gosub SingleHC595Output:pause pz
 b0=%10000000:gosub SingleHC595Output:pause pz
 b0=%01000000:gosub SingleHC595Output:pause pz
 b0=%00100000:gosub SingleHC595Output:pause pz
 b0=%00010000:gosub SingleHC595Output:pause pz
 b0=%00001000:gosub SingleHC595Output:pause pz
 b0=%00000100:gosub SingleHC595Output:pause pz
 b0=%00000010:gosub SingleHC595Output:pause pz
 b0=%00000001:gosub SingleHC595Output:pause pz
loop


SingleHC595Output:
 low clk
 low stor
 for b2=0 to 7
  if bit0=1 then:high ser:else:low ser:endif
  pulsout clk,1
  b0=b0/2
 next
 pulsout stor,1
 return
 

westaust55

Moderator
@Haku,
If you can afford one extra output from the 18M2, then use it to control the SRCLR signal (active low) on pin 10 of the 74HC595.

In that way you can clear all bits in all 74HC595s simultaneously with just two commands.
PULSOUT SRCLR, 1
PULSOUT STOR, 1​

At the start/initialisation add you will also need
HIGH SRCLR​

Then when you have two or more 74HC595’s cascaded together, rather than having to clock out a low bit 8, 16 , 24, or more times in a loop just the two instructions are needed.
 

tmfkam

Senior Member
I needed to drive three 74HC595 chips, individually switching one output on, sequentially down all 24 outputs. I couldn't see easily how using the 'shift left' method would cope with more than 16 bits. I decided that after trying to extend the 'shift left' into 24 bits, which did work, but looked very messy. It would be easier to run everything inside a couple of nested For...Next loops.

The code as shown simply sends a single 'on' command down all the 24 (3 chips cascaded) 74HC595 outputs before returning to the start. For more complex output patterns it probably wouldn't be easily extended, but as it is changing StringLen to match the total number of outputs (32, 48, 56 etc.) would be all that is required to add more 74HC595s and thus more outputs.

Code:
#No_Data
#Picaxe 14M2

symbol Clk  =b.0
symbol Stor =b.1
symbol Dat  =b.2
symbol Ena  =b.4
symbol Sens =pinb.5
symbol Led8 =c.0
symbol Led16=c.1
symbol Led24=c.2

'b.0 Clk  Pin 13 14M2 to Pin 11 74HC595   	SCK     (Serial Clock)
'b.1 Stor Pin 12 14M2 to Pin 12 74HC595   	RCK     (Latch)
'b.2 Dat  Pin 11 14M2 to Pin 14 First 74HC595   SER     (Serial Data)
'b.4 Ena  Pin 9  14M2 to Pin 13 74HC595   	/Enable (PWM 400kHz) 
'Pin 10 74HC595 to +5V
'Pin 9  First  74HC595 to Pin 14 Second 74HC595
'Pin 9  Second 74HC595 to Pin 14 Third  74HC595

symbol DataValue  =b0
symbol DataLength =b1
symbol StopLed    =b2
symbol PulseLen   =1
symbol StringLen  =b3


Low Clk
Low Stor
Low Dat

SetFreq M32

pwmout B.4, 19, 39 ; 400000Hz at 50% @ 32MHz
Let StringLen = 24

for DataLength = 1 to StringLen 'Send a string of null data to turn all off
	Gosub OutLo
next DataLength
pulsout Stor, PulseLen     'latch the output register

Do
	GoSub DataBuild
Loop


DataBuild:
For DataValue = StringLen to 1 Step-1 'output 1 to 24
	for DataLength = 1 to StringLen 'Send a string of data 
		If DataLength = DataValue Then
			GoSub OutHi
		Else
			Gosub OutLo
		EndIf
	Next DataLength
	PulsOut Stor, PulseLen     'latch the output register
Next DataValue
Return

OutHi:
	High Dat      'set output bit to one
	pulsout Clk, PulseLen   'clock the shift register
	Low Dat
Return

OutLo:
	Low Dat       'set output bit to zero
	pulsout Clk, PulseLen   'clock the shift register
Return
This does send all 24 bits, every time so it isn't particularly quick, but did what I wanted.

I needed to drive two pairs of Infra Red transmitters and receivers, each having 24 LEDs / sensors and each having three 74HC595s. These are used to detect obstructions across a chute. The Sens input on the PicAxe (pinb.5) is connected to the output of the receiver side of the pair to detect the obstruction. In my case it uses the LEDs on C.0, C.1, and C.2 to indicate which LED is blocked (faulty) to allow me to replace the faulty TX/RX pairs. This code isn't shown.
 
Top