3 Generating Constant Analog Signal
The DAC can be used to generate a constant analog signal. The Voltage Reference (VREF) peripheral module is used to provide a voltage reference for the DAC. The DAC output ranges from 0V to .
The voltage reference VREF can be selected from a list of predefined values:
The 4.34V was selected, to have the widest variation range:
VREF_CTRLA |= VREF_DAC0REFSEL_4V34_gc;
VREF_CTRLB |= VREF_DAC0REFEN_bm;
A 25 μs delay is recommended after configuring the VREF and enabling the voltage
reference output. To implement this delay, the VREF_STARTUP_MICROS
macro definition is used.
_delay_us(VREF_STARTUP_MICROS);
The DAC output can be used internally by other peripherals, or it can be linked to an output pin. For the ATtiny817, the DAC output is connected to pin A6 (see Figure 3-3).
The DAC output pin needs to have the digital input buffer and the pull-up resistor disabled to reduce its load.
PORTA.PIN6CTRL &= ~PORT_ISC_gm; PORTA.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; PORTA.PIN6CTRL &= ~PORT_PULLUPEN_bm;
The desired output for the DAC in this particular example is 0.5425V. To achieve it, the following equation is applied:
Writing to the DAC0.DATA register at initialization is optional; however, it may be useful to make the DAC output a specific voltage from the beginning.
DAC0.DATA = 0x20;
Enabling DAC, Output Buffer and Run in Standby sleep mode:
DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm | DAC_RUNSTDBY_bm;
Starting a Conversion
When the DAC is enabled (ENABLE = 1
in DAC.CTRLA), a conversion
starts as soon as the Data (DAC.DATA) register is written.
DAC0.DATA = 0x20;
After conversion, the output keeps its value of until the next conversion, as long as the DAC is running. Any change in VREF selection will immediately change the DAC output (if enabled and running).
An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here: