1.11 Getting Started with DAC
1.11.1 Introduction
Author: Srinivasa KR, Microchip Technology Inc. |
The Digital-to-Analog Converter (DAC) converts a digital signal into an analog signal. The DAC module in the 8-bit PIC® microcontrollers offers flexible input source selection, provision of routing output to other peripherals, such as an Analog Comparator (AC) and Analog-to-Digital Converter (ADC), and configurable register sets.
- Signal Generation
- Reference Voltage Generation
- Converting Digital Output of Encoders to Analog Signals, etc.
This technical brief provides insight into the 5-bit DAC peripheral. Along with the working principle of the module, the technical brief describes the configurations of the DAC based on requirements and performance parameters of the module. This document also discusses the possible functional configurations of the DAC module, code examples, and code generation for DAC using the MPLAB® Code Configurator (MCC).
1.11.2 Relevant Devices
The following 8-bit PIC® microcontroller families offer feature-identical 5-bit DAC.
- PIC18-Q10
- PIC18-K83
- PIC18-K42
- PIC18-K40
- PIC16F18346
- PIC16F18446
- PIC16F15386
- PIC16F18877
1.11.3 Overview
The DAC supplies a variable voltage reference that is ratiometric with the input source and has 32 selectable output levels.
- Flexible input source (both positive and negative)
- Selection of the DAC output, as a
reference to the following internal peripherals:
- DACxOUT1 pin
- DACxOUT2 pin
- Comparator positive input
- ADC input channel
The flexible input reference voltage (both positive input source (Vsource+) and negative input source (Vsource-)) aids in configuring a range of reference voltages from negative (-) to positive (+).
- FVR Buffer
- External VREF+ Pin
- VDD supply voltage
The Fixed Voltage Reference (FVR) is an internal module providing stable voltage references; Table 1-30 showcases those levels of voltages, which are limited to less than the VDD voltage. An external VREF+ pin is used when the user wishes to provide the input from an external source.
The VDD supply voltage of the microcontroller can also be used as a voltage resource to the positive input source of the DAC. The negative input source (Vsource-) of the DAC can be connected to the two sources:
- External VREF- Pin
- VSS
The external VREF- pin is a negative reference voltage pin, which can be used when the user wishes to provide the input from an external voltage source. (Refer to Electric Characteristics in the device-specific data sheet).
The VSS supply voltage pin is an operating supply ground (GND) pin, which can be used as a negative input source to the DAC. The output of DAC can be routed to other peripherals as a reference voltage. This includes:
- Noninverting input of comparator
- ADC input channel
- Microcontroller pins: DACxOUT1 and DACxOUT2
This feature makes DAC a flexible module suitable for a wide range of applications.
1.11.4 DAC Operation
The DAC has 32 voltage levels which are set with the DAC1R [4:0] bits of the DAC1CON1 register. The DAC output voltage is derived from the parameters VREF+, VREF-, 5-bit DAC input, and 32 voltage levels.
The internal structure of the DAC module is comprised of a 32-to-1
multiplexer and a 32-steps resistor ladder. The resistor ladder is a string of identical
resistors connected to the multiplexer, as shown above. One end of the resistor ladder
is connected to the positive input source (Vsource+) and the other, to negative input
source (Vsource-). With this architecture, Vsource+ to Vsource– is divided into 32 equal
steps. The DAC output value is derived from the resistor ladder and if the voltage of
either input source fluctuates, this would result in fluctuations in the DAC output
value. The DAC output voltage can be routed to the DACxOUTn pins and to other
peripherals by setting the respective control register bits. Reading the DACxOUTn pin
when it is configured for the DAC reference voltage output will always return a
‘0
’.
The DAC output can be determined by using the following equation:
- VREF+ = 5V
- VREF- = 0V
- DACR [4:0] = 31 (Maximum)
- 25 = 32
- DACx_output = [(5 - 0) x 31 / 32] + 0 = 4.843V
Thus, the 5-Bit DAC can provide a maximum output voltage of 4.843V
1.11.5 Possible Configurations
1.11.5.1 DAC Output as Reference Voltage
The following configuration can be used in an application where it is required to access the DAC output from an external pin.
Here, the DAC positive and negative reference voltage is configured as microcontroller AVDD and AVSS, respectively. This configuration is required for the functionalities, where variable analog voltage is required as output from the DAC.
Value | Description |
---|---|
11 | Reserved |
10 | FVR buffer |
01 | FVR+ (External Positive Voltage Reference) |
00 | AVDD (Microcontroller VCC) |
Value | Description |
---|---|
1 | VREF- (External Negative Voltage Reference) |
0 | AVSS (Microcontroller GND) |
Value | Description |
---|---|
1 | DAC voltage level is output on the DAC1OUT1 pin |
0 | DAC voltage level is disconnected from the DAC1OUT1 pin |
The DAC positive reference input source is configured as the microcontroller AVDD, the negative input source is configured as AVSS, and the output of the DAC is connected to the microcontroller Pin (GPIO Pin). The DAC1OUT1 pin must be configured as output and analog pin.
28-Pin SPDIP, SOIC, SSOP | 28-Pin (V)QFN | A/D | Reference | Comparator | |
---|---|---|---|---|---|
RA0 | 2 | 27 | ANA0 | C1IN0- C2IN0- | |
RA1 | 3 | 28 | ANA1 | C1IN1- C2IN1- | |
RA2 | 4 | 1 | ANA2 | DAC1OUT1 VREF-(DAC) VREF-(ADC) | C1IN0+ C2IN0+ |
RA3 | 5 | 2 | ANA3 | VREF+(DAC) VREF+(ADC) | C1IN1+ |
RA4 | 6 | 3 | ANA4 |
The MCC is a graphical programing environment that generates code seamlessly. Easy-to-use GUI helps to generate the desired code, which can be easily inserted into the project. Figure 4-2 shows how to use the MCC for the DAC configuration.
The following code is generated by the MCC for DAC initialization:
void DAC1_Initialize(void)
{
// DAC1EN enabled; NSS VSS; PSS VDD; OE1 enabled; OE2 disabled;
DAC1CON0 = 0xA0;
// DAC1R 31;
DAC1CON1 = 0x1F; //Default input value
}
The DAC output value is set by using the DACxCON1 register. The following MCC-generated API/function can be used for the same purpose.
void DAC1_SetOutput (uint8_t inputData)
{
DAC1CON1 = inputData;
}
After initializing this configuration, the DAC output can be connected to the DAC1CON1 (i.e., the RA2 pin). This DAC configuration can be used as a reference voltage generator and waveform/signal generator. Some of the examples are listed in the MPLAB Xpress code Examples.
1.11.5.2 Reading DAC Output Using ADC
- DAC for providing analog voltage to ADC channel
- ADC for application functionalities
The user can configure the DAC output as input to ADC, the DAC positive reference input as FVR buffer, the negative reference input source as microcontroller GND (i.e., AVSS), and the DAC output routed to ADC as an input channel. The DAC output can be configured as an analog input channel to ADC by configuring the ADC input selection register.
The following code is generated by the MCC for the DAC configuration:
//DAC0EN enabled; NSS VSS; PSS FVR_buf2; OE1 disabled; OE2 disabled;
void DAC1_Initialize(void)
{
// DAC1EN enabled; NSS VSS; PSS FVR_buf2; OE1 disabled; OE2 disabled;
DAC1CON0 = 0x88;
// DAC1R 0;
DAC1CON1 = 0x00;
}
Figure 4-6 showcases the block diagram of FVR, which can be used as a DAC positive reference input voltage. The FVRCON register is used to set the fixed reference voltage for DAC.
// FVRCON enabled; ADFVR [1:0] 2.048V;
void FVR_Initialize(void)
{
// CDAFVR off; FVREN enabled; TSRNG Lo_range; ADFVR 2x; TSEN disabled;
FVRCON = 0x82;
}
FVRCON.CDAFVR Value | Buffer Voltage |
---|---|
ob11 | 4.096V |
ob10 | 2.048V |
ob01 | 1.024V |
ob00 | Buffer is Off |
The following code is generated:
//code
ADPCH = 0x3E; // ADPCH DAC1;
1.11.5.3 DAC Output as an Analog Comparator Input
This configuration involves the AC and DAC peripherals, where the PIC 8-bit microcontroller has the flexibility to route the DAC output to the noninverting input of AC, internally.
The comparator register CMxPCH must be used for selecting the DAC output as a noninverting channel. The polarity of the comparator output can be inverted by setting the CxPOL bit. Clearing the CxPOL bit results in a noninverted output. The CxPOL bit belongs to register CMxCON0 and the output of the comparator is listed in following table:
Input Condition | CxPOL | CxOUT |
---|---|---|
VIN+ > VIN- | 0 | 0 |
VIN+ < VIN- | 0 | 1 |
VIN+ > VIN- | 1 | 1 |
VIN+ < VIN- | 1 | 0 |
Figure 4-10 and Figure 4-11 show how the MCC can be used to generate code for this particular configuration.
void DAC1_Initialize(void)
{
// DAC1EN enabled; NSS VSS; PSS VREF+; OE1 disabled; OE2 disabled;
DAC1CON0 = 0x84;
// DAC1R 8;
DAC1CON1 = 0x08;
}
void CMP1_Initialize(void)
{
PIE2bits.C1IE = 0; // C1HYS disabled; C1EN enabled; C1POL not inverted; C1SYNC asynchronous;
CM1CON0 = 0x80;
CM1CON1 = 0x02; // C1INTN no_intFlag; C1INTP intFlag_pos
CM1NCH = 0x07; // NCH Vss;
CM1PCH = 0x05; // PCH DACOUT;
PIR2bits.C1IF = 0; // Clearing IF flag before enabling the interrupt.
PIE2bits.C1IE = 1; // Enabling CMP1 interrupt.
}
1.11.6 DAC in Power-Down Modes
In the Power-Saving Operation modes (Sleep, Idle, and Doze) of the PIC microcontroller, we may expect changes in current consumption when using the DAC.
When the device wakes up from Sleep mode by an interrupt or by a Windowed Watchdog Timer (WWDT) time-out, the contents of the DACxCON0 register are not affected.
- To minimize the current consumption in Sleep mode, the voltage reference must be disabled from the FVR before the controller enters Power-Down mode.
- Refer to the device-specific data sheet for more details about Power-Saving Operation modes, DAC, FVR, and corresponding registers.