Another SPI Language Question...

Hello!

Okay, I'm a bit stuck here. I cant get my Arduino to communicate via SPI with a DAC and could really use some advice or direction.

I recently acquired the MCP4912 two channel DAC (data sheet = http://ww1.microchip.com/downloads/en/DeviceDoc/22250A.pdf). Programming my Arduino to talk to this DAC seemed straight forward but I'm having a really difficult time making it happen. Here are the specific problems I'm having:

  • I cant seem to definitively establish whether this DAC uses MSB or LSB to communicate
  • I dont know how to use the SPI.h library to send 16 bits of data
  • I'm not sure what speed to set the SPI clock to

I've sifted through the data sheet and found what bit corresponds to what function:

0 through 1 = n/a
2 through 11 = output voltage value
12 = shutdown switch (I want to set to 1)
13 = gain switch (I want to set to 1)
14 = buffer switch (I want to set to 0)
15 = A or B channel switch (A = 0 and B = 1)

I'm just toying around with this thing but here's the code thus far:

#include <SPI.h>

void setup()
{
  pinMode(3,OUTPUT);
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  Serial.begin(9600);
}

void loop()
{
    digitalWrite(3,LOW);
    SPI.transfer(//output voltage value//);
    digitalWrite(3,HIGH);   
    Serial.print("Channel A: ");
    Serial.print(analogRead(A0));
    Serial.print("\t\tChannel B: ");
    Serial.println(analogRead(A1));
}

Please advise.

alexxander_foster:
I'm just toying around with this thing but here's the code thus far:

Which doesn't compile:

sketch_jun15a.cpp: In function 'void loop()':
sketch_jun15a:18: error: expected `)' before ';' token

What pins have you connected your DAC to? (in detail)

alexxander_foster:

  • I cant seem to definitively establish whether this DAC uses MSB or LSB to communicate

See figure 1-1 on page 7 of the datasheet.

The timing diagrams in the datasheet show its MSB-first (this is the usual order for SPI).

You need to send 16 bits - pull CS low, send the first byte, send the next byte, pull CS high again.

You also need to ensure you take the chip out of shutdown mode. Bit 12 must be 1. Also, make sure the LDAC pin is tied to ground, or you are driving it properly to transfer the voltage settings to the outputs.

I have knocked up a little library for this family of chips:

http://hacking.majenko.co.uk/MCPDAC

Wow! A lot of extremely helpful responses! Thank you everybody for helping me verify that this IC uses MSB.

And thank you very much for your help majenko! As soon as I loaded your library it worked perfectly! Predictable and easy to integrate for a noob, like myself. This library you created is going to save me a lot of time and coding! Thank you again for making that available!

I will also set the LDAC latch to ground.

majenko:
I have knocked up a little library for this family of chips:

http://hacking.majenko.co.uk/MCPDAC