11.3 - SPI Communications (Applies to UD-Series) | LabJack
« Close

Datasheets and User Guides

App Notes

Software & Driver


11.3 - SPI Communications (Applies to UD-Series)

LabJack devices support serial communications using their digital lines using the standard SPI synchronous format. This is a powerful but advanced feature of the LabJack, though the details of SPI are beyond the scope of this guide.

Following is sample code that demonstrates SPI usage. It uses:

  • FIO0 for the clock (CLK),
  • FIO1 for CS,
  • FIO2 for MOSI, and
  • FIO3 for MISO.

The code sends out a string of 16 bytes and prints the received string to the command / alert window. If you short MISO to MOSI, then the 16 bytes sent out are echoed back. If MISO is tied to GND, then all zeros are received and printed. If you tie MISO to VS or leave it unconnected, then all 255's are received and printed.

include("c:\program files\labjack\drivers\labjackud.h") 
global ID = 0 // use first found 
//First, we do a pin config reset to set the LabJack to factory defaults. 
// Configure the SPI communication: 
//Enable automatic chip-select control. 
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chSPI_AUTO_CS,1,0,0) 
//Mode A:  CPHA=0, CPOL=0. 
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chSPI_MODE,0,0,0) 
//125kHz clock. 
//MOSI is FIO2 
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chSPI_MOSI_PIN_NUM,2,0,0) 
//MISO is FIO3 
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chSPI_MISO_PIN_NUM,3,0,0) 
//CLK is FIO0 
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chSPI_CLK_PIN_NUM,0,0,0) 
//CS is FIO1 
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chSPI_CS_PIN_NUM,1,0,0) 
//Execute the requests on a single LabJack.  The driver will use a single low-level TimerCounter command to hand 
// now that its setup, do the communication.  Note that you can do this part in a separate sequence, and run mul 
// without the reconfiguring the SPI with the above code. 
// initialize the variables 
private numSPIBytesToTransfer=4 
private dataArray 
dataArray[0] = 170 
dataArray[1] = 138 
dataArray[2] = 85 
dataArray[3] = 21 
//Transfer the data.  The write and read is done at the same time. 
eGet(ID, LJ_ioSPI_COMMUNICATION, 0, @numSPIBytesToTransfer, @dataArray) 
// print the read to the command / alert window.  Of course you'll probably do something a bit more exciting wit 
? dataArray

Each time you run the script, the 4 bytes of dataArray will be written and then 4 bytes will be read back and printed to the command / alert window. As mentioned in the script comments, you can do the actual communication multiple times without re-running reconfiguration script at the top of this sample.

Note that the UD example code provides similar code to the above script in C.


How can I do a write only operation on SPI? Everywhere I have seen, there is eGet being used and the comment says that write and read are performed at the same time.

All I want to do is write 2 bytes to a slave device. I have a seperate feedback loop to see if my 2 bytes have made it thru or not.

SPI always writes and reads at the same time, so do your communication just like show above.  If you are not reading anything then you just don't worry about what is returned in the array.

I only need MISO pin configured.  Is it possible to run the code without assigning a pin for MOSI?

If you look at the actual low-level packet sent to the U3 (Section 5.2.15 of the U3 User's Guide), you can see that MOSI is part of it.  So if you don't specify a pin number for MOSI, some default will be used and that default might conflict with something else.  So you need to specify a pin number for MOSI that will not bother anything else.