Digital-to-Analog Conversion: Microchip MCP4921 Features General Overview Serial SPI Interface
Digital-to-Analog Conversion: Microchip MCP4921 Features General Overview Serial SPI Interface
Table of Contents
References:
Introduction
Before we begin watch the following instructional video on Digital Audio - you may be
surprised to discover that much of what you thought you knew about digitization is wrong.
http://www.wired.com/gadgetlab/2013/02/sound-smart-watch-this-excellent-primer-on-dig
ital-audio/
• Rail-to-Rail Output
Serial communication conforms to the SPI protocol Modes 0 and 3 as defined by the
ATmega328P datasheet. The coding of these devices is straight binary and the ideal output
voltage is given by Equation 1, where G is the selected gain (1x or 2x), DN represents the
digital input value and n represents the number of bits of resolution (n = 12).
The MCP4921 is designed to interface with the Serial Peripheral Interface (SPI) port of the
ATmega328P microcontrollers in SPI Mode 0 or Mode 3 as defined in Table 2 SPI Modes
(Table 18.2 of the ATmega328P datasheet). The MCP4921 datasheet refers to these as
modes 0, 0 and 1, 1 respectively (see Figure 3).
Commands and data are sent to the device via the SDI pin, with data being clocked-in on
the rising edge of SCK. The communications are unidirectional and, thus, data cannot be read
out of the MCP4921. The CS pin must be held low for the duration of a write command. The
write command/data consists of 16 bits and is used to configure the DAC’s control and data
latches. Figure 4 details the input registers used to configure and load the DACA registers.
Figure 4 Writing Command/Data Word Register
For the MCP4921 you should keep A/B bit 15 at logic zero and SHDN bit 12 at logic 1. Bits 14
(BUF) and 13 (GA), may be customized to your design solution as discussed in the next two
Sections.
selections, a gain of 1 V/V (GA = 1) or a gain of 2 V/V (GA = 0). The output range is ideally
0.000V to 4095/4096 * VREF when G = 1, and 0.000 to 4095/4096 * VREF when G = 2. The
default value for this bit is a gain of 2, yielding an ideal full-scale output of 0.000V to 4.096V
when utilizing a 2.048V VREF. Note that the near rail-to-rail CMOS output buffer’s ability to
approach AVSS and VDD establish practical range limitations. The output swing specification
in the Datasheet Section 1.0 “Electrical Characteristics” defines the range for a given load
condition.
the input buffer amplifiers, achieving a Buffered or Unbuffered mode. The default value for
this bit is unbuffered. Buffered mode provides a very high input impedance, with only minor
limitations on the input range and frequency response. Unbuffered mode provides a wide
input range (0V to VDD), with a typical input impedance of 165 kΩ w/7 pF.
//**************************************************************//
// Name : Analog In to 12bit SPI DAC Microchip MCP4921 //
// Author : Gary Hill //
// Date : 14 March, 2010 //
// Version : 1.0 //
// Reference(s): http://www.arduino.cc/playground/Code/Spi //
// Notes : Download Spi folder and place in //
// : arduino00nn/hardware/libraries folder //
//****************************************************************
// SPI Interface SS_PIN(PB2), SCK_PIN(PB5), MOSI_PIN(PB3), MISO_PIN
// Arduino Pin 10 13 11 12
// MCP4921 DAC SS SCK MOSI n/a
#include <Spi.h>
// ATmega328P ADC
int analogPin = 0; // analog input channel
// ADC analog input value
word sensorValue = 0; // equivalent to unsigned int
// Byte of data to output to DAC
byte data = 0;
void setup() {
//set pin(s) to input and output
pinMode(analogPin, INPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(analogPin); // comment out this line to test DAC
// sensorValue = 0x0200; // 0x03FF = Vref, 0x0200 = 1/2 Vref
sensorValue = sensorValue << 2 ; // 10 bit ADC to 12bit DAC word
// set SS pin low, beginning 16bit (2 byte) data transfer to DAC
digitalWrite(SS_PIN, LOW);
// send high byte
data = highByte(sensorValue);
data = 0b00001111 & data; // clear 4bit command field (optional)
data = 0b00110000 | data; // 0=DACA, 0=buffered, 1=1x, 1=output enabled
Spi.transfer(data); // alt: shiftOut(MOSI, SCK, MSBFIRST, data);
// send low byte
data = lowByte(sensorValue);
Spi.transfer(data); // alt: shiftOut(MOSI, SCK, MSBFIRST, data);
// set SS pin high, completing 16bit transfer to DAC
digitalWrite(SS_PIN, HIGH);
}
Bipolar Operation
source: Section 6.5 "Bipolar Operation," Microchip Technology Inc. MCP4921/MCP4922,
12-Bit DAC with SPI™ Interface, Microchip Technology document DS21897B
Bipolar operation is achievable using the MCP4921 by using an external operational amplifier
(op amp). This configuration is desirable due to the wide variety and
availability of op amps. This allows a general purpose DAC, with its cost and availability
advantages, to meet almost any desired output voltage range, power and
noise performance. Figure 5 illustrates a simple bipolar voltage source configuration. R1 and
R2 allow the gain to be selected, while R3 and R4 shift the DAC's output to a selected offset.
Note that R4 can be tied to VREF, instead of AVSS, if a higher offset is desired. Note that a
pull-up to VREF could be used, instead of R4, if a higher offset is desired.
An output step magnitude of 1 mV with an output range of ±2.05V is desired for a particular
application.
1. Calculate the range: +2.05V – (-2.05V) = 4.1V.
2. Calculate the resolution needed: 4.1V/1 mV = 4100, Since 212 = 4096, 12-bit resolution is
desired.
3. The amplifier gain (R2/R1), multiplied by VREF, must be equal to the desired minimum output
to achieve bipolar operation. Since any gain can be realized by choosing resistor values
(R1+R2), the VREF source needs to be determined first. If a VREF of 4.1V is used, solve for the
gain by setting the DAC to 0, knowing that the output needs to be -2.05V. The equation can
be simplified to:
4. Next, solve for R3 and R4 by setting the DAC to 4096, knowing that the output needs to be
+2.05V