Arduino - ShiftOut

Arduino

Guide

Tutorials

Reference

Hardware

Software

Forum

Playground

Exhibition

Hacking

FAQ

Buy

Language | Libraries | Comparison

## shiftOut(dataPin, clockPin, bitOrder, value)

#### Description

Shifts out a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is written in turn to a pin, after which another pin is toggled to indicate that the bit is available.

This is known as synchronous serial protocol and is a common way that microcontrollers communicate with sensors, and with other microcontrollers. The two devices always stay synchronized, and communicate at close to maximum speeds, since they both share the same clock line. Often referred to as SPI (synchronous protocol interface) in hardware documentation.

#### Parameters

dataPin: the pin on which to output each bit (int)

clockPin: the pin to toggle once the dataPin has been set to the correct value (int)

bitOrder: which order to shift out the bits (either MSBFIRST or LSBFIRST).

value: the data to shift out. (byte)

#### Returns

None

#### Note

The dataPin and clockPin must already be configured as outputs by a call to pinMode.

#### Common Programming Errors

Note also that this function, as it is currently written, is hard-wired to output 8 bits at a time. An int holds two bytes (16 bits), so if one tries to do something like this:

    int data;
    int clock;
    int cs;
    ...

    digitalWrite(cs, LOW);
    data = 500;
    shiftOut(data, clock, MSBFIRST, data)
    digitalWrite(cs, HIGH);

    // this will actually only output 244 because
    // 500 % 256 = 244
    // since only the low 8 bits are output

    // Instead do this for MSBFIRST serial
    data = 500;
    shiftOut(data, clock, MSBFIRST, (data >> 8));
    // shift out highbyte
    shiftOut(data, clock, MSBFIRST, data);
    // shift out lowbyte

    // And do this for LSBFIRST serial
    data = 500;
    shiftOut(data, clock, MSBFIRST, data);
    // shift out lowbyte
    shiftOut(data, clock, MSBFIRST, (data >> 8));
    // shift out highbyte

#### Example

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);
      }
    }

Reference Home Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

Edit Page | Page History | Printable View | Recent Site Changes