74HC165 Demo [SPIN]

posted in: Tutorials 0

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 74HC165 is a Parallel to Serial shift register, which means that it takes a parallel input and creates a serial output. More specifically it takes eight bits on a parallel port, loads them into an internal shift register (MSB first) and then shifts them out serially. The 74HC165 is an SPI or Synchronous Serial device in that it has a clock that synchronizes the bits in the serial data. The LOAD line on the 74HC165 writes the data on the 8-bit port into an internal shift register to be shifted out of the device. All eight bits are loaded at once. Loading the data before shifting it prevents the inputs from changing during the shift. The 74HC165 is a great way to add additional inputs 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 inputs, limited only by memory.

Okay, say you have a bunch of configuration jumpers or other inputs you want to read but you don’t want to eat all your I/O pins up on the Propeller chip. Since these inputs only ever need to be high / low you can use the 74HC165 to read eight (or more) inputs 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 store the data into a variable (in this case a variable called value) by calling the readHC165 method. See the code attachment in the resources below. This is a full running demo that reads the 8 inputs and sends these values to the shift register which, in my circuit is connected to the eight DIP Switches 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. This code uses the Parallax Serial Terminal to display the binary representation of the data coming in. The baud rate is set to 115,200 bps.

Looking at the code lines 29-31 are the constants for the I/O pins connected to the various signals on the 74HC165 (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 74HC165 two signal pins are inputs and 1 is an output, so the I/O pins on the Propeller chip must be set accordingly as shown in line 48-49. Line 52 in the Main PUB is where I am reading the data from the readHC165 method. Now let’s look at lines 63-73 which makes up the actual method used to read data from the shift register.

In line 63 we’re declaring a local variable to hold the temporary data. Before we actually start shifting data in we need to grab the state of the inputs and this is done in lines 65-66 which bring the HC165_LOAD line low then high, storing the data into the shift register. Now we can start clocking the eight bits in so we have a repeat loop at line 68. Within the loop we will rotate the bits left one position then read each bit into our local variable bits one at a time. Line 69 does this by taking bits and shifting it left one position and the logically ORing it with the state of HC165_DI. The results are then put back into bits. At this point the HC165_CLK line is pulsed by lines 70-71 which bring it high and then low, effectively shifting the next bit of data within the shift register to the Q7 (MSB) pin. To get a visual for what’s happening on the 74HC165 let’s assume our switches are as follows:

10010011

The left-most 1 is in the MSB position and is therefore on the Q7 pin, which is what we’re getting our data from. Our loop first shifts its data and then loads this one into the LSB. On the next loop the 0 will now be in Q7 on the 74HC165. When we read this in line 69 a 0 will be stored in the LSB of bits. It is in this manner the data are shifted out of the 74HC165 and into our variable, which is being built 1 bit at a time starting with the MSB. Line 73 returns the data in bits back to our method call which assigns it to our global variable value.

You may have noticed a lack of any waitcnt statements in the code in the CLK and LOAD 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 74HC165 can run at up to 25MHz, which translates to a period of 40ns.

[IMAGE: SALEAE CAPTURE]

If you look at one of the test timing captures 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 in PASM we would exceed the specification of the 74HC165 without some calculated 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 or adjust the pin assignments to match your connections, declare a global variable in the VAR section to read the data into and copy the PRI method. Now you can shift data in easily using the 74HC165.

[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]

74HC595 Demo [SPIN] – Tutorial

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