/* p7_6.c: Use DAC to generate sawtooth waveform * The DAC is initialized with no buffer and use software trigger, * so every write to the DAC data register will change the analog output. * This program uses a pre-calculated lookup table to generate a * sine wave output through a DAC. * The output of DAC is on pin PA02. * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" unsigned char* ARRAY_PORT_PINCFG0 = (unsigned char*)®_PORT_PINCFG0; unsigned char* ARRAY_PORT_PMUX0 = (unsigned char*)®_PORT_PMUX0; int main (void) { int i; const static int sineWave[] = {512, 767, 954, 1023, 954, 767, 512, 256, 69, 0, 69, 256}; REG_PM_APBCMASK |= 0x40000; /* enable bus clock for DAC */ REG_GCLK_CLKCTRL = 0x4021; /* GCLK0 to DAC */ REG_DAC_CTRLB = 0x41; /* Vref = AVcc; enable output driver */ REG_DAC_CTRLA = 2; /* enable DAC */ ARRAY_PORT_PINCFG0[2] |= 1; /* Use PMUX for PA02 */ ARRAY_PORT_PMUX0[1] = 0x01; /* PA02 = VOUT */ while (1) { for (i = 0; i < sizeof(sineWave)/sizeof(int); i++) { REG_DAC_DATA = sineWave[i]; /* write value of i to DAC */ } } }