shiftOut()

Shift out a byte of data, one bit at a time.

Library Documentation

void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 value)

Shift out a byte of data, one bit at a time.

This function starts at either the most significant or least significant bit in a byte value, and shifts out each byte in order onto a data pin. After each bit is written to the data pin, a separate clock pin is pulsed to indicate that the new bit is available.

Parameters
  • dataPin -

    Pin to shift data out on

  • clockPin -

    Pin to pulse after each bit is shifted out

  • bitOrder -

    Either MSBFIRST (big-endian) or LSBFIRST (little-endian).

  • value -

    Value to shift out

Discussion

This is a software implementation. There is also a hardware SPI library available which will be faster and consume less CPU cycles than this function.

Note that the dataPin and clockPin must already be configured to OUTPUT mode by a call to pinMode().

Also note that since shiftOut() outputs 1 byte (8 bits) at a time, it requires multiple steps to output values larger than 255.

Examples

To use these examples, replace dataPin and clockPin with the numbers of the pins you want to use:

/* MSBFIRST example */

uint16 data = 500;
// shift out high byte
shiftOut(dataPin, clockPin, MSBFIRST, (data >> 8));
// shift out low byte
shiftOut(dataPin, clockPin, MSBFIRST, data);

/* LSBFIRST serial */

data = 500;
// shift out low byte
shiftOut(dataPin, clockPin, LSBFIRST, data);
// shift out high byte
shiftOut(dataPin, clockPin, LSBFIRST, (data >> 8));

Arduino Tutorial Example

This Arduino example runs unmodified on the Maple. For accompanying circuit, see the tutorial on controlling a 74HC595 shift register.

//**************************************************************//
//  Name    : shiftOutCode, Hello World                         //
//  Author  : Carlyn Maw, Tom Igoe                              //
//  Date    : 25 Oct, 2006                                      //
//  Version : 1.0                                               //
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : to count from 0 to 255                            //
//**************************************************************//

// Pin connected to ST_CP of 74HC595
int latchPin = 8;
// Pin connected to SH_CP of 74HC595
int clockPin = 12;
// Pin connected to DS of 74HC595
int dataPin = 11;

void setup() {
  // Set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  // Count up routine
  for (int j = 0; j < 256; j++) {
    // Ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, j);
    // Return the latch pin high to signal chip that it
    // no longer needs to listen for information
    digitalWrite(latchPin, HIGH);
    delay(1000);
  }
}

License and Attribution

Some information in this page was adapted from the Arduino Reference Documentation, which is released under a Creative Commons Attribution-ShareAlike 3.0 License.