Arduino ShiftOut: Control chips with a
3 wire Serial Interface. Use it to control many different chips that
use a serial interface. Find out
how to use it, how it works and how fast it operates.
Arduino shiftOut : How to create serial output to control chips.
Send data to a chip by sending a clock and sending serial data.
It is a software parallel to serial converter.
Chips use a serial inputs to
Instead of 8 or 16 pins to a chip you just use two! - a clock and a data pin.
Here you can find out how Arduino shiftOut works and how fast it is. You
can use the function to control many different types of chips. For
a serial device e.g. BMP280 or create more outputs e.g. 74HC595.
All Arduino chips have an SPI (Serial Peripheral Interface) but the I/O
for SPI are always attached to specific pins. The shiftout function can
be applied to any digital pins.
Arduino shiftOut is a purely
software implementation of a serial output interface; The equivalent
hardware interface is SPI (Although shiftOut() represents half of that
interface i.e. the data output part) - SPI also allows for data
input (the equivalent software function is shiftIn).
Many chips use a serial interface to reduce the number of physical
pins, so instead of using a parallel processor bus to transfer data 8 or
16 bits at a time, two signals (clock and data) send data into the
device, one bit at a time.
In the case of a 16bit device, "think of a 16 bit ADC", you save 14
pins. This can often mean a device will fit into a tiny surface mount
package, so that you can fit more functions into the same area on a PCB.
Differences between SPI and Arduino shiftOut
SPI is fast.
SPI operates only from specific pins.
SPI accommodates different clock types e.g. normally high etc.
SPI transmits and receives data at the same time.
shiftOut is slow.
shiftOut can only transmit data.
shiftOut can work from any digital pins.
shiftOut can be applied to multiple pins.
Since the shiftOut function is not limited to specific pins you could have different devices attached to different pins.
TIP: You can have multiple instances of shiftOut on different pins.
How ShiftOut Works
Arduino ShiftOut uses two signal pins (any digital output pins - i.e. most of
them!) and generates a clock signal and and a data signal using the digitalWrite() function.
Source code for shiftOut
The source code for Arduino shiftOut is contained in wiring_shift.c. The path is:
The basic operation is to loop through each bit using the for-loop.
Then figure out a value for the current bit, and if set, then output high, or
if reset, then output low. Then pulse the clock signal high, then low.
In this way the data line is set to the value of each bit in turn and
the clock is pulsed. The receiving chip accepts the data line output
(from the shiftOut dataPin) as an input. The data is transferred into
this chip on the transition of the clock from low to high. You can use
this type of process to drive the 74HC595 Serial-in-parallel-out chip.
How does the shifOut() code work?
The code works by using the bit shift operator '<<' which shifts all the bits in a variable one to the left.
Assume the bitOrder is LSBFIRST then for the first loop iteration i
is zero and the term (1<<i) shifts a variable left by zero places
i.e. it leaves the variable with the value 1.
Note: In C temporary variables are used to hold intermediate values.
Generating a walking 'one'
This is bit zero (b0).
The next time around the loop the variable holds the value 1 shifted left by one place:
In this way a walking '1' is created moving from b0 to b7.
For a bitOrder of MSBFIRST, the value of the index is manipulated to
move from b7 to b0 for each value of i. So the walking '1' walks in the
opposite direction, starting from bit 7.
Note: MSBFIRST, code is slightly slower as a 8 x minus operations occur.
Extracting the bit value
To figure out the bit value of val, this walking one value is 'ANDed'
with val. This results in a different value for each bit. For example
if bit 6 were set in val, then the value output would be pow(2,6) = 64.
If it was not set then the value would be zero.
Purifying the bit value
The two exclamation marks look like a different maths operator but
they are in fact just two inversion actions. The exclamation mark is a
logical inversion operator, not a bit inversion operator.
The first invert action changes the result of the 'AND' operation
into a true or false value. i.e if result>0 then output false,
and if result =0 output true.
At this point the logic is inverted so the second inversion operator
'!' flips it back.
This value is then used by the digitalWrite()
function to output the data value to the selected dataPin.
How fast is Arduino shiftOut()?
The function uses digitalWrite() so it should be of the order of the
speed of digitalWrite() - in that link the pulse period (for an Arduino
Uno) is 7us.
Since 8 pulses are needed, shiftOut() should take about 56us plus a bit more for bit manipulation and looping round the for-loop. In fact the extra operations in shiftOut() double this time (see below for timings).
Example Program for testing Arduino shiftOut()
The following sketch outputs a marker signal on pin 7 (so you can
trigger a scope on the start of the serial data) and alternates data
with a pattern 0x55 and 0x99 - an alternating data pattern.
The oscilloscope output that the time is actually
121us (lowest signal - marker). The period of the clock signal is: 15.2us (65.8kHz).
When using LSB the width of the marker signal increases to 122us.
However you would normally use the marker signal as the chip select,
so it is representative of the achievable timing (CS would normally be
inverted so that it is active low).
The top line shows the clock signal on pin 6
The next is data output of x55 (MSB first) on pin 5.
The next is data output of 0x99 (MSB first) on pin 5 (overlay of stored image).
The next is the marker (trigger signal) on pin 7.