Name:
shiftout
Syntax:

SPIOUT sclk,sdata,mode,(data{/ bits}, {data{/ bits},...})

SHIFTOUT sclk,sdata,mode,(data{/ bits}, {data{/ 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-3) which specifies the mode:

0 LSBFirst_L (LSB first, idles low)
1 MSBFirst_L (MSB first, idles low)
4 LSBFirst_H (LSB first, idles high)
5 MSBFirst_H (MSB first, idles high)

Data - is a variable/constant that contains the data to send.

Bits - (optional) is the number of bits to transmit. If omitted the default number of bits is automatically set to 8.

Description:

The spiout (shiftout is also accepted by the compiler) command is a bit-bang 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 'hspiout' command.

By default 8 bits are shifted out. A different number of bits (1 to 8) can be defined via the optional / bits. Therefore, for instance, if you require to shift out 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 MSB first method, the bits are shifted left (out from the left) and so when shifting just 4 bits they must be located in bits 7-4 (not 3-0). With the LSB method the bits are shifted out 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.

Some PICAXE microcontrollers do not have a shiftout 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:

spiout on X1/X2 parts

Example of using spiout, LSB first

Code Example:
spiout 1,2,LSB_First, (b1 / 8) ‘ clock 8 bits from b1
Copy Code Submit an Example

Shiftout LSB first

Example of using shiftout, LSB first

Code Example:
‘***** 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)


‘ ***** Shiftout LSB first *****
shiftout_LSBFirst:
	for counter = 1 to bits 	‘ number of bits
	mask = var_out & 1 		‘ mask LSB
	low sdata 			‘ data low
	if mask = 0 then skipLSB
	high sdata 			‘ data high
skipLSB: pulsout sclk,1 		‘ pulse clock for 10us
	var_out = var_out / 2 		‘ shift variable right for LSB
	next counter
	return
Copy Code Submit an Example

Shiftout MSB first

Example of using shiftout, MSB first

Code Example:
‘***** 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)


‘ ***** Shiftout MSB first *****
shiftout_MSBFirst:
	for counter = 1 to bits 	‘ number of bits
	mask = var_out & MSBValue 	‘ mask MSB
	high sdata 			‘ data high
	if mask = 0 then skipMSB
	low sdata 			‘ data low
skipMSB: pulsout sclk,1 		‘ pulse clock for 10us
	var_out = var_out * 2 		‘ shift variable left for MSB
	next counter
	return
Copy Code Submit an Example

Submit Your Own Code!

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

Comments

  • Login to leave a comment.