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.
- Posts: 403
- Joined: Tue May 12, 2015 2:43 pm
- Location: Palmyra, NY
March 1st, 2014, 04:28 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 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 8 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 8 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 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 inputs.
Okay, say we have a bunch of configuration jumpers or other inputs we want to read but we don't want to eat all our I/O pins up on the Propeller chip. Since these inputs only ever need to be high / low we can use the 74HC165 to read 8 (or more) inputs 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 store the data into a variable (in this case a variable called value) by calling the method. See the code attachment below, 74HC165_Demo.spin. 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 8 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 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 74HC165 2 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 our 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 8 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 1 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 25 MHz which translates to a period of 40 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 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, declare a global variable in the VAR section and copy the PRI method. Now you can shift data in easily.
- (11.5 KiB) Downloaded 212 times
I'm only responsible for what I say, not what you understand.
Users browsing this forum: No registered users and 2 guests