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 255 × V R E F 256 .

The voltage reference VREF can be selected from a list of predefined values:

Figure 3-1. CTRLA.DAC0REFSEL Values and Associated VREF Voltages

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.

Figure 3-2. Internal Voltage Reference Characteristics
_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).

Figure 3-3. PORT Function Multiplexing

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:

D A T A = V O U T × 256 / V R E F = 0.5425 V × 256 / 4.34 V = 32 = 0 x 20

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;
Important: If Run in Standby sleep mode is enabled, the DAC will continue to run when the MCU is in Standby sleep mode.

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 D A T A × V R E F 256 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).

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: