ZappBots wishes a warm welcome to everyone.

Especially our visitors from
Savage///Circuits and Parallax!!!

Please take a look our new forums, we hope you register and become an active user.

The ZappBots forums are being run on phpBB 3.1.4 software which is different then the vBulletin used on the Savage///Circuits and Parallax forums.
Therefore, things look and work a little different, especially creating posts and uploading images / attachments.

The important thing is we have a place toTalk, Learn and Showcase our projects.

74HC595 Shift Register Demo [SPIN] [Propeller]

This is the place to talk about Parallax Propeller Chip and Boards
User avatar
Posts: 403
Joined: Tue May 12, 2015 2:43 pm
Location: Palmyra, NY

74HC595 Shift Register Demo [SPIN] [Propeller]

Postby Savage///Circuits » Mon May 25, 2015 7:38 pm

Originally posted on the Savage///Circuits Forums
March 1st, 2014, 12:35 PM
by Chris Savage

It's pretty easy to access a shift register on a BASIC Stamp Module. You just use SHIFTIN or SHIFTOUT and you have all kinds of paremeters to select the bit order and clocking, etc. Add to that tons of code examples and accessing these flexible devices is made pretty easy. There is no SHIFTIN / SHIFTOUT equivalent on the Propeller. There are several objects out there for handling various shift registers, however many of them are meant to be flexible like the PBASIC command or specific to a particular chip or device. This can sometimes make it hard for a beginner to understand what is actually happening and how to access the shift register from SPIN. So let's simplify things. First of all, as I said there is no equivalent of the PBASIC SHIFTIN / SHIFTOUT commands. We'll have to get old-school and bit-bang the shift registers or as some call it, bit-twiddle. The key to understanding how the shift register works is by looking at the timing diagrams found in the datasheets, however I know for some people these read like hieroglyphics, so we're instead going to take a look at what is needed to access the common 74HC595 shift register, which can run at both 3.3V and 5V.

The 74HC595 is a Serial to Parallel shift register, which means that it takes a serial input and creates a parallel output. More specifically it takes 8 bits in serially (MSB first) and then latches the 8 bits onto an 8-bit parallel output port. The 74HC595 is an SPI or Synchronous Serial device in that it has a clock that synchronizes the bits in the serial data. The LATCH line (sometimes called STORE) on the 74HC595 writes the data in the shift register to the outputs once all the data has been shifted into the device. All 8 bits are written at the same time. This prevents the outputs from rippling during the shift. The 74HC595 is a great way to add additional outputs to your microcontroller in groups of 8 using only 3 pins on your microcontroller. Since additional groups of 8 do not require any additional I/O pins on the microcontroller you can add many more chips in a daisy-chain fashion increasing your available outputs.

Okay, say we have a bunch of LEDs or other outputs we want to control but we don't want to eat all our I/O pins up on the Propeller chip. Since these LEDs only ever need to be on/off we can use the 74HC595 to control 8 (or more) LEDs with just 3 pins from the microcontroller. Moreover the code to do it is extremely simple, consisting of a very small PRI method we can call to do all the work for us. We need only put our 8-bit data into a variable called value and call the method. See the first code attachment below, 74HC595_Demo.spin. This is a full running demo that counts from 0-255 (and repeats) and sends these values to the shift register which, in my circuit is connected to 8 LEDs on the Propeller Professional Development Board (PPDB). So let's explore how this code works and the important parts you would need to add to your own code to accomplish this task. As a note I have line numbers enabled in the Propeller Tool so I can refer to parts of the code by line number. A photo of my board is shown below.


Looking at the code lines 29-31 are the constants for the I/O pins connected to the various signals on the 74HC595 (see the attached schematic for this demo). You don't have to use these pins, but these are the ones I used. In line 36 in the VAR section I have declared the variable value as a long, however we'll only be using 8 bits of it. I could have used a byte variable, however this code can be easily expanded to handle 32-bit values, so we'll leave it as a long. Again you could use any name you like, I just chose value. For the 74HC595 all 3 signal pins are inputs, so the I/O pins on the Propeller chip must all be outputs as shown in line 41. Line 44 in the Main PUB is where I am sending the data to the writeHC595 method. Now let's look at lines 54 through 64 which makes up the actual method used to send data to the shift register.

In line 54 we're receiving a parameter, in this case the data stored in the global variable value as shown in line 44 being passed. This data will be stored locally in bits which is a parameter variable which now contains a copy of what was in value when the method was called. Since this is a long variable and we're using only 8 bits we need to rotate the bits 24 positions to the left to align the MSB as shown in line 56. The reason is that in the loop we're going to rotate the bits into the LSB position one at a time starting with the MSB. The bit currently in the LSB will be copied the 74HC595 Serial Input.

Let's say we have the value %11000011 to send out. Since the variable is actually a long it will look like this internally: 00000000000000000000000011000011

When line 56 executes bits will now look like this: 11000011000000000000000000000000

Now when the bits are rotated left they will end up one at a time in the LSB position starting with the MSB. Line 58 is a repeat loop that will execute 8 times (once for each bit we want to shift). Line 59 actually puts each bit into the LSB position and writes it to the 74HC595 Serial Input. Essentially what the lines says is that the HC595_DI line equals what is in bits after it is rotated left 1 bit. Since the HC595_DI is a single bit only the LSB gets copied to it. As each bit lands in the LSB position it is clocked out by lines 60-61 which are bringing the HC595_CLK line high then low, essentially clocking (or storing) that bit into the shift register. Once all 8 bits have been clocked in lines 63-64 bring the HC595_LATCH line high and then low, effectively latching what is in the 74HC595 shift register onto its parallel outputs and completing the cycle.

You may have noticed a lack of any waitcnt statements in the code in the CLK and LATCH pulses or even in the loop itself. These delays seem to be in most of the other examples I have seen, however in SPIN they're completely unnecessary since the speed of SPIN will never take us out of the timing specifications of these shift registers. Not even close. The 74HC595 can run up to 100 MHz which translates to a period of 10 ns. If you look at one of my test timing captures below from my Saleae Logic Analyzer you'll see that without any delays in SPIN we're only able to produce a 7 us pulse at a period of 33 us which translates to a maximum clock speed of about 30 kHz. Nowhere near the maximum speed of either shift register. Delays is actually what we typically want to avoid. At 80 MHz the Propeller can execute 1 instruction every 12.5 ns so I would guess that even if we were doing this in PASM we still wouldn't exceed the specification of the 74HC595. I do realize these specifications are at a higher voltage than we're running, however I would still actually test the clock rate before adding delays.


So in order to make use of this code in your own code you need only copy the CON section with the pin assignments, declare a global variable in the VAR section and copy the PRI method. Now you can shift data out easily.

(10.5 KiB) Downloaded 160 times
I'm only responsible for what I say, not what you understand.

Return to “Parallax Propeller Chip and Boards”

Who is online

Users browsing this forum: No registered users and 2 guests