Page 1 of 1

Interfacing a DAC088S085 over SPI help

Posted: Mon Jun 24, 2019 6:34 pm
by charels
Hi all,

I could use some help interfacing over spi with this chip. Mostly I need help understanding how to set up spi with C++. My current setup is a raspberry pi controlling a DAC for a precision voltage control of pressure controllers. I am new to SPI and am unsure how to set up the initial variables. I have an example of spi for the mcp3008 included.

However, I am having a hard time translating this example to a useable SPI interface for my DAC. I cannot get any control over the chip but that's because I am using the code for adc almost exactly and not sure what to change.

Also, if you know any good spi example code please link them.

LINKS
adc example: http://www.hertaville.com/interfacing-a ... ing-c.html
dac page: http://www.ti.com/product/DAC088S085

Additional Info:
- Raspberry PI 3
- Qt 5.3.2

Re: Interfacing a DAC088S085 over SPI help

Posted: Thu Aug 01, 2019 11:54 pm
by charels
Solved

Go check out the attached code to figure out how to get the spi part of this software functions. Can get solid performance out of the DAC and the code has been reliable.

Code: Select all

bool dac::dacOpen()
{

   // Open the SPI connection
   if (!_spi.spiOpen("/dev/spidev0.0", SPI_MODE_0, 1000000, 8)) //CS1 DAC
       return false;

   // Set the DAC in write through mode (WTM)
   unsigned char data[2];  // 2 bytes needed for one transfer
   data[0] = 0b10010000;   // first byte transmitted
   data[1] = 0;            // second byte transmitted.

   if (!_spi.spiWrite(data, sizeof(data)))
   {
       _spi.spiClose();
       return false;
   }

   return true;
}

bool dac::dacWrite(int nDAC, int nValue)
{
   unsigned char data[2];      // 2 bytes needed for one transfer
   data[0] = (nDAC & 7) << 4;  // first byte transmitted has the DAC number in the high nibble
   data[1] = 0;                // second byte transmitted


   //8-bit code
   // Add in the data payload
//   data[0] |= (nValue >> 4) & 0xF;
//   data[1] |= (nValue & 0xF) << 4;

   //12-bit code
       data[0] |= (nValue >> 8) & 0xF;
       data[1] |= (nValue & 0xFF);

   if (!_spi.spiWrite(data, sizeof(data)))
       return false;

   return true;
}

Re: Interfacing a DAC088S085 over SPI help

Posted: Tue Aug 06, 2019 4:29 pm
by charels
NOTE

Just realized I didn't add a piece of info needed in case you try this. The original spi code for the ADC has a bug and needs to set a certain variable or will throw errors. This has the solution. This also solves an issue with MCP3008.

https://raspberry-projects.com/pi/progr ... -interface

In case the links ever breaks, the bug is an unset variable for the spi communication.

Here's a quote from the page:

"Error – Problem transmitting spi data..ioctl: Invalid argument

Due to changes in the underlying library the spi_ioc_transfer struct now needs to be initialised to NULL, and a hacky fix is to add this to the beginning of the for loop (this has been done in the code example above)

memset(&spi, 0, sizeof (spi));"