Generating Constant Analog Signal Code Example
#define F_CPU (3333333UL)
#define DAC_EXAMPLE_VALUE (0x20)
#define VREF_STARTUP_MICROS (25)
#include <avr/io.h>
#include <util/delay.h>
void VREF_init(void);
void DAC0_init(void);
void DAC0_setVal(uint8_t val);
void VREF_init(void)
{
VREF.CTRLA |= VREF_DAC0REFSEL_4V34_gc;
VREF.CTRLB |= VREF_DAC0REFEN_bm;
_delay_us(VREF_STARTUP_MICROS);
}
void DAC0_init(void)
{
PORTA.PIN6CTRL &= ~PORT_ISC_gm;
PORTA.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTA.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm | DAC_RUNSTDBY_bm;
}
void DAC0_setVal(uint8_t val)
{
DAC0.DATA = val;
}
int main(void)
{
VREF_init();
DAC0_init();
DAC0_setVal(DAC_EXAMPLE_VALUE);
while (1)
{
;
}
}
Generating Sine Wave Signal Code Example
#define F_CPU (3333333UL)
#define VREF_STARTUP_MICROS (25)
#define SINE_WAVE_STEPS (100)
#define SINE_AMPLITUDE (127)
#define SINE_DC_OFFSET (128)
#define M_2PI (2 * M_PI)
#define OUTPUT_FREQ (50)
#define STEP_DELAY_MICROS ((1000000 / OUTPUT_FREQ) / SINE_WAVE_STEPS)
#include <avr/io.h>
#include <util/delay.h>
#include <math.h>
uint8_t sineWave[SINE_WAVE_STEPS];
void sineWaveInit(void);
void VREF_init(void);
void DAC0_init(void);
void DAC0_setVal(uint8_t val);
void sineWaveInit(void)
{
for(uint16_t i = 0; i < SINE_WAVE_STEPS; i++)
{
sineWave[i] = SINE_DC_OFFSET + SINE_AMPLITUDE * sin(i * M_2PI / SINE_WAVE_STEPS);
}
}
void VREF_init(void)
{
VREF.CTRLA |= VREF_DAC0REFSEL_4V34_gc;
VREF.CTRLB |= VREF_DAC0REFEN_bm;
_delay_us(VREF_STARTUP_MICROS);
}
void DAC0_init(void)
{
PORTA.PIN6CTRL &= ~PORT_ISC_gm;
PORTA.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTA.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.DATA = SINE_DC_OFFSET;
DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm | DAC_RUNSTDBY_bm;
}
void DAC0_setVal(uint8_t val)
{
DAC0.DATA = val;
}
int main(void)
{
uint16_t i = 0;
VREF_init();
DAC0_init();
sineWaveInit();
while (1)
{
DAC0_setVal(sineWave[i++]);
i = i % SINE_WAVE_STEPS;
_delay_us(STEP_DELAY_MICROS);
}
}
Reading DAC Internally With ADC Code Example
#define F_CPU (3333333UL)
#define VREF_STARTUP_MICROS (25)
#include <avr/io.h>
#include <util/delay.h>
void VREF_init(void);
void DAC0_init(void);
void ADC0_init(void);
uint16_t ADC0_read(void);
void DAC0_setVal(uint8_t val);
void VREF_init(void)
{
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);
}
void DAC0_init(void)
{
DAC0.CTRLA = DAC_ENABLE_bm;
}
void ADC0_init(void)
{
ADC0.CTRLC = ADC_PRESC_DIV4_gc
| ADC_REFSEL_INTREF_gc
| ADC_SAMPCAP_bm;
ADC0.CTRLA = ADC_ENABLE_bm
| ADC_RESSEL_10BIT_gc;
ADC0.MUXPOS = ADC_MUXPOS_DAC0_gc;
}
uint16_t ADC0_read(void)
{
ADC0.COMMAND = ADC_STCONV_bm;
while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) )
{
;
}
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}
void DAC0_setVal(uint8_t val)
{
DAC0.DATA = val;
}
int main(void)
{
uint8_t dacVal = 0;
volatile uint16_t adcVal = 0;
VREF_init();
DAC0_init();
ADC0_init();
_delay_us(VREF_STARTUP_MICROS);
DAC0_setVal(dacVal);
while (1)
{
adcVal = ADC0_read();
dacVal++;
DAC0_setVal(dacVal);
}
}
Using DAC as Negative Input for AC Code Example
#define F_CPU (3333333UL)
#define DAC_DATA_1V4 (239)
#define VREF_STARTUP_MICROS (25)
#include <avr/io.h>
#include <util/delay.h>
void VREF_init(void);
void DAC0_setVal(uint8_t val);
void DAC0_init(void);
void AC0_init(void);
void VREF_init(void)
{
VREF.CTRLA |= VREF_DAC0REFSEL_1V5_gc;
VREF.CTRLB |= VREF_DAC0REFEN_bm;
_delay_us(VREF_STARTUP_MICROS);
}
void DAC0_setVal(uint8_t val)
{
DAC0.DATA = val;
}
void DAC0_init(void)
{
PORTA.PIN6CTRL &= ~PORT_ISC_gm;
PORTA.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTA.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm | DAC_RUNSTDBY_bm;
}
void AC0_init(void)
{
AC0.MUXCTRLA = AC_MUXNEG_DAC_gc
| AC_MUXPOS_PIN0_gc;
AC0.CTRLA = AC_ENABLE_bm
| AC_OUTEN_bm
| AC_RUNSTDBY_bm
| AC_LPMODE_bm;
}
int main(void)
{
VREF_init();
DAC0_init();
AC0_init();
DAC0_setVal(DAC_DATA_1V4);
while (1)
{
;
}
}