6

I have an AD5685R DAC that I am trying to communicate with over SPI. Per the datasheet,

...These [24] bits are transferred to the input register on the 24 falling edges of SCLK and are updated on the rising edge of /SYNC

where /SYNC is used as chip-select.

I believe Arduino's SPI.transfer waits to receive 8 bits after sending 8 bits. But for this particular DAC, I need to send 24 straight bits on 24 clock edges before releasing the chip-select. Is this possible?

migrated from electronics.stackexchange.com Nov 20 '14 at 23:34

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

11

I believe Arduino's SPI.transfer waits to receive 8 bits after sending 8 bits.

Incorrect. With SPI, a byte is clocked in at the same time as a byte is clocked out. It may be that this byte is simply a dummy byte though, if the slave has nothing to say.

So for 24 bits, simply clock out 3 bytes without deasserting the select. Since SPI is a synchronous protocol there should be no issue with having a delay in between bytes provided the select is maintained.

7

In the SPI protocol, you get one byte back for every one you send. If you are not interested in the response from a device, you can just ignore the return bytes.

SPI has no handshaking. You just must send no faster than the slave device can handle. In the case of the AD5685R, that is 50 MHz.

For multibyte transfer, you keep the chip select asserted (low) between every byte by setting the transferMode to SPI_CONTINUE in the SPI.transfer call. For the last byte, you will deassert the chip select (set it high) by setting the transferMode parameter to SPI_LAST in the SPI.transfer call..

  • Unless you have one of those weird SPI devices with a positive select. – Ignacio Vazquez-Abrams Nov 20 '14 at 18:30
6

Note that there is also a version of SPI.transfer for multi-byte (buffer) transfers:

SPI.transfer(buffer, size)

I used the buffer transfer in my library for the TLC59711 (see code) and found the buffer transfer faster than transferring bytes individually using SPI.transfer(val); the switch to buffer transfers is one of the reasons my library is about 3.5 times faster than a previous library for the TLC59711.

  • This is relatively new, works in Arduino 1.8, not sure about 1.0. – qwr Jan 14 at 18:21
3

Looks like it just set cs low and do three byte transfers then bring it high. Here's someone's example on how to do it:

http://www.reigndesign.com/blog/controlling-a-10-bit-digital-potentiometer-via-spi-with-arduino/

Code below taken from above page.

#include <SPI.h> 

const int csPin = 10;

void setup() {

 SPI.begin();
 SPI.setBitOrder(MSBFIRST); //We know this from the Data Sheet

 pinMode(csPin,OUTPUT);
 digitalWrite(csPin, HIGH);
}

void loop() {
 for(int i=0; i<1023; i++) { 
  digitalPotWrite(0,i);
  delay(10);
 } 
}

void digitalPotWrite(int address, int value) {
 digitalWrite(csPin, LOW); //select slave
 byte command = 0xB0; //0xB0 = 10110000 
 command += address; 
 SPI.transfer(command); 
 byte byte1 = (value >> 8);
 byte byte0 = (value & 0xFF); //0xFF = B11111111
 SPI.transfer(byte1);
 SPI.transfer(byte0);
 digitalWrite(csPin, HIGH); //de-select slave
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.