Summary of How to drive 595 shift registers with ATmega168
This article explains how to drive 74HC595 shift registers using the ATmega168 AVR chip's built-in SPI hardware. It details connecting control pins like SCLR and Enable, wiring serial data lines (SI, SCK, RCK), and cascading multiple registers. The guide outlines essential code steps: defining output pins, enabling SPI in Master mode, pulling the SS pin low, writing data to SPDR, and toggling the latch to update outputs.
Parts used in the Driving Shift Registers with ATmega168 Project:
- ATmega168 microcontroller
- 74HC595 shift register chips
- SPI module (Serial Peripheral Interface)
- VCC power supply connection
- GND ground connection
- SCLR pin
- Enable pin
- SI (Serial In) pin
- SCK (Serial Clock) pin
- RCK (Storage Register Clock) pin
- QH prime pin
- Data buses
- LEDs
Driving a shift register using an AVR chip’s built-in hardware is really quite easy. Most of their offerings have an SPI module, or Serial Peripheral Interface. A shift register is exactly that, a peripheral device that communicates via a serial line. All we need to do is hook up our connections and use a few pieces of simple code. Join me after the break to see how that’s done.
Just want to know how shift registers work? Check in on my other post on that topic.
The first thing to consider is that it may not be necessary to connect the Clear and Enable pins to the microcontroller. If there are relatively few shift registers being used together it may be easier to shift in all zeros and latch them to the registers. But if timing is a big issue you may want this capability. For this example I have just connected the SCLR pin to VCC and the enable pin to GND.
This leaves three control pins: SI, SCK, and RCK. It is possible to drive SCK and RCK from the same signal, but that’s for another tutorial. My test hardware, the ATmega168, has pins meant to drive each of these controls. Note that the serial and storage clocks (SCK and RCK) for both shift register chips are hooked together on two data buses. The Serial In of the first chip (SI) is connected to the microcontroller. The QH prime pin of this first chip is connected to the SI pin of the second chip to cascade the data from one to the next.
Using the AVR SPI hardware
Accessing the SPI hardware is pretty simple. There’s only a few things that need to happen to get it running:
- Define which pins we’re using and set them to outputs
- Enable SPI and set it to Master mode
- pull the SS pin low
- Write our data to the SPDR, it will automatically be strobed into the shift registers
- Toggle the latch to display the data on the LEDs
Schematic
The definitions are easy enough. I can’t just choose any pins, the SPI outputs are specific pins which can be looked up in the datasheet:
#define SHIFT_REGISTER DDRB#define SHIFT_PORT PORTB#define DATA (1<<PB3)//MOSI (SI)#define LATCH (1<<PB2)//SS(RCK)#define CLOCK (1<<PB5)//SCK(SCK)
SHIFT_REGISTER |= (DATA | LATCH | CLOCK);//Set control pins as outputsSHIFT_PORT &= ~(DATA | LATCH | CLOCK);//Set control pins low
Enabling SPI in Master mode is a one-liner. But there are other clock prescaling options that you can choose from if you need to. I’ll leave that up to you to explore:
For more detail: How to drive 595 shift registers with ATmega168
- How do you handle the Clear and Enable pins if timing is not critical?
You can connect the SCLR pin to VCC and the enable pin to GND. - What are the three main control pins required to drive the shift register?
The three control pins are SI, SCK, and RCK. - Can SCK and RCK be driven by the same signal?
Yes, it is possible to drive both from the same signal, though this is covered in another tutorial. - How do you cascade data from one shift register to the next?
Connect the QH prime pin of the first chip to the SI pin of the second chip. - What is the first step when accessing the SPI hardware?
Define which pins you are using and set them to outputs. - Which mode must the SPI be set to for this project?
The SPI must be enabled and set to Master mode. - How is data automatically strobed into the shift registers?
You write your data to the SPDR register. - What action is needed to display the data on the LEDs?
You must toggle the latch to display the data. - Are the SPI output pins arbitrary or specific?
The SPI outputs are specific pins that must be looked up in the datasheet.


