Skip to main content
Skip table of contents

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.

using("device.labjack")
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.
ePut(ID,LJ_ioPIN_CONFIGURATION_RESET,0,0,0)
// 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.
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chSPI_CLOCK_FACTOR,0,0,0)
//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
GoOne(ID)
// 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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.