5 Reading the DAC Internally with the ADC

As seen in Figure 5-1, the ADC input can be connected to the DAC output. Therefore, the DAC conversion result (a digital value) can be transmitted directly to the ADC. Then, the ADC will convert the analog result provided by the DAC again, obtaining a digital result.

Figure 5-1. Analog-to-Digital Converter Block Diagram

The VREF is configured to provide the voltage reference for the ADC0 and DAC0, as presented below.

Figure 5-2. VREF.CTRLA Register

The VREF peripheral module is initialized as illustrated in the code snippet below.

VREF_CTRLA |= VREF_DAC0REFSEL_4V34_gc; 
VREF_CTRLB |= VREF_DAC0REFEN_bm;
VREF_CTRLA |= VREF_ADC0REFSEL_4V34_gc; 
VREF_CTRLB |= VREF_ADC0REFEN_bm;    
_delay_us(VREF_STARTUP_MICROS);

Then, the ADC is initialized as presented below.

ADC0.CTRLC = ADC_PRESC_DIV4_gc    
           | ADC_REFSEL_INTREF_gc  
           | ADC_SAMPCAP_bm;     

ADC0.CTRLA = ADC_ENABLE_bm        
           | ADC_RESSEL_10BIT_gc;

To read the DAC with the ADC, the MUXPOS register of the ADC must be set to 0x1C.

Figure 5-3. MUXPOS DAC Output Selection
ADC0.MUXPOS = ADC_MUXPOS_DAC0_gc;
The ADC conversion is started by writing the corresponding bit to the ADC0.COMMAND.
ADC0.COMMAND = ADC_STCONV_bm;
When the conversion is done, the Result Ready (RESRDY) flag in ADC0.INTFLAGS will be set by the hardware.
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm))
{
    ;
}
The flag must be cleared by software, as presented below.
ADC0.INTFLAGS = ADC_RESRDY_bm;

The ADC conversion result is available in the ADC0.RES register.

The DAC output can be set to different values and read using the ADC inside the infinite loop.
while (1) 
{
    adcVal = ADC0_read();               
    dacVal++;
    DAC0_setVal(dacVal); 
}
Tip: The full code example is also available in the Appendix section.

An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here: