/* p7_5.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. * The loop count i is incremented by 1 every loop. The 10-bit DAC * has the range of 0-1023. * The sawtooth has 1024 steps. The period of the * waveform is 12.3ms and the frequency is about 81.5 Hz. * 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; REG_PM_APBCMASK |= 0x40000; /* enable bus clock for DAC */ REG_GCLK_CLKCTRL = 0x4021; /* GCLK0 to DAC */ REG_DAC_CTRLB = 0x41; /* Vref = AVcc; enable external 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 < 1024; i++) { REG_DAC_DATA = i; /* write value of i to DAC */ } } }