Fork me on GitHub

Project Notes

#189 LED7Segment/ShiftDriveSPI

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


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.



The Schematic

The Build

Credits and References

About LEAP#189 7-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 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 (IMHO!).

The projects are usually inspired by things found wild on the net, or ideas from the sources such as:

Feel free to borrow liberally, and if you spot any issues do let me know. See the individual projects for credits where due. There are even now a few projects contributed by others - send your own over in a pull request if you would also like to add to this collection.