jlim14
jlim14

Reputation: 1

How to generate Sine Wave on top of Triangular Wave using the DAC with DMA of STM32G4

I have a STM32G4 nucleo board. I would like to generate a summation waveform consisting of triangular wave (~1Hz) and sine wave (500Hz) using the DAC and DMA on STM32G4.

Is it possible to get the summation waveform out from one DAC channel? Can anyone help me with this? Any help is appreciated. Thanks.

I computed a lookup table for one cycle of sine wave. And I added the sine wave onto an incrementing line. Then I realized it will only generate a triangle wave with one cycle of sine wave when it is ramping up and one cycle of sine wave when it is ramping down.

#define dac_buf_len 200
HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t *) dac, dac_buf_len,DAC_ALIGN_12B_R);

//generate sine wave
for (uint32_t i=0; i < dac_buf_len; i++)
{
    float s = (float) i/(float)(dac_buf_len-1);
    dac_sin[i] = sine_amplitude * sin(2*M_PI*s);    //one cycle of sine wave
}

//generate triangular wave (ramp up)
for (uint32_t i=0; i<dac_buf_len/2; i++)
{
    dac_triangular[i] = 0.006*i - 0.5;
}

//generate triangular wave (ramp down)
for (uint32_t i=0; i<dac_buf_len/2; i++)
{
    dac_triangular[100+i] = -0.006*i + 0.1;
}

//sum two waves together
for (uint32_t i=0; i< dac_buf_len; i++)
{
    dac[i] = dac_sin[i] + dac_triangular[i];
}

Upvotes: 0

Views: 508

Answers (1)

Chris_B
Chris_B

Reputation: 489

for me it sounds like you'd want the DAC Peripheral / the DMA Peripheral do the Math automatically do for you. IMHO this is simply not possible and the wrong approach.

The correct approach would be: calculate the sinus wave, calculate the triangular wave, add both values (for each sample), convert it into the corresponding integer value and store it in the DMA buffer. Then the DAC will create the output voltages that correspond to a superposition of both signals you generated.

if you want to fill the DMA Buffer blockwise, do the same, but in a loop.

Upvotes: 0

Related Questions