/* p7_4.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 calculates a lookup table at the initialization to be used * 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" #include unsigned char* ARRAY_PORT_PINCFG0 = (unsigned char*)®_PORT_PINCFG0; unsigned char* ARRAY_PORT_PMUX0 = (unsigned char*)®_PORT_PMUX0; #define WAVEFORM_LENGTH 256 int sinewave[WAVEFORM_LENGTH]; int main (void) { int i; double Radians; const double M_PI = 3.1415926535897; /* construct data table for a sine wave */ Radians = 2 * M_PI / WAVEFORM_LENGTH; for (i = 0; i < WAVEFORM_LENGTH; i++) { sinewave[i] = 511 * (sin(Radians * i) + 1); } 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 < WAVEFORM_LENGTH; i++) { REG_DAC_DATA = sinewave[i]; /* write value of i to DAC */ } } }