Introduction: How to Use an IC DAC
There are a number of tutorials out there that explain how to use an R-2R resistor ladder as a Digital to Analog converter (DAC), such as this one, but not very many are out there that explain how you would use a DAC that is an Integrated Circuit. (Let me know if you know of a good one though).
And why not? Resistor ladder DACs are only as precise as how close the individual resistors match their theoretically identical twin resistors. I imagine that most people, such as myself, do not have a box of military grade resistors available to use in random projects. Don't get me wrong; a resistor ladder that you create yourself will likely work just fine in most hobby applications... but what if you wanted a DAC that could produce multiple outputs or a high 16 bit resolution, soldering your own resistors onto a circuit board may not be the most ideal.
Step 1: A Small Amount of Background
The PmodDA4 is engineered around Analog Devices' AD5628 DAC. Rather than having a precisely engineered resistor ladder like other IC DACs, this particular DAC uses a resistor string where the bits that are given to the DAC via code will close or leave open switches connecting the resistor string to the output voltage.
But we're not here to learn the theory of why the DAC works; instead, we're going to learn how to make the DAC work.
Step 2: So, Which Pin Does What?
This brings us to the five connected male pins on the PmodDA4 (one of them is not connected).
On pin 1 we have the "SYNC" pin. This is essentially the "Chip Select" pin used in the SPI protocol (if you are not familiar with the theory behind SPI, you can learn about it here). The second pin is the "DIN" pin which stands for "Data Input". As you likely guessed, this is where we will send the digital signal we wish to be turned into an analog signal. It is also known as the "Master Out, Slave In" (MOSI) pin in SPI.
Pin 3 is not connected but would normally be the "Master In, Slave Out" (MISO) pin in SPI. The reason it is not connected is because we are only converting digital signals from the host board to analog output signals as opposed to the other way around which would involve using a Analog-to-Digital Converter (ADC).
So why do we even have this pin on the Pmod in the first place if it's not connected? This is mostly for convenience; because people will likely be using the SPI protocol to communicate with the DAC, it needs to be able to fit into all of the standard SPI ports (I don't know of a host board that only offers five pins on their SPI port).
The fourth pin is the "SCLK" or the serial clock and is twin "SCK" pin in SPI. The particular chip in the PmodDA4, Analog Devices AD5628, collects data from the data input line on the falling edge of the serial clock. The remaining two pins are the ground and positive power pins, respectively.
Step 3: Proper Communication
On the other side of the module we have our eight output pins (labeled A through H) which we can either output to individually or output to all of them simultaneously. But how does the Pmod know which output we want to use?
If we look at the datasheet for the chip on pages 21 and 22, we can see that in our communication with the chip we will end up sending a series of "command" bits to let the chip know what we plan to do as well as a series of address bits to indicate which output we want our analog signal to appear on, and finally the 12 bits of digital signal (since we are using the AD5628) we want to convert into an analog signal. The "Don't cares" (bits that need to be sent but won't actually be assessed) that frame our information are essentially a buffer to help the device know that data is forthcoming or ending.
For those of you that might not know, the 12 bits are used to divide output analog voltage into incremental steps; 4096 steps in fact (2^12 = 4096).
Step 4: A Necessary First Step
However, if we tried to just send our desired following the style of 4 "Don't Care" bits, 4 "Command bits", 4 "Address" bits, 12 "Value" bits" and 8 more "Don't Care" bits, we would find, just like I did, that the DAC is apparently doing nothing whatsoever. Awkward.
The reason for this, as I eventually realized after searching through the datasheet, is because of a Voltage Reference pin on the Analog Devices Chip. It is this pin that determines to what scale the given digital values are working on. On page 9, according to the Table 6 "Pin Function Descriptions", it says that the Voltage Reference pin takes an external voltage as it's reference by default.
Since we don't easily have a way to provide that external reference voltage, we have to tell the chip to use it's internal reference voltage via a special command. Continuing to look through the datasheet, we can finally find on page 25 (after being directed there from the Internal Reference Register Section on page 24), in Tables 10 and 11 the series of 32 bits that we need to send to set up the Internal Reference Voltage. That particular command is shown in the picture, taken from the datasheet, above.
Step 5: Utilizing Our DAC
So now that our DAC is ready to operate correctly, if we wished to send a signal that sent a full strength analog signal (2.5 V for this particular device) and incrementally decreasing signal strength in a continual loop we would type the the code provided in the text file below (it was too big to put into a step).
Note that you may have to adjust the digital signals you are sending to get different analog values depending on what LEDs you are using. For example, these green LEDs take much more current and voltage to light up than red LEDs, so this code in combination with the red version of LEDs will keep them at full brightness for 3 out of the 4 different values, as opposed to the nice increments you can see with the green LEDs.
Step 6: Some Parting Thoughts
If you have any questions or thoughts about DACs, please feel free to comment below and I will do my best to help you out. To see what other cool things Digilent is up to and to find out more about the various Pmods that Digilent offers, check out the Digilent Blog!