This object is provided under license. By downloading this object, you are agreeing to the terms of the MIT License.
User Reviews:
Huey commented:
I started using this code in a data logger project and it was a great jump start. I did have a little trouble when logging at a high rate of speed and ended up adding 2 lines to the code below to correct the problem.
PRI transfer(read,status,data): val | mask '
'/**
' * Transfer data to/from Vinculum USB Host in SPI mode, using seperate data in and data out pin
' *
' * @param read True for read, false for write.
' * @pram status False to access command register, true to access status register.
' * @param data If write, byte to write. If read, not used.
' * @return 9bit value, bit0 is 0 if byte written succesfully or new data read
' * If read, b8-b1 is byte read
' */
'' For read, send 110, then read 8 bits (MSB first), read statusbit (total 12 clockpulses)
'' For write, send 100, then write 8 bits (MSB first), read statusbit (total 12 clockpulses)
repeat 'line added by Huey ---------------------------------------------------------------
val := 0
data := $800 | ($400 & read) | ($200 & status) | (data << 1)
mask := $800
outa[SPI_CLK]~~ ' At least one clock after deselecting
outa[SPI_CLK]~
outa[SPI_CS]~~ ' Select chip
repeat 12 ' Start, direction, address, 8 data, status
outa[SPI_SDO] := (data & mask) > 0
val |= (mask & (ina[SPI_SDI] > 0))
outa[SPI_CLK]~~ ' Data valid on positive edge of clock
outa[SPI_CLK]~
mask >>= 1
outa[SPI_CS]~ ' Deselect chip
outa[SPI_CLK]~~ ' At least one clock after deselecting
outa[SPI_CLK]~
while (((val & $001) > 0) AND NOT ($400 & read)) 'line added by Huey -----------------------------------------------
return val
I also added the following code to get things started during initialization. This is only called in my program 1 time after power up.
PUB flush
'very important to have this section in the code for logger to work on power up
waitcnt(clkfreq/1000 + cnt)
repeat
if readByte <> -1 'if new data read
waitcnt(clkfreq/1000 + cnt)
else
quit
Overall it worked great.
Huey
February 21, 2010 12:53 a.m.