Serial to parallel, parallel to serial and rotation, with the 74HC595 and the 74HC165
A shift register is a row of D flip-flops in which the output of each goes to the input of the next. On each clock edge all the bits move one position: the one entering at one end pushes the others and the one at the other end leaves. This converts data from serial to parallel and from parallel to serial, delays signals by an exact number of cycles or builds ring and Johnson counters.
| Type | In | Out | Use |
|---|---|---|---|
| SISO | Serial | Serial | Delay line |
| SIPO | Serial | Parallel | More outputs: LEDs, relays, displays |
| PISO | Parallel | Serial | More inputs: push buttons, DIP switches |
| PIPO | Parallel | Parallel | Storage register |
| Universal | Both | Both | Shift left or right, like the 74HC194 |
The 74HC595 receives data on the SER pin, which shifts on each edge of SRCLK (shift clock). It also has a separate output register: the bits do not appear on outputs Q0–Q7 until an edge arrives on RCLK (latch). That way the outputs do not flicker while new data is loaded. The Q7' pin outputs the bit that falls off the end and connects to the SER of the next chip: with three microcontroller pins (data, clock and latch) you control 8, 16, 24 or more outputs. OE low enables the outputs, and PWM on OE dims all the LEDs at once.
digitalWrite(LATCH, LOW); shiftOut(DATA, CLOCK, MSBFIRST, value); // one byte per 74HC595 digitalWrite(LATCH, HIGH); // the outputs change here
Each 74HC595 output comfortably gives about 6 mA and the whole chip should not exceed about 70 mA. For relays or larger loads, add a ULN2803 after it or use the TPIC6B595, which has power outputs.
The 74HC165 does the opposite: a low pulse on SH/LD copies all eight inputs into the register at once, and then each clock edge shifts one bit out on Q7. Several are chained by connecting the Q7 of one to the serial input SER of the previous one. It is used to read many push buttons or switches with three or four pins.
In a binary number, shifting all bits one position towards the most significant bit multiplies by 2, and towards the least significant bit divides by 2. That is why processors have shift instructions (in C, x << 1 and x >> 1) and use them to multiply and divide quickly by powers of 2. In rotation, the bit that leaves comes back in at the other end and nothing is lost.
Related: latches and flip-flops, counters, ULN2803 for relays and loads and multiplexers.