Connect max5312 spi dac to Arduino

Hi all,
I am trying to control a serial dac using arduino nano. I wrote my program but unable to get it working:

#include <SPI.h> 

const int ldacPin = 3;
const int sclkPin = 4;
const int dinPin = 5;
const int csPin = 6;
const int clrPin = 7;
const int uniPin = 8;
      
void setup() {

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

      pinMode(10,OUTPUT); //as pin 10 is not used
      pinMode(ldacPin,OUTPUT);
      pinMode(sclkPin,OUTPUT);
      pinMode(dinPin,OUTPUT);
      pinMode(csPin,OUTPUT);
      pinMode(clrPin,OUTPUT);
      pinMode(uniPin,OUTPUT);
 
 digitalWrite(csPin, HIGH);
 delay(timer); //100us delay as per MAX5312 datasheet
 digitalWrite(ldacPin, HIGH);
 digitalWrite(clrPin, HIGH);
 digitalWrite(dinPin, HIGH);
 digitalWrite(uniPin, HIGH); //To set DAC to unipolar mode pin 8 on the dac
}
 void loop() {
 
 SPI.transfer(0x4B33);
 /* Above: First 3bits(from msb) is control bits  last 12 bits is 2867 so expected the output  voltage is around 7 volts
 */
 delay(2);
 digitalWrite(csPin, LOW); 
 digitalWrite(sclkPin,HIGH);
 delay(2);
 digitalWrite(sclkPin,LOW);
 }

From the datasheet, the dac output is updated with control bits 0x400 (0100). For a 2867 code (0xB33), I set to transfer the full 16 bits in one go 0x4B33 when the CS is low and latch on the dropping edge of SCLK.
Thanks

If it's an SPI DAC, why are you not using the SPI pins: D13 = SCK, D12 = MISO, D11 = MOSI, and generally D10 is used for SS as it has to be set to an output on the master SPI device anyway.

Post a link to the MAXIM website for that device.
From this

 /* Above: First 3bits(from msb) is control bits  last 12 bits is 2867 so expected the output  voltage is around 7 volts
 */

I'd say you want this:

digitalWrite (ssPin, LOW); // pin 10
SPI.transfer (highByte (commandPlusData));
SPI.transfer (lowByte (commandPlusData));
digitalWrite (ssPin, HIGH);

and you put the 4 bits + 12 bits together into an int however you'd like.
Perhaps:
int command = 0bxxxx000000000000; // xxxx is command bits from datasheet Table 2
int data = 0b0000xxxxxxxxxxxx; // xxxx xxxx xxxx is desired DAC output.
int commandPlusData = command | data;

If I understand, on the arduino, the MOSI pin (11) is connected to the DIN pin on the max5312. The rest are MISO=DIN, ss=CS.
The max5312 datasheet: http://datasheets.maximintegrated.com/en/ds/MAX5312.pdf
I have inserted the suggestion as follow but still no luck

#include <SPI.h> 

const int ldacPin = 3;
const int clrPin = 7;
const int uniPin = 8;
const int ssPin = 10;
const int misoPin = 11; //To connect the DIN pin from the DAC
const int sclkPin = 13;

int command = 0b0100000000000000; // 0100  Load input and DAC registers from shift register; DAC output updated.
int data = 0b0000101100110011; // 2867 desired DAC output.
int commandPlusData = command | data;

void setup() {

  SPI.begin();
  SPI.setBitOrder(MSBFIRST); // from the Datasheet

  pinMode(ldacPin,OUTPUT);
  pinMode(sclkPin,OUTPUT);
  pinMode(misoPin,OUTPUT);
  pinMode(ssPin,OUTPUT);
  pinMode(clrPin,OUTPUT);
  pinMode(uniPin,OUTPUT);

  digitalWrite(ssPin, HIGH);
  delay(100); //100us delay as per MAX5312 datasheet
  digitalWrite(clrPin, HIGH);
  digitalWrite(misoPin, HIGH);
  digitalWrite(uniPin, HIGH); //To set DAC to unipolar mode pin 8 on the dac
}
void loop() {

  digitalWrite (ssPin, LOW); // pin 10
  SPI.transfer (highByte (commandPlusData));
  SPI.transfer (lowByte (commandPlusData));
  //digitalWrite (ssPin, HIGH);
  delay(2);
  digitalWrite(ssPin, LOW); 
  digitalWrite(sclkPin,HIGH);
  delay(2);
  digitalWrite(sclkPin,LOW);
  digitalWrite(ldacPin, LOW);
}

Datasheet says on DIN pin the clock data in on the rising edge of SCLK.
Thanks

If I understand, on the arduino, the MOSI pin (11) is connected to the DIN pin on the max5312. The rest are MISO=DIN, ss=CS.

That doesn't pass the sniff test. Why would MISO and MOSI both be connected to DIN?

MISO is not used, leave the pin unconnected.
Connect Arduino's MOSI to DIN on the chip.
DOUT from the chip is not connected to anything for now.

Get rid of these, SPI library sets them up:

const int misoPin = 11; //To connect the DIN pin from the DAC
const int sclkPin = 13;
  digitalWrite(misoPin, HIGH);

  pinMode(sclkPin,OUTPUT);
  pinMode(misoPin,OUTPUT);


  SPI.setBitOrder(MSBFIRST); // from the Datasheet
Don't need - MSBFIRST is default setting

Delete these - not needed with software command 0100:
LDAC & CLR only need to be HIGH:

  digitalWrite(ssPin, LOW); 
  digitalWrite(sclkPin,HIGH);
  delay(2);
  digitalWrite(sclkPin,LOW);
  digitalWrite(ldacPin, LOW);

Add to go with these:
  pinMode(ldacPin,OUTPUT);
digitalWrite (ldacPin, HIGH);

  pinMode(clrPin,OUTPUT);
digitalWrite (clrPin, HIGH);

Make those changes, repost your code.

I should have clarified that I connected pin 11 (MOSI on arduino nano) to the DIN (MISO onpin 2 on the DAC).
The latest code which still not working.

#include <SPI.h> 

const int ldacPin = 3;
const int clrPin = 7;
const int uniPin = 8;
const int ssPin = 10;
const int sclkPin = 13;

int command = 0b0100000000000000; // 0100  Load input and DAC registers from shift register; DAC output updated.
int data = 0b0000101100110011; // 2867 desired DAC output.
int commandPlusData = command | data;

void setup() {

  SPI.begin();
  
  pinMode(clrPin,OUTPUT);
  digitalWrite (clrPin, HIGH); //To clear DAC
    
  pinMode(ldacPin,OUTPUT);
  digitalWrite (ldacPin, HIGH);
  
  pinMode(uniPin,OUTPUT);
  digitalWrite(uniPin, HIGH); //Needed to set DAC to unipolar mode pin 8 on the dac
   
}
void loop() {

  digitalWrite (ssPin, LOW); // pin 10 - initiate serial communication
  SPI.transfer (highByte (commandPlusData));
  SPI.transfer (lowByte (commandPlusData));
  delay(2);
  digitalWrite(sclkPin,HIGH);//Data clocked rising edge
  delay(2);
  digitalWrite(sclkPin,LOW);
  digitalWrite(ldacPin, LOW);//Force update
}

Connections:
Arduino pins-------------------to DAC pins
D3--------------------16(LDAC)
D7--------------------15(CLR)
D8--------------------8(UNI/BIP)
D10--------------------3(CS)
D11--------------------2(DIN)
D13--------------------1(SCLK)
DOUT, SHDN are not connected.

Connect SHDN to +5.

Get rid of this, is taken care of by SPI library
const int sclkPin = 13;

Get rid of these - not needed with software command 0100 being used:
delay(2);
digitalWrite(sclkPin,HIGH);//Data clocked rising edge
delay(2);
digitalWrite(sclkPin,LOW);
digitalWrite(ldacPin, LOW);//Force update

Add this in setup:
pinMode (ssPin, OUTPUT);
digitalWrite (ssPIN, HIGH);

Add this after the SPI.transfers
digitalWrite (ssPin, HIGH);

I realised earlier after the post I had to connect SHDN to 5v just in case as it was ambiguous in the pin description or my understanding :slight_smile:
You are the man CrossRoads. Much appreciated.
It is now working.
My next step is now to have it to increment up to a set value.
Thanks for the help

No problem. All comes down to reading/understanding the datasheet.

The working incrementing code:

void loop() {
  
  for (int data=0b0000000000000000; data < 3891; data++){ //Increment upto data<xxxx
      commandPlusData = command | data;
  digitalWrite (ssPin, LOW); // pin 10 - initiate serial communication
  SPI.transfer (highByte (commandPlusData));
  SPI.transfer (lowByte (commandPlusData));
  digitalWrite (ssPin, HIGH);
  
}
}

So what does the rising voltage do for you?