23 Mar 2013

Serial and parallel I/O : shift registers

Sequential logic

When I discussed about digital logic, I mostly focused on combinational logic (decoders, adders...) but there is another really important type of digital circuits : sequential logic circuits. Sequential logic circuits are circuits that the outputs not only depend on the inputs, but also of the order in time that these inputs are read. Some new terms are introduced in this circuit type.

First of all, the concept of the memory bit : if the sequential circuit is time dependent, it means that there is some way to memorize the internal state of the circuit at a given time period. This is done with bistable circuits called latches or flip-flop (more on this in another article) that can be used as storage elements. If we combine more than one of these memory bits together, we can create registers.

Most of the time, sequential circuits work with a clock signal. A clock signal is a stream of square wave pulses that is used to synchronize the circuits. With the clock signal, data can be serialized on a single wire. This is the basis of high-speed computer buses. Here is examples waveform of a I2C bus transaction. The SDA signal is serial data and the SCL signal means serial clock and is square wave signal. The data being transferred is something like 011011001...



Shift registers

An interesting sequential logic device is the shift register. The shift register is a clocked device and can work in many modes. In the picture below, the blue cubes are memory bits and red arrows represent data being shifted right each clock cycle. Data can be shifted at the rising or falling edge of the clock pulses (depending on the IC). As you can see, inputs can be serial or parallel and outputs can also be serial or parallel. We're going to see two example of chips using a shift register.




Serial to parallel with the 74HC595 IC

The 74HC595 chip is often called an outputs expander. This is due to the fact that a µC (microcontroller) or other devices that can talk serially to this chip have the possibility to add a few output lines to their I/O. Has you may have already guessed, this IC is a serial input, parallel output shift register. With a single 595, you can add 8 output lines with as few as 3 wires. What's interesting is that you can daisy chain multiple 595 to add 16, 24, 32 and more outputs with only 3 control wires.

To be able to understand the inner working of that chip, I used it's datasheet. Below are some information that I found concerning the pin-out of the chip and the pins functionality.


The interesting pins are the 8 parallel outputs (Q0-Q7) and the 3 control pins : STCP (latch), SHCP (clock), DS (serial input). The OE pin is the output enable and must be tied to the ground to activate the outputs. MR is the master reset, tie it to Vcc (positive supply voltage) to get the chip out of the reset state.


In the table below, you can easily see what is going to happen with a given set of inputs. To make that short, a rising edge on the clock pin (SHCP) will store the serial input bit (DS) in the least significant bit (Q0S) of the shift register. All the previous bits in the shift register will move one bit toward the most significant bit (Q7S) one clock at a time. When the latch input is pulsed (STCP) the actual content of the shift register will be saved in the storage register. The storage register is directly tied to the parallel output (Q0-Q7). Remember that you must activate OE to make the outputs visible.



I wrote a driver in SPIN (the language used by my favorite µC, the P8X32A) to drive the 74HC595. I hope that in a couple of article I can write some tutorials about microcontroller programming. If you are a bit familiar with programming languages and algorithm, you should easily understand the code below.



You can see the result of the code on a LED bar-graph. On the first picture, $FF (1111_1111) was sent to the 595. On the second one, $55 was sent (0101_0101), can you see it? Please note that the 595 only has 8 output, so I only use 8 of the ten available LED on the bar-graph. Besides, I burned these two LEDs by doing some experiments on the bar-graph without current limiting resistors, duh!




Parallel to serial with the 74HC165 IC

The 165 IC is a parallel input, serial output shift register. Almost everyone has already used a device containing a 8 bit parallel to serial static shift register, if you ever played with an old Nintendo Entertainment System :)


In fact, this is the kind of chip used to store the 8 NES controller buttons (start, select, A, B, up, down, left, right) state and send it serially to the console. This operation is performed several times in a second. This can be seen as the inverse operation of the 595, as an input pins expander.

The working principle is quite simple : when PL (parallel load) is asserted, data from (D0-D7) are loaded in the shift register. After that, the bits are sent serially to Q7 (serial output from last stage) on each rising CP pin (clock) pulses.


I think that this IC is a little less interesting to demonstrate, as the data end up in the microcontroller and cannot really be seen... Though, it could be interesting to make some kind of gamepad with this IC as a demonstration. Moreover, the development board that I use has 2 NES gamepad port on it. Here is the source code for dealing with two gamepads (thanks to XGameStation, the developers of the board) :



Still here? :)

There was a lot of new information in this post, I did my best to make it the clearer and easier to understand as possible. For the lines of code shown, as I said, I will blog about programming later but there is a lot of interesting things to see before delving into that. Do not hesitate to write suggestions in the comment box!

No comments:

Post a Comment

Post a Comment