Shin
Syntax
Shin Data_Pin, Clock_Pin, Mode, [ result { \bits } { ,result { \bits }...} ]
or
Assignment Variable = Shin Data_Pin, Clock_Pin, Mode, Shifts
Overview
Shift data in from a synchronous-serial device.
Parameters
Data_Pin is a Port.Pin constant that specifies the I/O pin that will be connected to the synchronous-serial device's data output. This pin's I/O direction will be changed to input and will remain in that state after the instruction is completed. Clock_Pin is a Port.Pin constant that specifies the I/O pin that will be connected to the synchronous-serial device's clock input. This pin's I/O direction will be changed to output. Mode is a constant that tells Shin the order in which data bits are to be arranged and the relationship of clock pulses to valid data. Below are the symbols, values, and their meanings: -
Symbol
Value
Description
MsbPre
MsbPre_L
Shift data in highest bit first. Read data before sending clock. Clock idles low
LsbPre
LsbPre_L
Shift data in lowest bit first. Read data before sending clock. Clock idles low
MsbPost
MsbPost_L
Shift data in highest bit first. Read data after sending clock. Clock idles low
LsbPost
LsbPost_L
Shift data in highest bit first. Read data after sending clock. Clock idles low
MsbPre_H
Shift data in highest bit first. Read data before sending clock. Clock idles high
LsbPre_H
Shift data in lowest bit first. Read data before sending clock. Clock idles high
MsbPost_H
Shift data in highest bit first. Read data after sending clock. Clock idles high
LsbPost_H
Shift data in lowest bit first. Read data after sending clock. Clock idles high
Result is a bit, byte, or word variable in which incoming data bits will be stored. Bits is an optional constant specifying how many bits (1-16) are to be input by Shin. If no bits entry is given, Shin defaults to 8 bits. Shifts informs the Shin command as to how many bit to shift in to the assignment variable, when used in the inline format.
Notes.
Shin provides a method of acquiring data from synchronous-serial devices, without resorting to the hardware SPI modules resident on some devices. 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 such as ADCs, DACs, clocks, memory devices, etc.
The Shin command causes the following sequence of events to occur: - Makes the clock pin output low. Makes the data pin an input. Copies the state of the data bit into the msb (lsb-modes) or lsb (msb modes) either before (-pre modes) or after (-post modes) the clock pulse. Pulses the clock pin high. Shifts the bits of the result left (msb- modes) or right (lsb-modes). Repeats the appropriate sequence of getting data bits, pulsing the clock pin, and shifting the result until the specified number of bits is shifted into the variable.
Making Shin 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.
Symbol Clock_Pin = PORTB.0
Symbol Data_Pin = PORTB.1
Shin Data_Pin, Clock_Pin, MsbPre, [Var1]
' Shift in msb-first, pre-clock.
In the above example, both Shin instructions are set up for msb-first operation, so the first bit they acquire ends up in the msb (leftmost bit) of the variable.
The post-clock Shift in, acquires its bits after each clock pulse. The initial pulse changes the data line from 1 to 0, so the post-clock Shift in returns %01010101.
By default, Shin acquires eight bits, but you can set it to shift any number of bits from 1 to 16 with an optional entry following the variable name. In the example above, substitute this for the first Shin instruction: -
Shin Data_Pin, Clock_Pin, MsbPre, [Var1\4] ' Shift in 4 bits.
Some devices return more than 16 bits. For example, most 8-bit shift registers can be daisychained together to form any multiple of 8 bits; 16, 24, 32, 40... You can use a single Shin instruction with multiple variables.
Each variable can be assigned a particular number of bits with the backslash (\) option. Modify the previous example: -
' 5 bits into Var1; 8 bits into Var2.
Shin Data_Pin, Clock_Pin, MsbPre, [Var1\5, Var2]
Print "1st variable: ", Bin8 Var1
Print "2nd variable: ", Bin8 Var2
Inline Shin Command.
The structure of the inline Shin command is: -
Var = Shin dpin, cpin, mode, shifts
DPin, CPin, and Mode have not changed in any way, however, the Inline structure has a new operand, namely Shifts. This informs the Shin command as to how many bit to shift in to the assignment variable. For example, to shift in an 8-bit value from a serial device, we would use:
Var1 = Shin Data_Pin, Clock_Pin, MsbPre, 8
To shift 16-bits into a Word variable: -
MyWord = Shin Data_Pin, Clock_Pin, MsbPre, 16