arduino how to use spi drive DAC8522

should I use SPI.h or Icreate a new library file

ddsff:
should I use SPI.h or Icreate a new library file

you should be able to use the SPI.h library.

from the data sheet:

that's how I would wire it(untested):

and code it for example(also untested):

#include "SPI.h"

/* SPI pins
  #define MOSI 11
  #define MISO 12
  #define SCK 13
*/
#define CS 10 //chip select pin
#define LD 9 //chip load pin
#define RS 8 //chip reset pin
#define MSB 8 //chip MSB pin
const uint8_t W_DACA = B10100000; //Software decode, Channel A
const uint8_t W_DACB = B11000000; //Software decode, Channel B

uint8_t DAC_write(uint8_t channel, uint16_t mV) {
  byte xbyte;

  if (mV < 4096) { //12 bit resolution i.e max value = 4095
    if (channel == 1) {
      xbyte = (mV >> 8) & 0x0F; //store DACvalue [11..8] to xbyte[3..0]
      xbyte |= W_DACA;          //Add DAC configuration (DAC_A selected)
    }
    else if (channel == 2) {
      xbyte = (mV >> 8) & 0x0F; //store DACvalue [11..8] to xbyte[3..0]
      xbyte |= W_DACB;    //Add DAC configuration (DAC_B selected)
    }

    //Select DAC Chip
    digitalWrite(CS, LOW);

    //Send High Byte
    SPI.transfer(xbyte);

    //Send Low Byte
    xbyte = mV;
    SPI.transfer(xbyte);

    //Deselect DAC Chip
    digitalWrite(CS, HIGH);

    //output voltage on selected channel
    digitalWrite(LD, LOW);
    digitalWrite(LD, HIGH);

    return 1;
  }
  else
    return 0;

}

void Reset_DAC(uint8_t msb) {
  digitalWrite(MSB, msb); //0: Resets DAC Registers to Zero, 1: DAC Registers to Half Scale (800H)
  digitalWrite(RS, LOW);
  digitalWrite(RS, HIGH);
}

void setup() {

  //Define SPI IOs (optional)
  //pinMode(MOSI, OUTPUT);
  //pinMode(MISO, INPUT);
  //pinMode(SCK, OUTPUT);

  //initialising the other pins
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
  pinMode(LD, OUTPUT);
  digitalWrite(LD, HIGH);

  //if intending to used reset feature, RS and MSB also need to be initialised
  pinMode(RS, OUTPUT);
  digitalWrite(RS, HIGH);
  pinMode(MSB, OUTPUT);
  digitalWrite(MSB, HIGH);

  //Initialise SPI interface
  //SPI clock speed:10MHz, Data Shift:MSB First, Data Clock Idle: SPI_MODE0
  SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));

  DAC_write(1, 1000); //sets channel A to 1V
  DAC_write(2, 2500); //sets channel B to 2.5V

}

void loop() {

}

I am so sorry,but i am so careless that I should write DAC8552(a 16 bit da) but I write a DAC8522(a 12 bit da).It is wrong.

ddsff:
I am so sorry,but i am so careless that I should write DAC8552(a 16 bit da) but I write a DAC8522(a 12 bit da).It is wrong.

no matter... the datasheet seems to apply the same principle I used in the code I posted... hopefully you can figure it out! :slight_smile:

PS for that chip, no need to connect the MOSI pin since it does not exist on it! :slight_smile:

sherzaad:
no matter... the datasheet seems to apply the same principle I used in the code I posted... hopefully you can figure it out! :slight_smile:

PS for that chip, no need to connect the MOSI pin since it does not exist on it! :slight_smile:

thank you.iam trying to transfer this code