Reference.ShiftOut History

Hide minor edits - Show changes to output

August 19, 2013, at 04:30 PM by Roberto Guido - minor typo in code. Thanks to Andy D for feedback
Changed lines 39-40 from:
shiftOut(data, clock, MSBFIRST, data);
to:
shiftOut(dataPin, clock, MSBFIRST, data);
October 25, 2011, at 08:09 PM by David A. Mellis - adding note about devices clocked by a rising edge
Changed lines 4-5 from:
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 data pin, after which a clock pin is pulsed to indicate that the bit is available.
to:
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 data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available.

Note: if you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the call to shiftOut(), e.g. with a call to digitalWrite(clockPin, LOW).
September 24, 2011, at 01:24 AM by Tom Igoe -
Changed lines 30-31 from:
[@
to:
(:source lang=arduino tabwidth=4:)
Changed lines 44-46 from:
shiftOut(dataPin, clock, LSBFIRST, (data >> 8)); @]

to:
shiftOut(dataPin, clock, LSBFIRST, (data >> 8));

(:sourceend:)



Changed lines 55-56 from:
[@//**************************************************************//
to:
(:source lang=arduino tabwidth=4:)
//**************************************************************//
Changed lines 90-91 from:
} @]
to:
}

(:sourceend:)


March 11, 2011, at 12:48 AM by David A. Mellis -
Changed lines 6-7 from:
This is a software implementation; Arduino (as of 0019) also provides an [[SPI | SPI library]] that uses the hardware implementation.
to:
This is a software implementation; see also the [[SPI | SPI library]], which provides a hardware implementation that is faster but works only on specific pins.
December 16, 2010, at 04:07 AM by David A. Mellis -
Added lines 85-89:

!!!!See also

* [[ShiftIn | shiftIn()]]
* [[SPI | SPI]]
September 05, 2010, at 06:12 PM by David A. Mellis -
Changed lines 4-7 from:
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 data pin, after which a clock 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 stay synchronized, and communicate at close to maximum speeds, since they both share the same clock line. This is often referred to in chip hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Serial Peripheral Interface (SPI)]].
to:
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 data pin, after which a clock pin is pulsed to indicate that the bit is available.

This is a software implementation; Arduino (as of 0019) also provides an [[SPI | SPI library]] that uses the hardware implementation.
May 13, 2009, at 06:14 AM by Paul Badger -
Changed line 32 from:
data = 500;
to:
int data = 500;
May 12, 2009, at 06:20 PM by Paul Badger -
Changed line 29 from:
shiftOut is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255.
to:
'''shiftOut''' is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255.
May 12, 2009, at 06:20 PM by Paul Badger -
Deleted line 41:
May 12, 2009, at 06:19 PM by Paul Badger -
Changed line 30 from:
to:
[@
Changed lines 44-46 from:
shiftOut(dataPin, clock, LSBFIRST, (data >> 8));

to:
shiftOut(dataPin, clock, LSBFIRST, (data >> 8)); @]

May 12, 2009, at 06:18 PM by Paul Badger -
Added lines 29-46:
shiftOut is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255.

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

// Or do this for LSBFIRST serial
data = 500;
// shift out lowbyte
shiftOut(dataPin, clock, LSBFIRST, data);

// shift out highbyte
shiftOut(dataPin, clock, LSBFIRST, (data >> 8));

November 02, 2008, at 10:56 AM by David A. Mellis -
Changed lines 1-2 from:
!!shiftOut(dataPin, clockPin, bitOrder, value)
to:
!!shiftOut()
Changed lines 4-7 from:
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 the dataPin, after which the clockPin 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. This is often referred to in chip hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Serial Peripheral Interface (SPI)]]
to:
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 data pin, after which a clock 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 stay synchronized, and communicate at close to maximum speeds, since they both share the same clock line. This is often referred to in chip hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Serial Peripheral Interface (SPI)]].

!!!!Syntax

shiftOut(dataPin, clockPin, bitOrder, value)
Changed lines 27-28 from:
The '''dataPin''' and '''clockPin''' must already be configured as outputs by a call to [[pinMode]].
to:
The '''dataPin''' and '''clockPin''' must already be configured as outputs by a call to [[pinMode]]().
Deleted lines 67-107:

!!!Note
Note 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 outputting an int with shiftout requires a two-step operation:

!!!!Example:

[@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;
// shift out highbyte
// " >> " is bitshift operator - moves top 8 bits (high byte) into low byte
shiftOut(data, clock, MSBFIRST, (data >> 8));

// shift out lowbyte
shiftOut(data, clock, MSBFIRST, data);



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

// shift out highbyte
shiftOut(data, clock, LSBFIRST, (data >> 8));
@]

!!!!See
October 18, 2008, at 04:36 PM by Paul Badger -
Changed line 104 from:
[[HomePage | Reference Home]]
to:
!!!!See
October 18, 2008, at 04:33 PM by Paul Badger -
Changed lines 6-7 from:
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. This is often referred to in chip hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Synchronous Peripheral Interface (SPI)]]
to:
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. This is often referred to in chip hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Serial Peripheral Interface (SPI)]]
October 18, 2008, at 04:32 PM by Paul Badger -
Changed lines 6-7 from:
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. This is often referred to in hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Synchronous Peripheral Interface (SPI)]]
to:
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. This is often referred to in chip hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Synchronous Peripheral Interface (SPI)]]
October 18, 2008, at 04:30 PM by Paul Badger -
Changed lines 6-7 from:
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.
to:
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. This is often referred to in hardware documentation as [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus | Synchronous Peripheral Interface (SPI)]]
July 22, 2008, at 04:05 AM by David A. Mellis -
Changed lines 6-7 from:
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.
to:
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.
Deleted lines 24-64:

!!!!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 outputting an int with shiftout requires a two-step operation:

!!!!Example:

[@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;
// shift out highbyte
// " >> " is bitshift operator - moves top 8 bits (high byte) into low byte
shiftOut(data, clock, MSBFIRST, (data >> 8));

// shift out lowbyte
shiftOut(data, clock, MSBFIRST, data);



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

// shift out highbyte
shiftOut(data, clock, LSBFIRST, (data >> 8));
@]

Changed lines 65-103 from:
to:
!!!Note
Note 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 outputting an int with shiftout requires a two-step operation:

!!!!Example:

[@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;
// shift out highbyte
// " >> " is bitshift operator - moves top 8 bits (high byte) into low byte
shiftOut(data, clock, MSBFIRST, (data >> 8));

// shift out lowbyte
shiftOut(data, clock, MSBFIRST, data);



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

// shift out highbyte
shiftOut(data, clock, LSBFIRST, (data >> 8));
@]
December 22, 2007, at 02:47 PM by Paul Badger -
Changed lines 59-60 from:
shiftOut(data, clock, MSBFIRST, data);
to:
shiftOut(data, clock, LSBFIRST, data);
Changed line 62 from:
shiftOut(data, clock, MSBFIRST, (data >> 8));
to:
shiftOut(data, clock, LSBFIRST, (data >> 8));
December 22, 2007, at 02:46 PM by Paul Badger -
Changed line 48 from:
// " >> " is bitshift operator - moves top 8 bit (high byte) into low byte
to:
// " >> " is bitshift operator - moves top 8 bits (high byte) into low byte
December 22, 2007, at 02:45 PM by Paul Badger -
Changed lines 27-28 from:
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 outputing an int with shiftout requires a two-step operation:
to:
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 outputting an int with shiftout requires a two-step operation:
December 05, 2007, at 07:56 PM by Paul Badger -
Changed lines 13-16 from:
bitOrder: which order to shift out the bits; either '''MSBFIRST''' or '''LSBFIRST'''.

-> (Most Significant Bit First, or, Least Significant Bit First)
to:
bitOrder: which order to shift out the bits; either '''MSBFIRST''' or '''LSBFIRST'''.\\
(Most Significant Bit First, or, Least Significant Bit First)
December 05, 2007, at 07:56 PM by Paul Badger -
Changed lines 13-14 from:
bitOrder: which order to shift out the bits; either '''MSBFIRST''' or '''LSBFIRST'''.\\
to:
bitOrder: which order to shift out the bits; either '''MSBFIRST''' or '''LSBFIRST'''.
December 05, 2007, at 07:55 PM by Paul Badger -
Changed lines 14-15 from:
->(Most Significant Bit First, or, Least Significant Bit First)
to:
-> (Most Significant Bit First, or, Least Significant Bit First)
December 05, 2007, at 07:55 PM by Paul Badger -
December 05, 2007, at 07:53 PM by Paul Badger -
Changed lines 14-15 from:
(Most Significant Bit First, or, Least Significant Bit First)
to:
->(Most Significant Bit First, or, Least Significant Bit First)
December 05, 2007, at 07:52 PM by Paul Badger -
Changed line 13 from:
bitOrder: which order to shift out the bits; either '''MSBFIRST''' or '''LSBFIRST'''.
to:
bitOrder: which order to shift out the bits; either '''MSBFIRST''' or '''LSBFIRST'''.\\
December 05, 2007, at 07:52 PM by Paul Badger -
Changed lines 13-14 from:
bitOrder: which order to shift out the bits (either '''MSBFIRST''' or '''LSBFIRST''').
to:
bitOrder: which order to shift out the bits; either '''MSBFIRST''' or '''LSBFIRST'''.
(Most Significant Bit First, or, Least Significant Bit First)
December 05, 2007, at 07:50 PM by Paul Badger -
Changed lines 26-27 from:
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:
to:
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 outputing an int with shiftout requires a two-step operation:

!!!!Example:
November 04, 2007, at 05:35 AM by Paul Badger -
Changed lines 4-5 from:
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.
to:
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 the dataPin, after which the clockPin is toggled to indicate that the bit is available.
July 15, 2007, at 01:10 AM by Paul Badger -
Added line 47:
Added line 55:
// shift out lowbyte
Changed lines 57-58 from:
// shift out lowbyte
to:

// shift out highbyte
Changed lines 60-62 from:
// shift out highbyte @]

to:
@]

July 15, 2007, at 01:09 AM by Paul Badger -
Changed lines 45-46 from:
shiftOut(data, clock, MSBFIRST, (data >> 8)); // >> is bitshift operator - moves highbyte into lowbyte
to:
// " >> " is bitshift operator - moves top 8 bit (high byte) into low byte
shiftOut(data, clock, MSBFIRST, (data >> 8));
July 15, 2007, at 01:08 AM by Paul Badger -
Deleted line 43:
shiftOut(data, clock, MSBFIRST, (data >> 8));
Added lines 45-46:
shiftOut(data, clock, MSBFIRST, (data >> 8)); // >> is bitshift operator - moves highbyte into lowbyte
// shift out lowbyte
Added lines 48-53:



// And do this for LSBFIRST serial
data = 500;
shiftOut(data, clock, MSBFIRST, data);
Deleted lines 54-59:


// And do this for LSBFIRST serial
data = 500;
shiftOut(data, clock, MSBFIRST, data);
// shift out lowbyte
April 14, 2007, at 06:30 AM by Paul Badger -
Changed lines 6-7 from:
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 in hardware documentation as SPI.
to:
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.
April 14, 2007, at 04:44 AM by Paul Badger -
April 14, 2007, at 04:43 AM by Paul Badger -
Changed line 25 from:
!!!!Common Errors
to:
!!!!Common Programming Errors
April 14, 2007, at 04:42 AM by Paul Badger -
Changed lines 25-27 from:
!!!!Warning
Note also that this function, as it is currently written, only outputs 8 bits at a time. An int holds two bytes (16 bits), so if one tries to do something like this:
to:
!!!!Common 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:
April 14, 2007, at 04:27 AM by Paul Badger -
Changed lines 26-27 from:
Note also that this function, as it is currently written, only outputs 8 bits at a time. An int holds two bytes, so if one tries to do something like this:
to:
Note also that this function, as it is currently written, only outputs 8 bits at a time. An int holds two bytes (16 bits), so if one tries to do something like this:
April 14, 2007, at 04:26 AM by Paul Badger -
Changed lines 26-27 from:
Note also that this function, as it is currently written, only outputs 8 bits at a time. An int holds two bytes so if one tries to do something like this:
to:
Note also that this function, as it is currently written, only outputs 8 bits at a time. An int holds two bytes, so if one tries to do something like this:
April 14, 2007, at 04:25 AM by Paul Badger -
Changed lines 4-5 from:
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.
to:
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 in hardware documentation as SPI.
Added lines 24-57:

!!!!Warning
Note also that this function, as it is currently written, only outputs 8 bits at a time. An int holds two bytes 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 @]

December 02, 2006, at 04:42 PM by David A. Mellis -
Changed lines 25-26 from:
[@//**************************************************************//
to:

[@//**************************************************************//
December 02, 2006, at 04:42 PM by David A. Mellis -
Changed line 25 from:
[@ //**************************************************************//
to:
[@//**************************************************************//
December 02, 2006, at 04:42 PM by David A. Mellis -
Changed lines 25-27 from:

[@
//**************************************************************//
to:
[@ //**************************************************************//
Deleted lines 40-41:

December 02, 2006, at 04:41 PM by David A. Mellis -
Added lines 23-25:

''For accompanying circuit, see the [[Tutorial/ShiftOut | tutorial on controlling a 74HC595 shift register]].''
Changed lines 65-66 from:
''For accompanying circuit, see the [[Tutorial/ShiftOut | tutorial on controlling a 74HC595 shift register]].''
to:
December 02, 2006, at 04:41 PM by David A. Mellis -
Changed lines 60-62 from:
}
@]
to:
} @]
December 02, 2006, at 04:41 PM by David A. Mellis -
Added lines 18-21:
!!!!Note

The '''dataPin''' and '''clockPin''' must already be configured as outputs by a call to [[pinMode]].
Deleted line 60:
Changed lines 63-66 from:
!!!!Note

The '''dataPin''' and '''clockPin''' must already be configured as outputs by a call to [[pinMode]].
to:
''For accompanying circuit, see the [[Tutorial/ShiftOut | tutorial on controlling a 74HC595 shift register]].''
December 02, 2006, at 04:39 PM by David A. Mellis -
Changed lines 21-22 from:
// Name : shiftOutCode, Hello World //
// Author : Carlyn Maw,Tom Igoe //
to:
// Name : shiftOutCode, Hello World //
// Author : Carlyn Maw,Tom Igoe //
Added line 57:
December 02, 2006, at 04:38 PM by David A. Mellis -
Added lines 1-63:
!!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.

!!!!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

!!!!Example
[@
//**************************************************************//
// 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);
}
}
@]

!!!!Note

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

[[HomePage | Reference Home]]

Share