Please help with my MCP4922 DAC - Page 1
Forum section: electronics / projects

author: wigman27
author_rank: Regular Contributor
timestamp: October 11, 2013, 11:17:21 pm
content: Hi Again,As part of mydummy loadproject, I am planning on using the MCP4922 DAC. I have started testing it and have wired up as per the schematic attached below.I have written some code to convert the 12 bit value and split it into a primary byte which includes the 4 address and setting bits as well as the 4 MSB bits for the value, the secondary byte just includes the 8 LSB for the value. It is written for the Arduino Leonardo and uses the Arduino SPI library.The MCP4922DatasheetBit 15 = 0 for DAC A and 1 for DAC BBit 14 = Input Buffered set HighBit 13 = Output gain of 1Bit 12 = Active Low ShutdownBits 11 - 0 = Output ValueCode:#include <SPI.h>// set pin 10 as the slave select for the digital pot:const int dacChipSelectPin = 9;byte dacRegister = 0x70; // Sets default DAC registers B01110000, 1st bit choses DAC, A=0 B=1, 2nd Bit enables input Buffer, 3rd bit sets output gain to 1x, 4th bit controls active low shutdown. LSB are insignifigant here.void setup() {// set the dacChipSelectPin as an output:pinMode (dacChipSelectPin, OUTPUT);digitalWrite(dacChipSelectPin, HIGH);// initialize SPI:SPI.begin();SPI.setBitOrder(MSBFIRST);SPI.setDataMode(SPI_MODE0);}void loop() {setDac(3000,0);delay(1000);}void setDac(int value, int address) {int dacSecondaryByteMask = 0xFF; // Isolates the last 8 bits of the 12 bit value, B0000000011111111.byte dacPrimaryByte = (value >> 8) | dacRegister; //Value is a maximum 12 Bit value, it is shifted to the right by 8 bytes to get the first 4 MSB out of the value for entry into th Primary Byte, then ORed with the dacRegisterbyte dacSecondaryByte = value & dacSecondaryByteMask; // compares the 12 bit value to isolate the 8 LSB and reduce it to a single byte.switch (address) {case 0:dacPrimaryByte &= ~(1 << 7);break;case 1:dacPrimaryByte |= (1 << 7);}// disable interuptsnoInterrupts();// take the Chip Select pin low to select the DAC:digitalWrite(dacChipSelectPin,LOW);// send in the Primary Byte:SPI.transfer(dacPrimaryByte);// send in the Secondary ByteSPI.transfer(dacSecondaryByte);// take the Chip Select pin high to de-select the DAC:digitalWrite(dacChipSelectPin,HIGH);// Enable interuptsinterrupts();}I am not getting anything out of the DAC at all, unfortunately I have not been able to add a logic analyser to my collection as yet, I just wanted to see if I have stuffed up somewhere?ThanksLee

author: dannyf
author_rank: Super Contributor
timestamp: October 11, 2013, 11:26:17 pm
content: Code:Bit 12 = Active Low ShutdownBits 11 - 0 = Output Value...setDac(3000,0);What "3000" translates into for the bits?

author: wigman27
author_rank: Regular Contributor
timestamp: October 12, 2013, 12:56:35 am
content: For that particular example, the binary sent will be 0111101110111000

author: dannyf
author_rank: Super Contributor
timestamp: October 12, 2013, 12:05:59 pm
content: QuoteI can confirm for you that your code does output such binaries (0x7bb8).Your issue is somewhere else - read the datasheet and particularly the timing chart, as the fix is fairly simple.

author: tszaboo
author_rank: Super Contributor
timestamp: October 12, 2013, 04:54:49 pm
content: Quote from: wigman27 on October 11, 2013, 11:17:21 pmSomething is not correct here, the CS is on PB5

author: wigman27
author_rank: Regular Contributor
timestamp: October 12, 2013, 10:00:09 pm
content: Quote from: NANDBlog on October 12, 2013, 04:54:49 pmHi NANDBlog,Sorry, the comment is misleading, the code has been written in the Arduino IDE, and PB5 is io9 for the Leonardo.Sorry for the confusion there

author: wigman27
author_rank: Regular Contributor
timestamp: October 12, 2013, 10:07:16 pm
content: Quote from: dannyf on October 12, 2013, 12:05:59 pmHi dannyf,If your referring to the LADC pin being taken low after the rise of CS to latch the data, I don't need to as I have tied it low which will cause the data to latch on the rise of CS see page 15 of the data sheet. That's how Dave used it in his power supply and he managed to get it to work I believe.Any other ideas?

author: dannyf
author_rank: Super Contributor
timestamp: October 13, 2013, 12:35:27 am
content: Tying LDAC down will work but not the best approach for synchronizing both channels.In that case, I would check wiring, etc. posting a schematic would be helpful.

author: wigman27
author_rank: Regular Contributor
timestamp: October 13, 2013, 09:20:18 am
content: Quote from: dannyf on October 13, 2013, 12:35:27 amHi dannyf,Both channels are driving different parts of the circuit entirely so in this case I am not overly worried about syncing the outputs. I did post the schematic to the original post but I will put it in here again just in case.It turned out to be a faulty breadboard! Im over cheep breadboards... time for a good one I believe!Thanks very much to those who helped!Lee