Example

  SHIFTOUT DPin, CPin, ShiftMode, Value {\Count} {, SpeedMult}

Function
Shift data out to a synchronous serial device.

Explanation
SHIFTIN and SHIFTOUT provide an easy method of connecting to 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 sets the clock and data pins to switch to output mode. Then, SHIFTOUT sets the data pin to the next bit state to be output and generates a clock pulse by inverting the state of the clock pin; this allows the programmer to set the desired clocking edge by presetting the clock pin to the opposite state prior to the call. SHIFTOUT continues 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 ShiftMode parameter.

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

SHIFTOUT Timing

T0 - T1  ~ 6 µs  (allow pins to stabilize)
T1 - T2  ~ 6 µs  (allows receiver to capture bit)
X1 - X2, X2 - X3, ...  ~ 12 µs  (bit-to-bit timing)
Transmission Rate  ~ 83 kBits/Sec

Here is a simple example:

  SHIFTOUT RC.0, RC.1, MSBFIRST, 250

Here, the SHIFTOUT instruction will write to RC.0 (the DPin) and will generate a clock signal on RC.1 (the CPin). The SHIFTOUT instruction will generate eight clock pulses while writing each bit (of the 8-bit value 250) onto the Dpin. In this case, it will start with the most significant bit first as indicated by the ShiftMode value of MSBFIRST.

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

  LOW 0
  SHIFTOUT 0, 1, MSBFIRST, 250\4

Will output only the lowest (rightmost) four bits (%1010 in this case).


Related instruction: SHIFTIN
Related project: Thermometer