Name:
shiftin
Syntax:

SPIIN sclk,sdata,mode,(variable {/ bits} {, variable {/ bits}, ...})

SHIFTIN sclk,sdata,mode,(variable {/ bits} {, variable {/ bits}, ...})

Sclk - is a variable/constant which specifies the i/o pin to use as clock.

Sdata - is a variable/constant which specifies the i/o pin to use as data.

Mode - is a variable/constant (0-7) which specifies the mode:

0 MSBPre_L (MSB first, sample before clock, idles low)
1 LSBPre_L (LSB first, sample before clock, idles low)
2 MSBPost_L (MSB first, sample after clock, idles low)
3 LSBPost_L (LSB first, sample after clock, idles low)
4 MSBPre_H (MSB first, sample before clock, idles high)
5 LSBPre_H (LSB first, sample before clock, idles high)
6 MSBPost_H (MSB first, sample after clock, idles high)
7 LSBPost_H (LSB first, sample after clock, idles high)

Variable - receives the data.

Bits - is the optional number of bits to transmit. If omitted the default is 8.

Description:

The spiin (shiftin also accepted by the compiler) command is a 'bit-bang' method of SPI communication on the X1 and X2 parts ONLY.

All other parts must use the sample program included below to duplicate this behaviour. For a hardware solution for X1/X2 parts see the 'hshin' command.

By default 8 bits are shifted into the variable. A different number of bits (1 to 8) can be defined via the optional / bits. Therefore, for instance, if you require to shift in 12 bits, do this as two bytes, one byte shifting 8 bits and the second byte shifting 4 bits. Note that if you are using the LSB first method, the bits are shifted right (in from the left) and so shifting just 4 bits would leave them located in bits 7-4 (not 3-0). With the MSB method the bits are shifted left (in from the right).

When connected SPI devices (e.g. EEPROM) remember that the data-in of the EEPROM connects to the data-out of the PICAXE, and vice versa.

Other PICAXE microcontrollers do not have a direct spiin (shiftin) command. However the same functionality found in other products can be achieved by using the sub procedures listed below.

Effect of increased clock speed

Increasing the clock speed increases the SPI clock frequency.

Applies To:
20X2, 28X1, 28X2, 40X1, 40X2 (all others by software implementation)
See Also:
Related Create:
Share:
Print:

spiin on an X1/X2 PICAXE

Example of using spiin, LSB first, Data Pre-Clock

Code Example:
	spiin 2,1,LSB_Pre_H, (b1 / 8) ; clock 8 bits into b1
Copy Code Submit an Example

Shiftin LSB first, Data Pre-Clock

Example of using shiftin, LSB first, Data Pre-Clock

Code Example:
; ~~~~~ SYMBOL DEFINITIONS ~~~~~
; Required for all routines. Change pin numbers/bits as required.
; Uses variables b7-b13 (i.e. b7,w4,w5,w6). If only using 8 bits
; all the word variables can be safely changed to byte variables.

;***** Sample symbol definitions *****
symbol sclk = 5				; clock (output pin)
symbol sdata = 7			; data (output pin for shiftout)
symbol serdata = input7			; data (input pin for shiftin, note input7)
symbol counter = b7			; variable used during loop
symbol mask = w4			; bit masking variable
symbol var_in = w5			; data variable used durig shiftin
symbol var_out = w6			; data variable used during shiftout
symbol bits = 8				; number of bits
symbol MSBvalue = 128			; MSBvalue =128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)

shiftin_LSB_Pre:

	let var_in = 0
	for counter = 1 to bits		; number of bits
	  var_in = var_in / 2		; shift right as LSB first
	  if serdata <> 0 then
	    var_in = var_in + MSBValue	; set MSB if serdata = 1
	  end if
	  pulsout sclk,1		; pulse clock to get next data bit
	next counter
	return
Copy Code Submit an Example

Shiftin MSB first, Data Pre-Clock

Example of using shiftin, MSB first, Data Pre-Clock

Code Example:
; ~~~~~ SYMBOL DEFINITIONS ~~~~~
; Required for all routines. Change pin numbers/bits as required.
; Uses variables b7-b13 (i.e. b7,w4,w5,w6). If only using 8 bits
; all the word variables can be safely changed to byte variables.

;***** Sample symbol definitions *****
symbol sclk = 5				; clock (output pin)
symbol sdata = 7			; data (output pin for shiftout)
symbol serdata = input7			; data (input pin for shiftin, note input7)
symbol counter = b7			; variable used during loop
symbol mask = w4			; bit masking variable
symbol var_in = w5			; data variable used durig shiftin
symbol var_out = w6			; data variable used during shiftout
symbol bits = 8				; number of bits
symbol MSBvalue = 128			; MSBvalue =128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)

shiftin_MSB_Pre:

	let var_in = 0
	for counter = 1 to bits		; number of bits
	  var_in = var_in * 2		; shift left as MSB first
	  if serdata <> 0 then 
	    var_in = var_in + 1		; set LSB if serdata = 1
	  end if
	  pulsout sclk,1		; pulse clock to get next data bit
	next counter
	return
Copy Code Submit an Example

Shiftin LSB first, Data Post-Clock

Example of using shiftin, LSB first, Data Post-Clock

Code Example:
; ~~~~~ SYMBOL DEFINITIONS ~~~~~
; Required for all routines. Change pin numbers/bits as required.
; Uses variables b7-b13 (i.e. b7,w4,w5,w6). If only using 8 bits
; all the word variables can be safely changed to byte variables.

;***** Sample symbol definitions *****
symbol sclk = 5				; clock (output pin)
symbol sdata = 7			; data (output pin for shiftout)
symbol serdata = input7			; data (input pin for shiftin, note input7)
symbol counter = b7			; variable used during loop
symbol mask = w4			; bit masking variable
symbol var_in = w5			; data variable used durig shiftin
symbol var_out = w6			; data variable used during shiftout
symbol bits = 8				; number of bits
symbol MSBvalue = 128			; MSBvalue =128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)

shiftin_LSB_Post: 

	let var_in = 0
	for counter = 1 to bits		; number of bits
	  var_in = var_in / 2 		; shift right as LSB first
	  pulsout sclk,1		; pulse clock to get next data bit
	  if serdata <> 0 then
	    var_in = var_in + MSBValue	; set MSB if serdata = 1
	end if
	next counter
	return
Copy Code Submit an Example

Shiftin MSB first, Data Post-Clock

Example of using shiftin, MSB first, Data Post-Clock

Code Example:
; ~~~~~ SYMBOL DEFINITIONS ~~~~~
; Required for all routines. Change pin numbers/bits as required.
; Uses variables b7-b13 (i.e. b7,w4,w5,w6). If only using 8 bits
; all the word variables can be safely changed to byte variables.

;***** Sample symbol definitions *****
symbol sclk = 5				; clock (output pin)
symbol sdata = 7			; data (output pin for shiftout)
symbol serdata = input7			; data (input pin for shiftin, note input7)
symbol counter = b7			; variable used during loop
symbol mask = w4			; bit masking variable
symbol var_in = w5			; data variable used durig shiftin
symbol var_out = w6			; data variable used during shiftout
symbol bits = 8				; number of bits
symbol MSBvalue = 128			; MSBvalue =128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)

shiftin_MSB_Post:

	let var_in = 0
	for counter = 1 to bits		; number of bits
	  var_in = var_in * 2		; shift left as MSB first
	  pulsout sclk,1		; pulse clock to get next data bit
	  if serdata <> 0 then 
	    var_in = var_in + 1		; set LSB if serdata = 1
	  end if
	next counter
	return
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.