0% found this document useful (0 votes)
29 views23 pages

Using 10bit DAC For Generating Analog Signals DS90003235C

10 bit DAC

Uploaded by

george.eth09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views23 pages

Using 10bit DAC For Generating Analog Signals DS90003235C

10 bit DAC

Uploaded by

george.eth09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

TB3235

Using 10-Bit DAC for Generating Analog Signals

Introduction
Authors: Cristian Sabiuta, Marius Nicolae, Microchip Technology Inc.

The AVR® DA MCU family of microcontrollers is based on the AVR architecture and brings a DAC peripheral
equipped with 10-bit resolution and high-drive capabilities, helping the user to generate precise analog voltages and
use them internally or externally on a physical pin.
This technical brief describes how the 10-bit DAC works on the AVR DA microcontroller family, covering the following
use cases:
• Generating Constant Analog Signal Using 10-Bit DAC:
Initialize the DAC, set the voltage reference, set the DAC to output a specific constant voltage.
• Generating Sine Wave Signal Using 10-Bit DAC:
Initialize the DAC, set the voltage reference, output in a loop the samples of a sine wave.
• Reading the DAC Internally with the ADC:
Initialize the DAC and ADC, set the voltage reference, set the ADC to read the DAC, increment the DAC output
and read it with the ADC for each step.
• Generating Amplitude Modulated Signal Using 10-Bit DAC:
Initialize the DAC with external reference and link the signal that must be modulated to the external reference
pin. The AVR core will continuously change the Data (DACn.DATA) register to create a modulated signal.
Note: The code examples were developed on AVR128DA48 Curiosity Nano.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 1


TB3235

Table of Contents
Introduction.....................................................................................................................................................1

1. Relevant Devices.................................................................................................................................... 3
1.1. AVR® DA Family Overview.......................................................................................................... 3

2. Overview................................................................................................................................................. 4

3. Generating Constant Analog Signal Using 10-Bit DAC...........................................................................5

4. Generating Sine Wave Signal Using 10-Bit DAC.................................................................................... 8

5. Reading the DAC Internally with the ADC...............................................................................................9

6. Generating Amplitude Modulated Signal Using 10-bit DAC..................................................................13

7. References............................................................................................................................................14

8. Appendix............................................................................................................................................... 15

9. Revision History.................................................................................................................................... 20

The Microchip Website.................................................................................................................................21

Product Change Notification Service............................................................................................................21

Customer Support........................................................................................................................................ 21

Microchip Devices Code Protection Feature................................................................................................ 21

Legal Notice................................................................................................................................................. 21

Trademarks.................................................................................................................................................. 22

Quality Management System....................................................................................................................... 22

Worldwide Sales and Service.......................................................................................................................23

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 2


TB3235
Relevant Devices

1. Relevant Devices
This chapter lists the relevant devices for this document.

1.1 AVR® DA Family Overview


The figure below shows the AVR® DA devices, laying out pin count variants and memory sizes:
• Vertical migration is possible without code modification, as these devices are fully pin and feature compatible
• Horizontal migration to the left reduces the pin count, and therefore, the available features
Figure 1-1. AVR® DA Family Overview

Flash

128 KB AVR128DA28 AVR128DA32 AVR128DA48 AVR128DA64

64 KB AVR64DA28 AVR64DA32 AVR64DA48 AVR64DA64

32 KB AVR32DA28 AVR32DA32 AVR32DA48

Pins
28 32 48 64

Devices with different Flash memory size typically also have different SRAM.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 3


TB3235
Overview

2. Overview
The DAC features a 10-bit resolution and has one continuous time output with high-drive capabilities. The DAC
conversion can be started from the application by writing to the Data (DACn.DATA) register pair.
Figure 2-1. DAC Block Diagram
Other
Peripherals

10
DATA DAC OUT
Output
Driver

VREF
ENABLE

CTRLA
OUTEN

Figure 2-2. Signal Description

Signal Description Type


OUT DAC output Analog

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 4


TB3235
Generating Constant Analog Signal Using 10-Bit ...

3. Generating Constant Analog Signal Using 10-Bit DAC


The DAC can be used to generate a constant analog signal. It uses the output of the Voltage Reference (VREF)
peripheral as positive reference.
1023 × ����
The DAC output ranges from 0V to
1024
VREF can be selected from a list of predefined values:
• Internal 1.024V reference
• Internal 2.048V reference
• Internal 4.096V reference
• Internal 2.500V reference
• VDD reference
• External reference from the VREFA pin (PD7)
Figure 3-1. VREF.DAC0REF Register
Bit 7 6 5 4 3 2 1 0
ALWAYSON REFSEL[2:0]
Access R/W R/W R/W R/W
Reset 0 0 0 0

Bit 7 – ALWAYSON Reference Always On


This bit controls whether the DAC0 reference is always on or not.
Value Description
0 The reference is automatically enabled when needed
1 The reference is always on

Bits 2:0 – REFSEL[2:0] Reference Select


This bit field controls the reference voltage level for DAC0.
Note:
1. The values given for internal references are only typical. Refer to the Electrical Characteristics section for
further details.
Value Name Description
0x0 1V024 Internal 1.024V reference(1)
0x1 2V048 Internal 2.048V reference(1)
0x2 4V096 Internal 4.096V reference(1)
0x3 2V500 Internal 2.500V reference(1)
0x4 - Reserved
0x5 VDD VDD as reference
0x6 VREFA External reference from the VREFA pin
0x7 - Reserved

For the purpose of this example, the 2.048V reference voltage was selected:
VREF.DAC0REF = VREF_REFSEL_2V048_gc;

A 50 µs delay is recommended after enabling the VREF peripheral.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 5


TB3235
Generating Constant Analog Signal Using 10-Bit ...

Figure 3-2. Internal Voltage Reference (VREF) Characteristics

Symbol Description Min. Typ. Max. Units Conditions

VVREF_1V024 Internal Voltage -4 — +4 % VDD≥2.5V, -40°C to


Reference 1.024V 85°C

VVREF_2V048 Internal Voltage -4 — +4 % VDD≥2.5V, -40°C to


Reference 2.048V 85°C

VVREF_4V096 Internal Voltage -4 — +4 % VDD≥4.55V, -40°C to


Reference 4.096V 85°C

VVREF_2V500 Internal Voltage -4 — +4 % VDD≥4.55V, -40°C to


Reference 2.5V 85°C

TVREF_ST VREF Start-up Time — 50 — μs

_delay_us(50);

The DAC output can be used internally by other peripherals, or it can be linked to an output pin. For the
AVR128DA48, the DAC output is connected to pin PD6 (see the figure below).
Figure 3-3. PORT Function Multiplexing
Pin name (1,2)

CCL-LUTn
VQFN64/

VQFN48/

VQFN32/

USARTn
SSOP28
SOIC28/
TQFP64

TQFP48

TQFP32

TWIn(4)
Special

EVSYS
ADC0

DAC0

ZCDn

TCBn

TCDn
TCA0

TCA1
SPIn
ACn
PTC

1,AINP2
30 24 14 10 PD4 AIN4 X20/Y20 WO4(3)
2,AINP1

31 25 15 11 PD5 AIN5 X21/Y21 1,AINN0 WO5(3)

0,AINP3

32 26 16 12 PD6 AIN6 X22/Y22 1,AINP3 VOUT 2,OUT(3)


2,AINP3

0,AINN2

33 27 17 13 PD7 VREFA AIN7 X23/Y23 1,AINN2 EVOUTD (3)

2,AINN0/AINN2

The DAC output pin needs to have the digital input buffer and the pull-up resistor disabled in order to reduce its load.

PORTD.PIN6CTRL &= ~PORT_ISC_gm;

PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;

PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;

The DACn.DATA register is used to generate a specific analog output voltage. The value of this output voltage can be
determined using the following equation:
����.���� × ����
���� =
1024
Writing to the DACn.DATA register at initialization is optional; however, it is useful to make the DAC output a specific
voltage from the beginning. The DAC features a 10-bit resolution, therefore, the DACn.DATAL and DACn.DATAH
register pair represents the 10-bit value DACn.DATA (see Figure 3-4). The two LSbs [1:0] are accessible at the
original offset and the eight MSbs [9:2] can be accessed at offset +1.
The output will be updated after DACn.DATAH is written.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 6


TB3235
Generating Constant Analog Signal Using 10-Bit ...

Figure 3-4. DACn.DATA Register


Bit 15 14 13 12 11 10 9 8
DATA[9:2]
Access R/W R/W R/W R/W R/W R/W R/W R/W
Reset 0 0 0 0 0 0 0 0

Bit 7 6 5 4 3 2 1 0
DATA[1:0]
Access R/W R/W
Reset 0 0

Bits 15:6 – DATA[9:0]


These bits contain the digital data, which will be converted to an analog voltage.

The desired output for the DAC in this example is 1.2V. To achieve this, the following equation is applied:
���� × 1024 1.2� × 1024
����.���� = = = 600 = 0x258
���� 2.048�

In order to enable the DAC, Output Buffer, and Run in Standby mode, use the following code:

DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm | DAC_RUNSTDBY_bm;

Important: If Run in Standby mode is enabled, the DAC will continue to run when the microcontroller is in
Standby Sleep mode.

Starting a Conversion
When the DAC is enabled (ENABLE = 1 in DACn.CTRLA), a conversion starts as soon as the Data (DACn.DATA)
register is written.
When the DAC is disabled (ENABLE = 0 in DACn.CTRLA), writing to the Data registers does not trigger a
conversion. Instead, the conversion starts on writing a ‘1’ to the ENABLE bit in the DACn.CTRLA register.

DAC0.DATAL = (value & (0x03)) << 6;

DAC0.DATAH = value >> 2;

����.���� × ����
After a conversion, the output keeps its value of until the next conversion, as long as the DAC is
1024
running. Any change in the VREF selection will immediately change the DAC output (if enabled and running).

Tip: The full code example is also available in 8. Appendix.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 7


TB3235
Generating Sine Wave Signal Using 10-Bit D...

4. Generating Sine Wave Signal Using 10-Bit DAC


The DAC can be used to generate a sine wave signal.
To generate this signal, the VREF and the DAC are initialized first, then the output value can be changed by writing a
new value to the DACn.DATA register.
Before the sine wave is generated, the samples corresponding to a period are calculated and stored in a buffer.

for(i = 0; i < SINE_PERIOD_STEPS; i++)


{
sineWave[i] = SINE_DC_OFFSET + SINE_AMPLITUDE * sin(2 * M_PI * i / SINE_PERIOD_STEPS);
}

The sinusoidal waveform is created using a fixed number of steps (N_SAMPLES). To create a sine wave signal with
a specific frequency (SIGNAL_FREQ), all steps are executed in one period resulting in the following sample rate:
1
������_���� = = ������_���� × �_�������
����_�����_����

while (1)
{
DAC0_setVal(sineWave[sineIndex++]);
if(sineIndex == SINE_PERIOD_STEPS)
sineIndex = 0;
_delay_us(STEP_DELAY_TIME);
}

Tip: The full code example is also available in 8. Appendix.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 8


TB3235
Reading the DAC Internally with the ADC

5. Reading the DAC Internally with the ADC


The analog output of the DAC can be internally connected to other peripherals when the DAC is enabled (ENABLE =
1 in DACn.CTRLA). When the DAC analog output is only being used internally, it is not necessary to enable the pin
output driver (OUTEN = 0 in DACn.CTRLA is acceptable).
Referring to the ADC block diagram below, the output of the 10-bit DAC can be used as the internal input to the ADC.
Figure 5-1. Analog-to-Digital Converter Block Diagram
MUXPOS VREF
AVDD

VREFA
AIN0 VREF
Internal Reference
AIN1
.
.
.
AINn
VAINP
Internal
Inputs
ADC ACC
AIN0 VAINN
AIN1
.
. Result Result ready
.
AINn formatting RES (IRQ)

Internal
> Window compare
Inputs Control Logic < (IRQ)

MUXNEG CTRLA WINLT


EVCTRL WINHT
COMMAND
The DAC voltage reference is initialized as mentioned in 3. Generating Constant Analog Signal Using 10-Bit DAC
and the ADC voltage reference is initialized in the same way from the VREF peripheral.
Figure 5-2. VREF.DAC0REF Register
Bit 7 6 5 4 3 2 1 0
ALWAYSON REFSEL[2:0]
Access R/W R/W R/W R/W
Reset 0 0 0 0

Bit 7 – ALWAYSON Reference Always On


This bit controls whether the DAC0 reference is always on or not.
Value Description
0 The reference is automatically enabled when needed
1 The reference is always on

Bits 2:0 – REFSEL[2:0] Reference Select


This bit field controls the reference voltage level for DAC0.
Note:
1. The values given for internal references are only typical. Refer to the Electrical Characteristics section for
further details.
Value Name Description
0x0 1V024 Internal 1.024V reference(1)
0x1 2V048 Internal 2.048V reference(1)
0x2 4V096 Internal 4.096V reference(1)
0x3 2V500 Internal 2.500V reference(1)
0x4 - Reserved
0x5 VDD VDD as reference
0x6 VREFA External reference from the VREFA pin
0x7 - Reserved

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 9


TB3235
Reading the DAC Internally with the ADC

Figure 5-3. VREF.ADC0REF Register


Bit 7 6 5 4 3 2 1 0
ALWAYSON REFSEL[2:0]
Access R/W R/W R/W R/W
Reset 0 0 0 0

Bit 7 – ALWAYSON Reference Always On


This bit controls whether the ADC0 reference is always on or not.
Value Description
0 The reference is automatically enabled when needed
1 The reference is always on

Bits 2:0 – REFSEL[2:0] Reference Select


This bit field controls the reference voltage level for ADC0.
Note:
The values given for internal references are only typical. Refer to the Electrical Characteristics section for
further details.
Value Name Description
0x0 1V024 Internal 1.024V reference(2)
0x1 2V048 Internal 2.048V reference (2)
0x2 4V096 Internal 4.096V reference (2)
0x3 2V500 Internal 2.500V reference(2)
0x4 - Reserved
0x5 VDD VDD as reference
0x6 VREFA External reference from the VREFA pin
0x7 - Reserved
The complete VREF initialization is shown below:

VREF.DAC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for DAC */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
VREF.ADC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for ADC */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
/* Wait VREF start-up time */
_delay_us(VREF_STARTUP_TIME);

Then, the ADC must be initialized:

ADC0.CTRLC = ADC_PRESC_DIV2_gc;
ADC0.CTRLA = ADC_ENABLE_bm | ADC_RESSEL_12BIT_gc;

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

ADC0.MUXPOS = ADC_MUXPOS_DAC0_gc;

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 10


TB3235
Reading the DAC Internally with the ADC

Figure 5-4. MUXPOS DAC Output Selection


Bit 7 6 5 4 3 2 1 0
MUXPOS[6:0]
Access R/W R/W R/W R/W R/W R/W R/W
Reset 0 0 0 0 0 0 0

Bits 6:0 – MUXPOS[6:0] MUX Selection for Positive ADC Input


This bit field selects which analog input is connected to the positive input of the ADC. If these bits are changed during
a conversion, the change will not take effect until the conversion is complete.

Value Name Description


0x00-0x0F AIN0-AIN15 ADC input pin 0-15
0x10-0x15 AIN16-AIN21 ADC input pin 16-21
0x16-0x1F - Reserved
0x20-0x3F - Reserved
0x40 GND Ground
0x41 - Reserved
0x42 TEMPSENSE Temperature sensor
0x48 DAC0 DAC0
Other - Reserved

The ADC conversion is started by writing the corresponding bit to the ADCn.COMMAND register:

ADC0.COMMAND = ADC_STCONV_bm;

When the conversion is done, the RESRDY bit in the ADCn.INTFLAGS register will be set by hardware.

while(!(ADC0.INTFLAGS & ADC_RESRDY_bm))


{
;
}

The flag is cleared by either writing a ‘1’ to the RESRDY bit location or by reading the Result (ADCn.RES) register.
Writing a ‘0’ to this bit has no effect.

ADC0.INTFLAGS = ADC_RESRDY_bm;

The ADC data can be read from the Result (ADCn.RES) register.
The DAC output can be set to different values, and read with the ADC in a loop:

while (1)
{
adcVal = ADC0_read();
dacVal++;
DAC0_setVal(dacVal);
}

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 11


TB3235
Reading the DAC Internally with the ADC

Tip: The full code example is also available in 8. Appendix.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 12


TB3235
Generating Amplitude Modulated Signal Using ...

6. Generating Amplitude Modulated Signal Using 10-bit DAC


The DAC can be used to generate amplitude modulated signal. In this case, the DAC uses the voltage reference from
the VREFA pin as modulation waveform (information signal), while it is configured to generate sine wave for the
carrier signal.

VREF.DAC0REF = VREF_REFSEL_VREFA_gc;

�������� × �����
���� =
1024
Therefore, the amplitude of the carrier signal will vary according to the signal that needs to be modulated.

Important: The frequency of the carrier signal must be greater than that of the information signal in order
for the resulting signal to be relevant.

Based on the carrier wave frequency (SIGNAL_FREQ), the core will use a fixed number of samples (N_SAMPLES)
to build the signal as described in “Generating Sine Wave Signal Using 10-Bit DAC”. The resulting sample rate will
be:
������_���� = �_������� * ������_����
The resulting modulated signal will be available on the DAC output external pin (PD6).
In this example, a 50 Hz sine wave is used as the carrier signal and the modulated signal is a 2 Hz sine wave from a
signal generator.
Figure 6-1. Resulting Signal Visualized on Oscilloscope

Tip: The full code example is also available in 8. Appendix.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 13


TB3235
References

7. References
1. AVR128DA28/32/48/64 Preliminary Data Sheet.
2. AVR128DA48 Curiosity Nano User's Guide.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 14


TB3235
Appendix

8. Appendix
Example 8-1. Generating Constant Analog Signal Code

/* 4 MHz (needed for delay function) */


#define F_CPU (4000000UL)

#include <avr/io.h>
#include <util/delay.h>

/* DAC Value */
#define DAC_EXAMPLE_VALUE (0x258)
/* VREF start-up time */
#define VREF_STARTUP_TIME (50)
/* Mask needed to get the 2 LSb for DAC Data Register */
#define LSB_MASK (0x03)

static void VREF_init(void);


static void DAC0_init(void);
static void DAC0_setVal(uint16_t value);

static void VREF_init(void)


{
VREF.DAC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for DAC */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
/* Wait VREF start-up time */
_delay_us(VREF_STARTUP_TIME);
}

static void DAC0_init(void)


{
/* Disable digital input buffer */
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable pull-up resistor */
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm /* Enable DAC */
| DAC_OUTEN_bm /* Enable output buffer */
| DAC_RUNSTDBY_bm; /* Enable Run in Standby mode */
}

static void DAC0_setVal(uint16_t value)


{
/* Store the two LSbs in DAC0.DATAL */
DAC0.DATAL = (value & LSB_MASK) << 6;
/* Store the eight MSbs in DAC0.DATAH */
DAC0.DATAH = value >> 2;
}

int main(void)
{
VREF_init();
DAC0_init();

DAC0_setVal(DAC_EXAMPLE_VALUE);

while (1)
{
;
}
}

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 15


TB3235
Appendix

Example 8-2. Generating Sine Wave Signal Code

/* 4 MHz (needed for delay function) */


#define F_CPU (4000000UL)

#include <avr/io.h>
#include <util/delay.h>
#include <math.h>

/* VREF start-up time */


#define VREF_STARTUP_TIME (50)
/* Mask needed to get the 2 LSb for DAC Data Register */
#define LSB_MASK (0x03)
/* Number of samples for a sine wave period */
#define SINE_PERIOD_STEPS (100)
/* Sine wave amplitude */
#define SINE_AMPLITUDE (511)
/* Sine wave DC offset */
#define SINE_DC_OFFSET (512)
/* Frequency of the sine wave */
#define SINE_FREQ (100)
/* Step delay for the loop */
#define STEP_DELAY_TIME ((1000000 / SINE_FREQ) / SINE_PERIOD_STEPS)

static void sineWaveInit(void);


static void VREF_init(void);
static void DAC0_init(void);
static void DAC0_setVal(uint16_t value);

/* Buffer to store the sine wave samples */


uint16_t sineWave[SINE_PERIOD_STEPS];

static void sineWaveInit(void)


{
uint8_t i;
for(i = 0; i < SINE_PERIOD_STEPS; i++)
{
sineWave[i] = SINE_DC_OFFSET + SINE_AMPLITUDE * sin(2 * M_PI * i / SINE_PERIOD_STEPS);
}
}

static void VREF_init(void)


{
VREF.DAC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for DAC */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
/* Wait VREF start-up time */
_delay_us(VREF_STARTUP_TIME);
}

static void DAC0_init(void)


{
/* Disable digital input buffer */
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable pull-up resistor */
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm /* Enable DAC */
| DAC_OUTEN_bm /* Enable output buffer */
| DAC_RUNSTDBY_bm; /* Enable Run in Standby mode */
}

static void DAC0_setVal(uint16_t value)


{
/* Store the two LSbs in DAC0.DATAL */
DAC0.DATAL = (value & LSB_MASK) << 6;
/* Store the eight MSbs in DAC0.DATAH */
DAC0.DATAH = value >> 2;
}

int main(void)
{
uint8_t sineIndex = 0;

VREF_init();
DAC0_init();

sineWaveInit();

while (1)
{
DAC0_setVal(sineWave[sineIndex++]);
if(sineIndex == SINE_PERIOD_STEPS)
sineIndex = 0;
_delay_us(STEP_DELAY_TIME);
}
}

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 16


TB3235
Appendix

Example 8-3. Reading the DAC Internally with the ADC Code

/* 4 MHz (needed for delay function) */


#define F_CPU (4000000UL)

#include <avr/io.h>
#include <util/delay.h>

/* VREF start-up time */


#define VREF_STARTUP_TIME (50)
/* Mask needed to get the 2 LSb for DAC Data Register */
#define LSB_MASK (0x03)

static void VREF_init(void);


static void DAC0_init(void);
static void DAC0_setVal(uint16_t val);
static void ADC0_init(void);
static uint16_t ADC0_read(void);

static void VREF_init(void)


{
VREF.DAC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for DAC */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
VREF.ADC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for ADC */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
/* Wait VREF start-up time */
_delay_us(VREF_STARTUP_TIME);
}

static void DAC0_init(void)


{
/* Enable DAC */
DAC0.CTRLA = DAC_ENABLE_bm;
}

static void DAC0_setVal(uint16_t value)


{
/* Store the two LSbs in DAC0.DATAL */
DAC0.DATAL = (value & LSB_MASK) << 6;
/* Store the eight MSbs in DAC0.DATAH */
DAC0.DATAH = value >> 2;
}

static void ADC0_init(void)


{
/* CLK_PER divided by 2 */
ADC0.CTRLC = ADC_PRESC_DIV2_gc;
ADC0.CTRLA = ADC_ENABLE_bm /* Enable ADC */
| ADC_RESSEL_12BIT_gc; /* Use 12-bit resolution */
/* Select ADC channel */
ADC0.MUXPOS = ADC_MUXPOS_DAC0_gc
}

static uint16_t ADC0_read(void)


{
/* Start conversion */
ADC0.COMMAND = ADC_STCONV_bm;
/* Wait until ADC conversion is done */
while(!(ADC0.INTFLAGS & ADC_RESRDY_bm))
{
;
}
/* The interrupt flag is cleared when the conversion result is accessed */
return ADC0.RES;
}

int main(void)
{
uint16_t dacVal = 0;
volatile uint16_t adcVal = 0;

VREF_init();
DAC0_init();
ADC0_init();

while (1)
{
adcVal = ADC0_read();

/* do something with adcVal */

dacVal++;
DAC0_setVal(dacVal);
}
}

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 17


TB3235
Appendix

Example 8-4. Generating Amplitude Modulation Signal Code

/* 4 MHz (needed for delay function) */


#define F_CPU (4000000UL)

#include <avr/io.h>
#include <util/delay.h>
#include <math.h>

/* VREF start-up time */


#define VREF_STARTUP_TIME (50)
/* Mask needed to get the 2 LSb for DAC Data Register */
#define LSB_MASK (0x03)
/* Number of samples for a sine wave period */
#define SINE_PERIOD_STEPS (100)
/* Sine wave amplitude */
#define SINE_AMPLITUDE (511)
/* Sine wave DC offset */
#define SINE_DC_OFFSET (512)
/* Frequency of the sine wave */
#define SINE_FREQ (50)
/* Step delay for the loop */
#define STEP_DELAY_TIME ((1000000 / SINE_FREQ) / SINE_PERIOD_STEPS)

static void PORT_init (void);


static void sineWaveInit(void);
static void VREF_init(void);
static void DAC0_init(void);
static void DAC0_setVal(uint16_t val);

/* Buffer to store the sine wave samples */


uint16_t sineWave[SINE_PERIOD_STEPS];

static void PORT_init (void)


{
/* Set the VREFA pin (PD7) as input */
PORTD.DIRCLR |= PIN7_bm;
}

static void sineWaveInit(void)


{
uint8_t i;
for(i = 0; i < SINE_PERIOD_STEPS; i++)
{
sineWave[i] = SINE_DC_OFFSET + SINE_AMPLITUDE * sin(2 * M_PI * i / SINE_PERIOD_STEPS);
}
}

static void VREF_init(void)


{
VREF.DAC0REF = VREF_REFSEL_VREFA_gc /* Select the External Reference from VREFA pin */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
/* Wait VREF start-up time */
_delay_us(VREF_STARTUP_TIME);
}

static void DAC0_init(void)


{
/* Disable digital input buffer */
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable pull-up resistor */
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm /* Enable DAC */
| DAC_OUTEN_bm /* Enable output buffer */
| DAC_RUNSTDBY_bm; /* Enable Run in Standby mode */
}

static void DAC0_setVal(uint16_t value)


{
/* Store the two LSbs in DAC0.DATAL */
DAC0.DATAL = (value & LSB_MASK) << 6;
/* Store the eight MSbs in DAC0.DATAH */
DAC0.DATAH = value >> 2;
}

int main(void)
{
uint8_t sineIndex = 0;

PORT_init();
VREF_init();
DAC0_init();

sineWaveInit();

while (1)
{
DAC0_setVal(sineWave[sineIndex++]);
if(sineIndex == SINE_PERIOD_STEPS)
sineIndex = 0;
_delay_us(STEP_DELAY_TIME);

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 18


TB3235
Appendix

}
}

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 19


TB3235
Revision History

9. Revision History
Doc. Rev. Date Comments
C 05/2020 Updated AVR® MCU DA (AVR-DA) to AVR® DA MCU, and AVR-DA to AVR DA, per latest
trademarking.

B 03/2020 Updated repository links.


Updated AVR-DA to AVR® MCU DA (AVR-DA), per latest trademarking.

A 02/2020 Initial document release

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 20


TB3235

The Microchip Website


Microchip provides online support via our website at http://www.microchip.com/. This website is used to make files
and information easily available to customers. Some of the content available includes:
• Product Support – Data sheets and errata, application notes and sample programs, design resources, user’s
guides and hardware support documents, latest software releases and archived software
• General Technical Support – Frequently Asked Questions (FAQs), technical support requests, online
discussion groups, Microchip design partner program member listing
• Business of Microchip – Product selector and ordering guides, latest Microchip press releases, listing of
seminars and events, listings of Microchip sales offices, distributors and factory representatives

Product Change Notification Service


Microchip’s product change notification service helps keep customers current on Microchip products. Subscribers will
receive email notification whenever there are changes, updates, revisions or errata related to a specified product
family or development tool of interest.
To register, go to http://www.microchip.com/pcn and follow the registration instructions.

Customer Support
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Embedded Solutions Engineer (ESE)
• Technical Support
Customers should contact their distributor, representative or ESE for support. Local sales offices are also available to
help customers. A listing of sales offices and locations is included in this document.
Technical support is available through the website at: http://www.microchip.com/support

Microchip Devices Code Protection Feature


Note the following details of the code protection feature on Microchip devices:
• Microchip products meet the specification contained in their particular Microchip Data Sheet.
• Microchip believes that its family of products is one of the most secure families of its kind on the market today,
when used in the intended manner and under normal conditions.
• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these
methods, to our knowledge, require using the Microchip products in a manner outside the operating
specifications contained in Microchip’s Data Sheets. Most likely, the person doing so is engaged in theft of
intellectual property.
• Microchip is willing to work with the customer who is concerned about the integrity of their code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code
protection does not mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection
features of our products. Attempts to break Microchip’s code protection feature may be a violation of the Digital
Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you
may have a right to sue for relief under that Act.

Legal Notice
Information contained in this publication regarding device applications and the like is provided only for your
convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 21


TB3235

your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER


EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION,
INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR
FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip
devices in life support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend,
indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such
use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights unless
otherwise stated.

Trademarks
The Microchip name and logo, the Microchip logo, Adaptec, AnyRate, AVR, AVR logo, AVR Freaks, BesTime,
BitCloud, chipKIT, chipKIT logo, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, HELDO, IGLOO, JukeBlox,
KeeLoq, Kleer, LANCheck, LinkMD, maXStylus, maXTouch, MediaLB, megaAVR, Microsemi, Microsemi logo, MOST,
MOST logo, MPLAB, OptoLyzer, PackeTime, PIC, picoPower, PICSTART, PIC32 logo, PolarFire, Prochip Designer,
QTouch, SAM-BA, SenGenuity, SpyNIC, SST, SST Logo, SuperFlash, Symmetricom, SyncServer, Tachyon,
TempTrackr, TimeSource, tinyAVR, UNI/O, Vectron, and XMEGA are registered trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
APT, ClockWorks, The Embedded Control Solutions Company, EtherSynch, FlashTec, Hyper Speed Control,
HyperLight Load, IntelliMOS, Libero, motorBench, mTouch, Powermite 3, Precision Edge, ProASIC, ProASIC Plus,
ProASIC Plus logo, Quiet-Wire, SmartFusion, SyncWorld, Temux, TimeCesium, TimeHub, TimePictra, TimeProvider,
Vite, WinPath, and ZL are registered trademarks of Microchip Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BlueSky, BodyCom,
CodeGuard, CryptoAuthentication, CryptoAutomotive, CryptoCompanion, CryptoController, dsPICDEM,
dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial Programming, ICSP,
INICnet, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, memBrain, Mindi, MiWi, MPASM, MPF,
MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation, PICDEM,
PICDEM.net, PICkit, PICtail, PowerSmart, PureSilicon, QMatrix, REAL ICE, Ripple Blocker, SAM-ICE, Serial Quad
I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC, USBCheck, VariSense,
ViewSpan, WiperLock, Wireless DNA, and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A.
and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
The Adaptec logo, Frequency on Demand, Silicon Storage Technology, and Symmcom are registered trademarks of
Microchip Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip
Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2020, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-6064-0

Quality Management System


For information regarding Microchip’s Quality Management Systems, please visit http://www.microchip.com/quality.

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 22


Worldwide Sales and Service
AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE
Corporate Office Australia - Sydney India - Bangalore Austria - Wels
2355 West Chandler Blvd. Tel: 61-2-9868-6733 Tel: 91-80-3090-4444 Tel: 43-7242-2244-39
Chandler, AZ 85224-6199 China - Beijing India - New Delhi Fax: 43-7242-2244-393
Tel: 480-792-7200 Tel: 86-10-8569-7000 Tel: 91-11-4160-8631 Denmark - Copenhagen
Fax: 480-792-7277 China - Chengdu India - Pune Tel: 45-4485-5910
Technical Support: Tel: 86-28-8665-5511 Tel: 91-20-4121-0141 Fax: 45-4485-2829
http://www.microchip.com/support China - Chongqing Japan - Osaka Finland - Espoo
Web Address: Tel: 86-23-8980-9588 Tel: 81-6-6152-7160 Tel: 358-9-4520-820
http://www.microchip.com China - Dongguan Japan - Tokyo France - Paris
Atlanta Tel: 86-769-8702-9880 Tel: 81-3-6880- 3770 Tel: 33-1-69-53-63-20
Duluth, GA China - Guangzhou Korea - Daegu Fax: 33-1-69-30-90-79
Tel: 678-957-9614 Tel: 86-20-8755-8029 Tel: 82-53-744-4301 Germany - Garching
Fax: 678-957-1455 China - Hangzhou Korea - Seoul Tel: 49-8931-9700
Austin, TX Tel: 86-571-8792-8115 Tel: 82-2-554-7200 Germany - Haan
Tel: 512-257-3370 China - Hong Kong SAR Malaysia - Kuala Lumpur Tel: 49-2129-3766400
Boston Tel: 852-2943-5100 Tel: 60-3-7651-7906 Germany - Heilbronn
Westborough, MA China - Nanjing Malaysia - Penang Tel: 49-7131-72400
Tel: 774-760-0087 Tel: 86-25-8473-2460 Tel: 60-4-227-8870 Germany - Karlsruhe
Fax: 774-760-0088 China - Qingdao Philippines - Manila Tel: 49-721-625370
Chicago Tel: 86-532-8502-7355 Tel: 63-2-634-9065 Germany - Munich
Itasca, IL China - Shanghai Singapore Tel: 49-89-627-144-0
Tel: 630-285-0071 Tel: 86-21-3326-8000 Tel: 65-6334-8870 Fax: 49-89-627-144-44
Fax: 630-285-0075 China - Shenyang Taiwan - Hsin Chu Germany - Rosenheim
Dallas Tel: 86-24-2334-2829 Tel: 886-3-577-8366 Tel: 49-8031-354-560
Addison, TX China - Shenzhen Taiwan - Kaohsiung Israel - Ra’anana
Tel: 972-818-7423 Tel: 86-755-8864-2200 Tel: 886-7-213-7830 Tel: 972-9-744-7705
Fax: 972-818-2924 China - Suzhou Taiwan - Taipei Italy - Milan
Detroit Tel: 86-186-6233-1526 Tel: 886-2-2508-8600 Tel: 39-0331-742611
Novi, MI China - Wuhan Thailand - Bangkok Fax: 39-0331-466781
Tel: 248-848-4000 Tel: 86-27-5980-5300 Tel: 66-2-694-1351 Italy - Padova
Houston, TX China - Xian Vietnam - Ho Chi Minh Tel: 39-049-7625286
Tel: 281-894-5983 Tel: 86-29-8833-7252 Tel: 84-28-5448-2100 Netherlands - Drunen
Indianapolis China - Xiamen Tel: 31-416-690399
Noblesville, IN Tel: 86-592-2388138 Fax: 31-416-690340
Tel: 317-773-8323 China - Zhuhai Norway - Trondheim
Fax: 317-773-5453 Tel: 86-756-3210040 Tel: 47-72884388
Tel: 317-536-2380 Poland - Warsaw
Los Angeles Tel: 48-22-3325737
Mission Viejo, CA Romania - Bucharest
Tel: 949-462-9523 Tel: 40-21-407-87-50
Fax: 949-462-9608 Spain - Madrid
Tel: 951-273-7800 Tel: 34-91-708-08-90
Raleigh, NC Fax: 34-91-708-08-91
Tel: 919-844-7510 Sweden - Gothenberg
New York, NY Tel: 46-31-704-60-40
Tel: 631-435-6000 Sweden - Stockholm
San Jose, CA Tel: 46-8-5090-4654
Tel: 408-735-9110 UK - Wokingham
Tel: 408-436-4270 Tel: 44-118-921-5800
Canada - Toronto Fax: 44-118-921-5820
Tel: 905-695-1980
Fax: 905-695-2078

© 2020 Microchip Technology Inc. Technical Brief DS90003235C-page 23

You might also like