It’s pretty easy to access a shift register on a BASIC Stamp Module. You just use SHIFTIN / SHIFTOUT and you have all kinds of parameters 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 74HC165 shift register, which can run at 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 eight bits in serially (MSB first) and then latches the eight 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 eight 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 eight using only three pins on your microcontroller. Since additional groups of eight 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 you have a bunch of LEDs or other outputs you want to control but you don’t want to eat all your I/O pins up on the Propeller chip. Since these LEDs only ever need to be on / off you can use the 74HC595 to control eight (or more) LEDs with just three pins from the microcontroller. Moreover the code to do it is extremely simple, consisting of a very small PRI method you can call to do all the work for you. You need only put your 8-bit data into a variable called value and call the writeHC595 method. See the code attachment in the resources below. 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 eight 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.
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 eight 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 three 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 writeHC595 method was called. Since this is a long variable and we’re using only eight bits we need to rotate the bits twenty four 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 eight 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 100MHz which translates to a period of 10ns. 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µs pulse at a period of 33µs which translates to a maximum clock speed of about 30kHz. Nowhere near the maximum speed of either shift register. Delays is actually what we typically want to avoid. At 80MHz the Propeller can execute one instruction every 12.5ns, 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.
[IMAGE]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.
[IMAGE][IMAGE]This is my test setup. It was originally set up on a PPDB, but alas, I no longer have one, so I am using the Propeller*****
THIS ARTICLE IS STILL BEING RECOVERED AND RESTORED. PLEASE CHECK BACK LATER! Why is the article being recovered?
Resources
[FILE]74HC165 to 74HC595 Demo 1 [SPIN] – Tutorial
74HC165 to 74HC595 Demo 2 [SPIN] – Tutorial
Discuss this tutorial on Savage///Chats
Released under MIT License
Copyright (c) 2014 Chris Savage – Savage///Circuits.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
PLEASE FEEL FREE TO LEAVE YOUR COMMENTS, QUESTIONS, SUGGESTIONS OR FEEDBACK ON THIS POST.
Leave a Reply
You must be logged in to post a comment.