stm32L4 : How to generate triangular wave using DAC DMA, uneven slopes

Viewed 4725

I am trying to generate a triangular wave of frequency 8kHz using DAC and DMA. DAC is triggered using a timer so that the DAC speed will be 1 MSPS. I am working on stm32L476 discovery board. I am using stm32CUBEMX for code initialization. My configurations are as follows:

In stm32CUBEMX :

 Clock configuration : system clock = 80MHz 
                                     APB1 and APB2 peripheral clocks = 20 MHz
                                     APB1 and APB2 timer clocks = 40 MHz
                                     PLL source mux : HSI
                                     PLLM = /1
                                     *N = x10
                                      /R = /8
                                     PLLCLK selected
    Timer : Prescalar = 39
                UP counter
                Period = 1
                So that output frequency is 1 MHz to trigger the DAC.
    DAC :
              Output buffer : Enable
              Trigger : TIM7 Event out
              DMA(option inside DAC configuration in cubeMX): DAC channel 2(PA5), Half word, circular mode, priority = very             high, memory(ticked) in cubeMX

I am using ac6SW4stm32(system workbench) for writing code. I created an array like this for triangular wave.

const uint16_t val[]={130,260,390,520,650,780,910,1040,
1170,1300,1430,1560,1690,1820,1950,2080,
2210,2340,2470,2600,2730,2860,2990,3120,
3250,3380,3510,3640,3770,3900,4030,4095,
4030,3900,3770,3640,3510,3380,3250,3120,
2990,2860,2730,2600,2470,2340,2210,2080,
1950,1820,1690,1560,1430,1300,1170,1040,
910,780,650,520,390,260,130,0};

In main, I added the following three statements. These statements start timer, DAC and DMA respectively.

HAL_TIM_Base_Start(&htim7);
HAL_DAC_Start(&hdac1,DAC_CHANNEL_2);
HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t*)val, 64, DAC_ALIGN_12B_R);

At the output, I am not getting proper triangular wave. Frequency of the wave is correct but the wave is not perfectly triangular. It has different rising and falling slopes(unequal edges).

1 Answers
Related