Go Down

Topic: 74HC595 Shift Register Examples.. (Read 7414 times) previous topic - next topic

Tchnclfl

Well, I bought myself some 74HC595 shift registers, and they won't be here for a while (stupid Thailand ;)), so I thought I'd read up on the coding for Shift Registers.  I'm having some trouble figuring out what this example (listed on the Playground page for shiftOut() ) is doing.  It has some comments, but never really explains the syntax of shiftOut().  Here's the example:

Code: [Select]
//**************************************************************//
//  Name    : shiftOutCode, Hello World                                 //
//  Author  : Carlyn Maw,Tom Igoe, David A. Mellis  //
//  Date    : 25 Oct, 2006                                      //
//  Version : 1.0                                               //
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : to count from 0 to 255                            //
//****************************************************************

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;



void setup() {
 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);
 pinMode(clockPin, OUTPUT);
 pinMode(dataPin, OUTPUT);
}

void loop() {
 //count up routine
 [glow]for (int j = 0; j < 256; j++)[/glow] {
   //ground latchPin and hold low for as long as you are transmitting
   digitalWrite(latchPin, LOW);
   [glow]shiftOut(dataPin, clockPin, MSBFIRST, j); [/glow]  
   //return the latch pin high to signal chip that it
   //no longer needs to listen for information
   digitalWrite(latchPin, HIGH);
   delay(1000);
 }
}


My questions deal with the highlighted segments.

1. I understand "For", but my question about that loop is this:  Is it PWMing a value of 0-256 to be sent to all of the LEDs?  I'm not quite sure what it's doing.

2. In the actual shiftOut() section, can someone explain to me what each of those arguments is?

That's it for now, but I'm sure answers will just arise more questions ;D!

Osgeld

#1
Dec 13, 2009, 02:06 am Last Edit: Dec 13, 2009, 02:11 am by Osgeld Reason: 1
I dont know the setup your running and therefore do not know EXACTLY what the for is ... well um for... But! here is the low down on shift registers so you can figure the code out

Serial > parallel shift registers like the 595 are simple in operation

you have a clock line, you have a data line, and in the case of the 595 you have a latch line (which is really insignificant in a lot of cases but lets go with it)

How you would write to a s>p shift register without a handy function is as follows

set the data line to 1 or 0
toggle the clock from low to high
whatever is on data shows up on bit 8
set the data line to 1 or 0
toggle the clock from low to high
whatever is on data shows up on bit 8,whatever was on bit 8 is now on bit 7

repeat 6 more times

if you have a latch (which the 595 does) turn the latch on to show the output results all at once (like flipping a light switch)

the shiftOut(function) does this for you, the arguments are as follows

data pin, clock pin, MSBFIRST or LSBFIRST, byte (or i guess you can send words too when using 16 bit SR's or daisy chaining them)

ok MSB and LSB

lets say you have B01010101

On msb first you would send bit 8 first, the leftmost bit, on the shift register it would show up as

B10101010

LSB first you send the rightmost bit first, the resulting shift register output would be B01010101

which one you use depends on how you process data in your software, sometimes logically its easier to do one or the other and just change which way you send it out
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Tchnclfl

#2
Dec 13, 2009, 02:19 am Last Edit: Dec 13, 2009, 02:23 am by Tchnclfl Reason: 1
Wow thanks for clarifying.  I'm still a bit confused, but I hope to work it out.  Any chance of decoding what exactly the sketch I showed does? :P.

I'll take a gander at it with my new knowledge (;D) to see if I can figure it out..

[edit]Nope still not really sure haha.  Looks to me like it's sending a value of 1-256 to each byte in a pattern from 8-1 :P.  Confusing as hell :P...

Here's a question though:  If I were to want to show all LEDs at the same time (assuming there was enough power), would I just shiftOut() 256 (doing the whole clock routine each time) 8 times), and then switch the latch back high?[/edit]

Osgeld

if you shift out 256 (B11111111) the shift out function sends each byte one at a time according to LSB or MSB toggles the clock and moves on, its sending 8 bits in 1 function

then you switch the latch to display output
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Osgeld

also be sure to look over my post on instructables about a 74HC164, wich is the same as a595 but without a latch for some basic example projects... schematic pinouts wont be much use, but the code should be easly transportable

http://www.instructables.com/id/The-74HC164-Shift-Register-and-your-Arduino/
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Tchnclfl

#5
Dec 13, 2009, 04:15 am Last Edit: Dec 13, 2009, 04:21 am by Tchnclfl Reason: 1
I think I may finally be getting to something.

You send the shift register your data in binary form (in an example I saw, it's in HEX form, but that worked too).  When it receives the data (and you initiate the Latch Pin, it displays what data it received.

For example, if you send "10101010" to a Shift register, having 8 LEDs attached to it in the correct order, then they will be like this down the line: "ON--OFF--ON--OFF--ON--OFF--ON--OFF"?

Also, in the example I saw (http://arduino.cc/en/Tutorial/ShftOut13), they omitted the MSBFIRST/LSBFIRST from the shiftOut() command.  What happens when you don't use one of those?  :-?


Also, what formats of data can you send to the shift register?  So far I've seen Decimal Bytes (0-255) and HEX, but apparently Binary works too if you can get the Arduino to use it.

[edit]I'm just realizing that the tutorial I saw is several years old (came from 2006), and was apparently created before shiftOut() was part of the Arduino language (they just used it as a function in the bottom of the code).  I Think they used MSBFIRST based on the comments in that function.[/edit]

Osgeld

#6
Dec 13, 2009, 04:26 am Last Edit: Dec 13, 2009, 04:31 am by Osgeld Reason: 1
Quote
I think I may finally be getting to something.

You send the shift register your data in binary form (in an example I saw, it's in HEX form, but that worked too).  When it receives the data (and you initiate the Latch Pin, it displays what data it received.

For example, if you send "10101010" to a Shift register, having 8 LEDs attached to it in the correct order, then they will be like this down the line: "ON--OFF--ON--OFF--ON--OFF--ON--OFF"?


yep

Quote

Also, in the example I saw (http://arduino.cc/en/Tutorial/ShftOut13), they omitted the MSBFIRST/LSBFIRST from the shiftOut() command.  What happens when you don't use one of those?  Huh


I dont know I have to assume thats from a older version that did not have that option, Ive only been on the scene since November

Quote

Also, what formats of data can you send to the shift register?  So far I've seen Bytes (0-255) and HEX, but apparently Binary works too if you can get the Arduino to use it.


you can send pretty much anything, but the arduino is going to send it 1 bit then 1 clock flip at a time

think of it as a digital port expander
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Tchnclfl

#7
Dec 13, 2009, 04:40 am Last Edit: Dec 13, 2009, 04:42 am by Tchnclfl Reason: 1
I fully get it now.  You have no idea how frustrating it is to be on the verge of understanding but not really getting it :P.  I feel like I could teach a class on them now ;D!

But thanks for helping out  ;)!

Here's a final question:  How can you send the information as raw binary?  Every time I see examples they always use either Decimal or HEX to send it (eg, using 0xAA instead of 10101010).  It'd be so much more intuitive to use 1s and 0s.  Is there any way to store them in a variable that can be read correctly by shiftOut()?  Or would it simply work to just store the binary in a variable? :P

thoed

You can use B00000001, B00001010  etc. You just need a Capital B.

Also, did you see the shiftOut example in the Earthshine Design manual? Personally I like their example alot more.

http://earthshinedesign.co.uk/ASKManual/ASKManual.pdf

Tchnclfl

#9
Dec 13, 2009, 05:12 am Last Edit: Dec 13, 2009, 05:14 am by Tchnclfl Reason: 1
Hmm I hadn't noticed it, but I do have that manual already saved to my computer.

So if I were to want to save something as a binary number into the variable, "sequence", would it be:

Code: [Select]
byte sequence = B10101010

?

And then to use it in shiftOut(), would the following work?:

Code: [Select]
shiftOut(dataPin, clockPin, MSBFIRST, sequence)

Osgeld

#10
Dec 13, 2009, 05:13 am Last Edit: Dec 13, 2009, 05:17 am by Osgeld Reason: 1
byte blah =B11111111;

yes
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

evolutionxbox

URGENT

I have been using the ShiftOut Tutorial for the 74HC595, BUT I don't have a capacitor.

Do I need one?

PaulS

According to the tutorial:

Quote
Notice the 0.1[ch65533]f capacitor on the latchPin, [glow]if you have some flicker when the latch pin pulses you can use a capacitor to even it out.[/glow]


No, you don't NEED one, but you might WANT one.
The art of getting good answers lies in asking good questions.

Grumpy_Mike

#13
Jan 09, 2010, 12:40 am Last Edit: Jan 09, 2010, 12:51 am by Grumpy_Mike Reason: 1
That is a piece of bad advice. You don't need one if you program it up correctly. It could be that bad wiring caused noise pickup but there is no reason to have a capacitor slowing down the latch input. If noise pickup is a problem then a pull down resistor would be much better.

However you should put one across the power supply to the shift register, that's decoupling.

Osgeld

thanks grumpy mike, you just gave me a solution that I already knew but did not piece together
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Go Up