Library
#include <SHIFT.bas>
Interface
DIM listIN(MAXshiftARRAY)
' values to be shifted in
DIM listOUT(MAXshiftARRAY) ' values to be
shifted out
DIM shiftCounts(MAXshiftARRAY) ' bit counts for
each value (0 assumed to be 8 bits), 1-32 allowed
' cnt is the number of elements
SUB SHIFTOUT (OUTpin, CLKpin, LSBfirst,
cnt)
SUB SHIFTIN (INpin, CLKpin, LSBfirst,
cnt)
Description
LSBfirst selects the bit order for the SHIFT
routines.
A #define is used to set clock mode #define
SHIFTclkNEGATIVE will invert the normally low clock. To use a normally
high clock this #define must be placed before the #include
<SHIFT.bas>
Another #define can be used to sample data before the
clock, #define SHIFTpreSample. The default case is to sample data after
each clock.
SHIFTIN can be used for devices that are not covered by
SPI, I2C or 1-Wire. Data is shifted in on
INpin, and a positive
clock is sent on
CLKpin for each bit.
While most other hardware functions use bytes, SHIFTIN is oriented for bit
control. The shiftCounts defines the number of bits that will be shifted
in (1 - 32) for each corresponding element of the listIN array. If a
shiftCounts is 0, it is assumed to be 8. Data is shifted in at 300 Kbits/sec.
SHIFTOUT can be used for devices that are not covered by SPI, I2C or
1-Wire. Data is shifted out on OUTpin, and a positive clock is
sent on CLKpin for each bit.
While most other hardware functions use bytes, SHIFTOUT is oriented for bit
control. The shiftCounts array defines the number of bits that will be
shifted out (1 - 32) for each corresponding element of the listOUT array. If
shiftCounts is 0, it is assumed to be 8 bits.
NOTE*** these shift modes are compatible with
SHIFTIN, BUT not the same as PBASIC
Data is shifted out of the device at 300 Kbits/sec.
Example
#include <SHIFT.bas>
'...
' use
SHIFTIN/OUT to control an SPI EN28J60 connected on pins 3,4 -- 6 as
CS
listOUT(0) = 2
shiftCounts(0) = 3
listOUT(1) =
&H1b
shiftCounts(1) = 5
listOUT(2) = y
shiftCounts(2) =
8
io(6)=0 '
used as CS
shiftout (3,4,1,3) ' set reg
&H1B to y
io(6)=1
listOUT(0) = reg
shiftCounts(0) =
8
io(6)=0
shiftout
(3,4,1,1)
'select the register
shiftin
(5,4,0,1)
'and read it back
x = listIN(0)
io(6)=1
Here is an example for a device (93LC46) which is byte oriented except
for the commands. So the commands are sent with SHIFTOUT, and data
transferred with SPIIN or SPIOUT. CS is manually controlled in this
example (it is also positive true).
#include <SHIFT.bas>
#include
<SPI.bas>
'...
DIM inbuf(20) as string
DIM outbuf(20)
as string
' mixed SPI, SHIFT example for a 93LC46 connected
to pins
11-14
IOI(14)=1
' CS to 93LC46
listOUT(0) = &H260
shiftCounts(0) = 10
SHIFTOUT(12,13,0,1) '
write enable
IO(14)=0
listOUT(0) =
&H280 '
count still 10
outbuf(0) = $41
IO(14)=1
SHIFTOUT(12,13,0,1)
' set write to address 0
SPIOUT (-1, 13, 12, 0, 1, outbuf) ' send a byte of
data
IO(14)=0
wait(20)
' allow for write time
IO(14)=1
listOUT(0) = &H300
SHIFTOUT(12,13,0,1)
' read addr 0
SPIIN (-1, 11, 13, 12, 0, -1, "", 10,
inlist) ' read 10 bytes of data
IO(14)=0