Generating Sine Wave Signal Using 10-Bit DAC

The DAC can be used to generate a sine wave signal.

To generate this signal, the VREF and the DAC are initialized first, then the output value can be changed by writing a new value to the DACn.DATA register.

Before the sine wave is generated, the samples corresponding to a period are calculated and stored in a buffer.

for(i = 0; i < SINE_PERIOD_STEPS; i++)
{
	sineWave[i] = SINE_DC_OFFSET + SINE_AMPLITUDE * sin(2 * M_PI * i / SINE_PERIOD_STEPS);
}

The sinusoidal waveform is created using a fixed number of steps (N_SAMPLES). To create a sine wave signal with a specific frequency (SIGNAL_FREQ), all steps are executed in one period resulting in the following sample rate:

SAMPLE_RATE=1STEP_DELAY_TIME=SIGNAL_FREQ×N_SAMPLES
while (1)
{
    DAC0_setVal(sineWave[sineIndex++]);
    if(sineIndex == SINE_PERIOD_STEPS)
        sineIndex = 0;
    _delay_us(STEP_DELAY_TIME);
} 

Tip: The full code example is also available in Appendix.