Generating Constant Analog Signal Code
#define F_CPU (4000000UL)
#include <avr/io.h>
#include <util/delay.h>
#define DAC_EXAMPLE_VALUE (0x258)
#define VREF_STARTUP_TIME (50)
#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
| VREF_ALWAYSON_bm;
_delay_us(VREF_STARTUP_TIME);
}
static void DAC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm
| DAC_OUTEN_bm
| DAC_RUNSTDBY_bm;
}
static void DAC0_setVal(uint16_t value)
{
DAC0.DATAL = (value & LSB_MASK) << 6;
DAC0.DATAH = value >> 2;
}
int main(void)
{
VREF_init();
DAC0_init();
DAC0_setVal(DAC_EXAMPLE_VALUE);
while (1)
{
;
}
}
Generating Sine Wave Signal Code
#define F_CPU (4000000UL)
#include <avr/io.h>
#include <util/delay.h>
#include <math.h>
#define VREF_STARTUP_TIME (50)
#define LSB_MASK (0x03)
#define SINE_PERIOD_STEPS (100)
#define SINE_AMPLITUDE (511)
#define SINE_DC_OFFSET (512)
#define SINE_FREQ (100)
#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);
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
| VREF_ALWAYSON_bm;
_delay_us(VREF_STARTUP_TIME);
}
static void DAC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm
| DAC_OUTEN_bm
| DAC_RUNSTDBY_bm;
}
static void DAC0_setVal(uint16_t value)
{
DAC0.DATAL = (value & LSB_MASK) << 6;
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);
}
}
Reading the DAC Internally with the ADC Code
#define F_CPU (4000000UL)
#include <avr/io.h>
#include <util/delay.h>
#define VREF_STARTUP_TIME (50)
#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
| VREF_ALWAYSON_bm;
VREF.ADC0REF = VREF_REFSEL_2V048_gc
| VREF_ALWAYSON_bm;
_delay_us(VREF_STARTUP_TIME);
}
static void DAC0_init(void)
{
DAC0.CTRLA = DAC_ENABLE_bm;
}
static void DAC0_setVal(uint16_t value)
{
DAC0.DATAL = (value & LSB_MASK) << 6;
DAC0.DATAH = value >> 2;
}
static void ADC0_init(void)
{
ADC0.CTRLC = ADC_PRESC_DIV2_gc;
ADC0.CTRLA = ADC_ENABLE_bm
| ADC_RESSEL_12BIT_gc;
ADC0.MUXPOS = ADC_MUXPOS_DAC0_gc
}
static uint16_t ADC0_read(void)
{
ADC0.COMMAND = ADC_STCONV_bm;
while(!(ADC0.INTFLAGS & ADC_RESRDY_bm))
{
;
}
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();
dacVal++;
DAC0_setVal(dacVal);
}
}
Generating Amplitude Modulation Signal Code
#define F_CPU (4000000UL)
#include <avr/io.h>
#include <util/delay.h>
#include <math.h>
#define VREF_STARTUP_TIME (50)
#define LSB_MASK (0x03)
#define SINE_PERIOD_STEPS (100)
#define SINE_AMPLITUDE (511)
#define SINE_DC_OFFSET (512)
#define SINE_FREQ (50)
#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);
uint16_t sineWave[SINE_PERIOD_STEPS];
static void PORT_init (void)
{
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
| VREF_ALWAYSON_bm;
_delay_us(VREF_STARTUP_TIME);
}
static void DAC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm
| DAC_OUTEN_bm
| DAC_RUNSTDBY_bm;
}
static void DAC0_setVal(uint16_t value)
{
DAC0.DATAL = (value & LSB_MASK) << 6;
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);
}
}