Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (45 sloc) 2.09 KB
// PROJECT :ShiftOutvsSPI
// PURPOSE :To highlight the overlap between ShiftOut (software) and SPI (hardware) by
// :looping through a full byte range to a Morland Bargraph v3.
// DEVICE :Arduino UNO + Morland Bargraph v3.
// AUTHOR :C. D'Arcy
// DATE :2019 02 02
// uC :328p
// STATUS :Working
// :
// :
// NOTES :This (revealing) code highlights and contrasts the difference between the use
// :of the (software-based) shiftOut library function and the (hardware-based)
// :SPI peripheral on the UNO. They are strikingly similar but each has their own
// :advantages and disadvantages depending on your application.
#include <SPI.h>
//Simply activate one of the next two lines. The code will do the rest...
#define SHIFTMODE //Start with (more common) SHIFTMODE...(notice the flicker)
//#define SPIMODE //Now (considerably faster) SPIMODE...(flicker-free)
uint8_t value = 0; //sample data to upload for confirmation and comparison
void setup() {
Serial.begin(9600); //Useful for time benchmarks to compare speeds if desired
pinMode(SCK, OUTPUT); //System Clock (pin 13) (exploit predefines:)
pinMode(MOSI, OUTPUT); //Master Out Slave In (pin 11). No need for MISO (12) in this example
pinMode(SS, OUTPUT); //Slave Select (pin 10)
#ifdef SPIMODE
SPI.beginTransaction(SPISettings(16000000, LSBFIRST, SPI_MODE0));
void loop() {
digitalWrite(SS, LOW); //pull down Slave Select line (aka RCLK or Latch) to start
shiftOut(MOSI, SCK, LSBFIRST, value);
#ifdef SPIMODE
digitalWrite(SS, HIGH); //pull up Slave Select (aka RCLK or Latch) to complete transfer
delayMicroseconds(100); //observe flicker with SHIFTMODE but not SPIMODE (100x faster!)