I've been trying to work with an AD5685R DAC. Per that datasheet, to get DAC1
to output max voltage (5V), I would perform:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3); // DAC uses CPHA=1, CPOL=1
digitalWrite(CS, LOW);
SPI.transfer(0x31); // upper nibble sends a "write and update" command, lower nibble says "DAC1"
SPI.transfer(0xFF);
SPI.transfer(0xFC); // 14-bit DAC, last 2 bits are dontcares, make them 0
digitalWrite(CS, HIGH);
Replace 0xFFFC
with 0x0000
to get 0 volts out.
Sending this command as written somehow writes to DAC4
instead of DAC1
! and while 5V will come out when I write 0xFFFC
, I get to 2.5V when writing 0x0000
instead of 0V.
If I command this with a Bus Pirate and directly write the bytes out, it works exactly as desired.
I used the Bus Pirate to sniff the SPI traffic and it turns out I'm not getting what I've commanded. Rather than 0x31 0x00 0x00
for instance, I get 0x18 0x80 0x00
.
If we break this out into bits:
0x310000: 0011 0001 0000 0000 0000 0000 // commanded
0x188000: 0001 1000 1000 0000 0000 0000 // actually sent
So the command actually sent out is shifted by 1 bit!
Here's the bit pattern for 16383 counts (i.e., 2^14-1):
0x31FFFC: 0011 0001 1111 1111 1111 1100 // commanded
0x18FFFE: 0001 1000 1111 1111 1111 1110 // actually sent
The opcode corresponding to 0x18
is "write to input register 4" (the output occurs because I've pulled the /LDAC
pin permanently low, see the datasheet), which explains why I'm getting an output on DAC4
instead of DAC1
.
This occurs with both a Sparkfun Pro Micro and an Arduino Mega 2560.
What is going on here? Why is the Arduino not sending out what I've actually commanded? Again, writing the hex codes directly to the DAC using a Bus Pirate works perfectly.
SPI_MODE2
. Arduino's SPI page doesn't make clear whatCPHA=0
vsCPHA=1
is (rising vs falling edge active). But maybe I'm just having a hard time comprehending words... Or, the comments on the AD5685R's sample code is incorrect. – Dang Khoa Nov 24 '14 at 15:21