Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

Trouble using SPI with U12


  • Please log in to reply
4 replies to this topic

#1 francoisdt

francoisdt
  • Members
  • 12 posts

Posted 26 May 2015 - 03:37 AM

Hi, 

 

I am currently trying to use the SPI bus of the Labjack U12 with C++ under Linux, ultimately to control MCP4151 or MCP4162 digital potentiometers, but for now I'm just trying to get SPI to work. 

 

Indeed, as I saw my standard wiring and a simple call to the Synch function wasn't working on the potentiometer, I wired the labjack to my arduino to do some more tests. 

 

First, a simple program setting the Arduino as SPI slave (found here http://www.gammon.com.au/spi ) didn't give any result. So I then tried to plug the different outputs of the labjack to the arduino's interrupt pin (2) and display something when it was triggered. 

 

I could therefore verified that taking low the CS pin is effectively working (though done twice for a single call to Synch), the SCK and MOSI outputs from labjack never triggered an interruption, while as was expecting them to do it during the communication process. 

 

Here is a code snippet of what I tried : 

 

  long idnum = -1;
  long mode=3;
  long msDelay=0;
  long husDelay=0;
  long controlCS=1;
  long configD=1;
  long demo=0;
  long csLine=0
  long csState=0
  

  long datatmp[18] = {0};
  datatmp[0] = 40 / 256;
  datatmp[1] = 40 % 256;
  
  
  
  errorcode = Synch(&idnum, demo, mode, msDelay, husDelay, controlCS, csLine, csState, configD, 2, datatmp);

Of course I used D15 as SCK and D13 as MOSI as I set configD to 1. 

 

If you have any suggestion about the way to use SPI they will be very welcome :)



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 26 May 2015 - 07:43 AM

Start with simple loopback testing.  With a jumper from MISO to MOSI (D13 to D14), you should read back whatever you write.  I just tested and this did work fine for me.  With D14 jumpered to GND you should read back 0s, and with D14 jumpered to +5V you should read back 255s.

 

I also put a scope on D14 to look at SCK, and could see that the clock was ~100kHz.



#3 francoisdt

francoisdt
  • Members
  • 12 posts

Posted 27 May 2015 - 06:13 AM

Thank you for your answer. I tried making the example you gave, but the Synch function always give me the same result, namely 0 and 0 in the first two cells of the data array, and the 16 others filled with 9999.

 

I tried pluging the MISO pin directly on the MOSI pin, or plugin it to the ground, but it always gave the same result. And when trying to wire it to +5V from Labjack, it shorted the Labjack. I tried to connect it to a digital pin from Arduino which was set to HIGH, but the result was the same...

 

Here is the exact code I used

#include <iostream>
#include "ljacklm.h"

const long demo = 0;			// Demo mode off
const long SPImode = 3;			// SPI Mode D
const long msDelay = 0;			// no delay
const long husDelay = 0;		// no delay
const long controlCS = 1;		// use synch to controll CS.
const long csLine = 1;			// CS line is D1
const long csState = 0;			// use standard !CS
const long configD = 1;			// Allow synch to set direction of D lines
const long numWriteRead = 2;	// Number of bytes to read/write.

void set_bit(long Dline, long state);
	
void SPI(long* data);

int main()
{
  char resp = 'y';
  long i;
  long result = 0;
  double voltage;
  long data[18] = {0};

  set_bit(0,1);				// start with conv line high
  set_bit(1,1);				// start with CS line high

  std::cout.precision(4);
	
  do{
    set_bit(0,0);			// start a conversion. conversion takes ~20 us
    set_bit(0,1);

    for(i=0; i<18; ++i)		// clear array.
      data[i] = 0;
    data[0] = 125;
    data[1] = 3;
    SPI(data);				// get data from ADS7813

    for(int cnt=0;cnt<18;cnt++)
      std::cout<<data[cnt]<<" ";
    std::cout<<std::endl;
     
  }while(resp == 'y' || resp == 'Y');

  return 0;
}
//------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------
void SPI(long* data)
{
  long IDnum = -1;
  long errorcode = 0;
  char error[50];

  errorcode = Synch(&IDnum,demo,SPImode,msDelay,husDelay,controlCS,csLine,csState,
		    configD,numWriteRead,data);

  if(errorcode) 
    {
      GetErrorString(errorcode, error);
      std::cout << "Error: " << error << std::endl;
    }
}

void set_bit(long Dline, long state)
{
  long errorcode = 0;
  long IDnum = -1;
  char error[50];

  errorcode = EDigitalOut(&IDnum,demo,Dline,1,state);

  if(errorcode) 
    {
      GetErrorString(errorcode, error);
      std::cout << "Error: " << error << std::endl;
    }
}
//------------------------------------------------------------------------------------------





 

I took the functions from the demo file ADS7813.cpp I found on your website.



#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 27 May 2015 - 11:03 AM

I tried your code with a D13 and D14 jumper and it was sending/receiving your { 125, 3 } bytes. Keep in mind that on the DB25 connector pinout 23 is D13/MISO and pinout 24 is D14/MOSI. You mentioned when you connected your MISO and MOSI lines you shorted your U12 which could happen if you mistakenly connected pinouts 13 and 14 which are +5 VS and GND. Try jumpering pinouts 23 and 24, and see if that helps your loopback test.

 

For a helpful table and image for the DB25 connector, take a look section 2.4 of the Users Guide:

 

http://labjack.com/s...users-guide/2.4



#5 francoisdt

francoisdt
  • Members
  • 12 posts

Posted 28 May 2015 - 02:16 AM

Ooh okay, I didn't stumble upon this table so I assumed that D13 was pin 14 of the DB25 connector and so on, since I noticed that D0 was pin 1...

 

I got everything to work now, and I even succeeded in talking to my digital potentiometers. 

 

Thanks for the help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users