Search code examples
embeddedstm32dacstm32cubemxcubemx

Outputting a Triangular Waveform from a DAC Output Pin at a rate of 77khz on an stm board


I am currently working on an stmG474-re development board and aiming to use dac1 channel 2 to output a triangular waveform at a frequency of 77khz with the high resolution timer but struggling to reach the higher frequency of 77khz required.

Generating a triangular waveform is not an issue I have done this through multiple methods now via:

  • inbuilt stm triangular waveform generator from the DAC with HRTIM as a trigger
  • writing an array to the DMA and using that (this method is preferable as I plan to generate custom waveforms in the future
  • Use a timer interrupt to update the DAC output manually at the desired rate and calculating a dac value in the interrupt handler.

However with each of these method I cannot seem to reach the high frequency required. Using the triangular waveform I am constrained to a period of 72 us. This is something I observe on my oscilloscope. Going above a frequency of approx 500kHz with the DMA creates a bottleneck and the DAC can't update fast enough instead outputtting a constant value.

I'm no expert in using cubeMX and pretty new to embedded programming but if anybody could suggest methods for outputting a triangular waveform at this frequency that would be great. Other Points:

  • when using a sawtooth waveform generator this seems to reach the desired frequency rate with no issue.
  • HCLK operating at 170MHz and PCLK1 also operating at 170MHz
  • This is for DAC 1 Out 2 pin PA6.
  • Most work here is done via configuration throug the cubeMX IDE.
  • For the triangular waveform generator more info: An internal triangle counter is incremented three dac_hclk clock cycles after each trigger event according to the manual

Solution

  • Use TIM6 instead which is "dedicated" for DAC.

    if TIM clock is 170MHz

    • PSC = 0
    • ARR = 220
    • ERROR: 0.1%

    You will need 10 samples per period.