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

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

Project Source on GitHub Return to the LEAP Catalog
About LEAP

LEAP is my personal collection of electronics projects - 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.

Projects are often inspired by things found wild on the net, or ideas from the many great electronics podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know or send a pull-request.

NOTE: For a while I included various scale modelling projects here too, but I've now split them off into a new repository: check out LittleModelArt if you are looking for these projects.

Project Gallery view the projects as an image gallery Notebook reference materials and other notes Follow the Blog follow projects and notes as they are published in your favourite feed reader