Fork me on GitHub

Project Notes

#189 7-Segment Shift Drive SPI

Using SPI to control a 7-segment LED display via a 74HC595 shift register.

The Build

Notes

In previous projects like ShiftDrive, the code has directly implemented the clock, latching and data transfer protocol expected by the 74HC595 shift register.

It’s also possible to control the 74HC595 shift register using hardware SPI. Although this doesn’t simplify the code or necessarily affect performance much, it does allow for expansion.

This sketch is a modification of ShiftDrive to control the ShiftDriveModule via SPI.

SPI pin mapping

Here’s how the 595 register pins relate to SPI. The Arduino pins are for an Uno - see the SPI reference page for details for other boards.

74HC595 SPI Description Arduino Uno SPI Pin
DATA (DS, 14) MOSI Master Out Slave In 11
n/a MISO Master In Slave Out 12
CLOCK (SH_CP,11) SCLK Serial Clock 13
n/a SS Slave Select (in) 10
LATCH (ST_CP,12) SS Slave Select (out) any free pin

SPI Settings

Although default SPI settings may work, let’s check. There are three factors:

  • maximum speed
  • bit order
  • mode

SPI Max Speed

The 74HC595 Datasheet indicates typical maximum clock frequency for the 74HC is 100MHz. OK, we’ll never get near that with a 16MHz Arduino, so anything goes. There may be reasons to do with the specific circuit to throttle the clock, but in this case it’s not a factor.

SPI Bit Order

Here’s a summary of how the bits in the shift register are mapped to LED segments in my build:

Bit Segment
0 A - top horizontal
1 B - top right vertical
2 C - bottom right vertical
3 D - bottom horizontal
4 E - bottom left vertical
5 F - top left vertical
6 G - center horizontal
7 dp - decimal point

However the code uses bytes to represent dp,G,F..A from MSB to LSB, so I use MSBFIRST transfer.

SPI Mode

SPI modes define the clock polarity and phase. The timing diagram in the 74HC595 Datasheet shows:

  • base clock polarity is 0 (CPOL=0)
  • data captured on the clock’s falling edge and data is output on a rising edge (CPHA=1)

This is “mode 0”, aka SPI_MODE0.

Construction

Breadboard

The Schematic

The Build

Credits and References

About LEAP#189 LED7-SegmentCMOS/TTLArduinoSPI
Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.