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.
The VREF is configured to provide the voltage reference for the ADC0 and DAC0, as presented below.
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
.
ADC0.MUXPOS = ADC_MUXPOS_DAC0_gc;
ADC0.COMMAND = ADC_STCONV_bm;
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { ; }
ADC0.INTFLAGS = ADC_RESRDY_bm;
The ADC conversion result is available in the ADC0.RES register.
while (1)
{
adcVal = ADC0_read();
dacVal++;
DAC0_setVal(dacVal);
}
An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here: