What’s the difference between using shiftOut (which is software) and SPI (aka, hardware) on Arduino, and how does it differ between an AtMega and an attiny? I built a simple test circuit and tested a few different configurations.
The test setup was simply a 74HC595 shift register connected to some pins with LEDs at the outputs. I wanted to make sure there was actual shifting happening, even though it doesn’t actually make much of a difference. The ‘595 acts like an SPI device (which, in turn, is basically just a fancy shift register) when you connect the SPI SCK pin to its SRCLK pin, MOSI to SER, and the SS pin to RCLK (which pushes the shifted-in bits to the outputs).
I measured the clock pin using a Saleae Logic 8 (amazing little device, btw). I will write more about it at some point in the future, but it’s basically a logic analyzer that can measure a variety of things, decode protocols, etc.
|AtMega328, 16MHz||SPI||8 MHz|
|AtMega328, 16MHz||shiftOut||79 kHz|
|AtMega32u4, 8MHz||SPI||4 MHz|
|AtMega32u4, 8MHz||shiftOut||40 kHz|
|AtTiny85, 8MHz||SPI||676 kHz|
|AtTiny85, 8MHz||shiftOut||27 kHz|
It’s not a secret that the SPI clock runs at half the system clock on the AtMega, so the two SPI results there are expected. What’s interesting to see is the clean factor of 100(!) between using SPI and shiftOut.
The attiny doesn’t have dedicated SPI hardware, instead it has a Universal Serial Interface (USI) that can be programmed to act as SPI, I2C, or serial. The recent versions of the ATTinyCore provide nice libraries that handle that for you. Still, there’s a penalty of roughly a factor of 10 when using that compared to an AtMega at the same clock speed. ShiftOut is then another factor 3 slower, which is actually surprising good: a factor 300 slower than the clock speed as compared to the AtMega, where it’s 200.
But either way, it’s clear that shiftOut is a poor substitute for real hardware SPI. Unless there’s some use case where SPI might not work (perhaps the pins are used for something else), shiftOut should be avoided.
I’m attaching my simple test program below.