Problems with SPI example | LabJack
 

Problems with SPI example

5 posts / 0 new
Last post
Oli
Haemaflow's picture
Problems with SPI example

Hi all,

Newb here, having problems with a T7 using DAQfactory getting the SPI example to work on https://labjack.com/support/datasheets/t-series/digital-io/spi 

When I try to run the code below:

1

2 using("device.labjackm")
3 global string id = "any"

4

5 LJM_eWriteName(id, "SPI_CS_DIONUM", 0)            // Use DIO0 as chip select.
6 LJM_eWriteName(id, "SPI_CLK_DIONUM", 1)           // Use DIO1 as Clock
7 LJM_eWriteName(id, "SPI_MISO_DIONUM", 2)          // Use DIO2 as MISO
8 LJM_eWriteName(id, "SPI_MOSI_DIONUM", 3)          // Use DIO3 as MOSI

9

10 LJM_eWriteName(id, "SPI_MODE", 0)                 // Select mode zero.
11 LJM_eWriteName(id, "SPI_SPEED_THROTTLE", 65500)   // Set clock speed to ~100 kHz.
12 LJM_eWriteName(id, "SPI_OPTIONS", 0)              // No special operation
13 LJM_eWriteName(id, "SPI_NUM_BYTES", 1)            // Transfer one byte.

14

15 LJM_eWriteNameByteArray(id, "SPI_DATA_TX", 0)     // Load a test data byte.
16 LJM_eWriteName(id, "SPI_GO", 1)                   // Initiate the transfer

17

18 ? LJM_eReadNameByteArray(id, "SPI_DATA_RX")

 

I get the following error:

Write error with LabJack M: LJME_RECONNECT_FAILED: SPI_test Line 16 - Uncaught error in sequence SPI_test
02/02/18 11:01:31.000
: Read error with LabJack M: LJME_RECONNECT_FAILED
02/02/18 11:01:31.500
: Read error with LabJack M: LJME_RECONNECT_FAILED

Could someone please point out what I'm doing wrong?

Thanks!

LabJack Support
labjack support's picture
DAQFactory doesn't currently

Edit: The below workaround does not currently work because DAQFactory does not support LJM_eAddresses.

DAQFactory doesn't currently have support for LJM_eReadNameByteArray, as of writing.

The way to do it without LJM_eReadNameByteArray is to do this:

  • Use LJM_eAddresses to read an array of UINT16s from address 5050 (SPI_DATA_RX) with data type LJM_UINT16 (0).
  • For a given number of bytes to read, divide it by two and round up. (E.g. if you need to read 3 bytes, you have to read 2 UINT16s.)
  • Then, split each resultant UINT16s into two bytes using LJM_UINT16ToByteArray (which handles endianness of the processor).

I've attached an example C program, spi.c, which demonstrates this. Please let me know if you have any questions about it or other feedback.

File Attachment: 
Oli
Haemaflow's picture
Dear Labjack,

Dear Labjack,

Thanks for the above. DAQfac are still working on eReadNameByteArray, so I am trying to use your examples to make a dll for it in C# in the meantime. 

I'm now having problems getting the data from the encoder (https://www.mouser.com/ds/2/54/EMS22A-50229.pdf ). The encoder works fine on my arduino using the simple script attached. In my C# LJM code though I seem to be missing the first bit of data. Please see the the encoder signal diagram, and the full datasheet also, attached. I have SPI_MODE = 2, and OPTIONS = 0. I've tried THROTTLE set to a variety of values from 1 to 4900 to 20000 to 0, but none seems to fix this. I'm wondering if it could be to do with the tCLKFE or other timings for the encoder? If so is there a way to fix this?

Many thanks,

Oli

P.S. I haven't posted my LJM code as if you can't see an issue with the diagram, I will just try translating the arduino code across OK.

LabJack Support
labjack support's picture
Try cycling through the SPI

Try cycling through the SPI modes. 1 and 2 look like they should work. Try them all just to be sure.

Oli
Haemaflow's picture
Yes sorry that was all it was

Yes sorry that was all it was. Works great now, cheers!