# SHIFTOUT

SHIFTOUT Example

Syntax: SHIFTOUT Dpin, Cpin, Mode, [OutputData {\Bits} {,OutputData {\Bits}...}]

## Function

Shift data out to a synchronous serial device.

- Dpin is a variable/constant/expression (0 - 15) that specifies the I/O pin that will be connected to the synchronous serial device's data input. This pin will be set to output mode.
- Cpin is a variable/constant/expression (0 - 15) that specifies the I/O pin that will be connected to the synchronous serial device's clock input. This pin will be set to output mode.
- Mode is a variable/constant/expression (0 - 1), or one of two predefined symbols, that tells SHIFTOUT the order in which data bits are to be arranged. See the table below for value and symbol definitions.
- OutputData is a variable/constant/expression containing the data to be sent.
- Bits is an optional variable/constant/expression (1 - 16) specifying how many bits are to be output by SHIFTOUT. If no Bitsentry is given SHIFTOUT defaults to 8 bits. When the Bits entry is given, the BASIC Stamp transmits the rightmost number of bits specified, regardless of the Mode.

## Quick Facts

| BS2, BS2e, and BS2pe | BS2sx and BS2p | BS2px
Timing of Th, Tl, Ta and Tb | 14 µs / 46 µs / 15 µs / 30 µs | 5.6 µs / 18 µs / 6.3 µs / 12.5 µs | 3.6 µs / 11.8 µs / 4.0 µs / 7.8 µs
Transmission Rate | ~ 16 kBits/Sec | ~ 42 kBits/Sec | ~ 65 kBits/Sec
Related Commands | SHIFTIN

## Explanation

SHIFTIN and SHIFTOUT provide an easy method of acquiring data from synchronous serial devices. Synchronous serial differs from asynchronous serial (like SERIN and SEROUT) in that the timing of data bits (on a data line) is specified in relationship to clock pulses (on a clock line). Data bits may be valid after the rising or falling edge of the clock line. This kind of serial protocol is commonly used by controller peripherals like ADCs, DACs, clocks, memory devices, etc.

At their heart, synchronous-serial devices are essentially shift-registers; trains of flip-flops that pass data bits along in a bucket brigade fashion to a single data output pin. Another bit is output each time the appropriate edge (rising or falling, depending on the device) appears on the clock line.

The SHIFTOUT instruction first causes the clock pin to output low and the data pin to switch to output mode. Then, SHIFTOUT sets the data pin to the next bit state to be output and generates a clock pulse. SHIFTOUTcontinues to generate clock pulses and places the next data bit on the data pin for as many data bits as are required for transmission.

Making SHIFTOUT work with a particular device is a matter of matching the mode and number of bits to that device's protocol. Most manufacturers use a timing diagram to illustrate the relationship of clock and data. One of the most important items to look for is which bit of the data should be transmitted first; most significant bit (MSB) or least significant bit (LSB). The table below shows the values and symbols available for the Mode argument

Symbol | Value | Meaning
LSBFIRST | 0 | Data is shifted out LSB-first
MSBFIRST | 1 | Data is shifted out MSB-first

(MSB is most-significant bit; the highest or left-most bit of a Nibble, Byte, or Word. LSB is the least-significant bit; the lowest or right-most bit of a Nibble, Byte, or Word.)

### SHIFTOUT Timing

Here is a simple example:

      SHIFTOUT 0, 1, MSBFIRST, [250]

Here, the SHIFTOUT command will write to I/O pin 0 (the Dpin) and will generate a clock signal on I/O 1 (the Cpin). The SHIFTOUT command will generate eight clock pulses while writing each bit (of the 8-bit value 250) onto the data pin (Dpin). In this case, it will start with the most significant bit first as indicated by the Mode value of MSBFIRST.

By default, SHIFTOUT transmits eight bits, but you can set it to shift any number of bits from 1 to 16 with the Bits argument. For example:

      SHIFTOUT 0, 1, MSBFIRST, [250\4]

Will output only the lowest (rightmost) four bits (%1010 in this case). But what if you want to output the leftmost bits of a given value? By adding the right-shift operator (>>) to the code you can adjust the output as required:

      SHIFTOUT 0, 1, MSBFIRST, [(250 >> 2)\6]

...will output the upper six bits (%111110 in this case).

Some devices require more than 16 bits. To solve this, you can use a single SHIFTOUT command with multiple values. Each value can be assigned a particular number of bits with the Bits argument. As in:

      SHIFTOUT 0, 1, MSBFIRST, [250\4, 1045\16]

The preceding example will first shift out four bits of the number 250 (%1010) and then 16 bits of the number 1045 (%0000010000010101). The two values together make up a 20 bit value.

In the examples above, specific numbers were entered as the data to transmit, but, of course, the SHIFTOUT command will accept variables and expressions for the OutputData and even for the Bits argument.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012