{"id":851,"date":"2022-03-25T13:58:00","date_gmt":"2022-03-25T13:58:00","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=851"},"modified":"2024-11-20T12:07:31","modified_gmt":"2024-11-20T12:07:31","slug":"working-with-stm32-and-internal-dac-timer-trigger-dac-with-dma","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=851","title":{"rendered":"Working with STM32 and internal DAC: timer trigger DAC with DMA"},"content":{"rendered":"\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Digital-to-analog-converter.png\" alt=\"\" \/><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide, we took a look at the internal DAC of STM32F4 (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=783\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=783\" target=\"_blank\">here<\/a>) and we where are able to generate sawtooth using DAC and software trigger. In this guide, we shall use timer and DMA to generate sinewave using DAC.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>DAC trigger sources.<\/li>\n\n\n\n<li>DAC with timer trigger calculations.<\/li>\n\n\n\n<li>Configure DMA for DAC.<\/li>\n\n\n\n<li>Configure timer for DAC.<\/li>\n\n\n\n<li>Configure DAC with DMA.<\/li>\n\n\n\n<li>Code.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Trigger Sources of DAC:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/02\/Screen-Shot-2022-02-21-at-7.01.43-PM.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>The DAC of the STM32 has three major trigger sources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Timer triggered.<\/li>\n\n\n\n<li>External Triggered.<\/li>\n\n\n\n<li>Software triggered.<\/li>\n<\/ul>\n\n\n\n<p>In the previous guide, we covered the software trigger part.<\/p>\n\n\n\n<p>For timer trigger, there are several options:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"441\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-1024x441.png\" alt=\"\" class=\"wp-image-852\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-1024x441.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-300x129.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-768x331.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-1536x662.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-2048x883.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-1150x496.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-750x323.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-400x172.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.27.52-PM-250x108.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall use timer6 to trigger the DAC.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. DAC with timer trigger calculation:<\/h2>\n\n\n\n<p>Let\u2019s say we\u2019ve generated a sinewave lookup table with 128 sample points (Ns), and configured Timer2 so it triggers the DMA transfer to the DAC output. What would be the output sine wave frequency?<\/p>\n\n\n\n<p>Here are the formulas to be used<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2020\/06\/STM32-DAC-DMA-Sine-Wave-Generator-Tutorial.png?ezimgfmt=rs:347x43\/rscb6\/ngcb6\/notWebP\" alt=\"STM32 DAC DMA Sine Wave Generator Tutorial\" class=\"wp-image-4910\" \/><\/figure><\/div>\n\n\n<p>Where F<sub>CLK<\/sub>&nbsp;is the frequency of the clock used by your timer module, PSC is the Prescaler, and ARR is the value of the auto-reload register.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2020\/06\/STM32-DAC-Sine-Wave-Generator-With-DMA-And-Timer-Trigger-Tutorial.png?ezimgfmt=rs:328x39\/rscb6\/ngcb6\/notWebP\" alt=\"STM32 DAC Sine Wave Generator With DMA And Timer Trigger Tutorial\" class=\"wp-image-4909\" \/><\/figure><\/div>\n\n\n<p>Where Ns is the sample points number in the lookup table.<\/p>\n\n\n\n<p><strong>For example<\/strong>, let\u2019s assume the following settings:<\/p>\n\n\n\n<p>The FCLK is 80MHz, the PSC is 0, ARR is 1000, and the sine lookup table has 128 sample points. What would be the output sine wave frequency?<\/p>\n\n\n\n<p>TriggerFrequency = 80MHz \/ 1001 = 79920.08<\/p>\n\n\n\n<p>Output Sinewave Frequency = TriggerFrequency \/ 128 =&nbsp;<strong>624.37 Hz<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Configuring DMA for DAC:<\/h2>\n\n\n\n<p>Before we configure the DMA, we need to get which DMA is responsible for DAC.<\/p>\n\n\n\n<p>From STM32F407 Reference manual DMA section:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"235\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-1024x235.png\" alt=\"\" class=\"wp-image-853\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-1024x235.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-300x69.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-768x176.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-1536x353.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-2048x470.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-1150x264.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-750x172.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-400x92.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.37.05-PM-250x57.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From the table, we can see that DMA1, Stream5 and channel 7 is responsible for DAC.<\/p>\n\n\n\n<p>Hence we can start by creating macros for the channel number as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define ch7 0x07<\/pre><\/div>\n\n\n\n<p>Then we enable clock access to DMA1 as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA1EN;<\/pre><\/div>\n\n\n\n<p>Then we set the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Channel to number 7.<\/li>\n\n\n\n<li>Memory and peripheral size to 16-bit (half-word).<\/li>\n\n\n\n<li>Memory increment.<\/li>\n\n\n\n<li>Circular mode<\/li>\n\n\n\n<li>Direction from memory to peripheral.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"386\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-1024x386.png\" alt=\"\" class=\"wp-image-854\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-1024x386.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-300x113.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-768x290.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-1536x579.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-2048x772.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-1150x434.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-750x283.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-400x151.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-25-at-4.40.25-PM-250x94.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DMA1_Stream5-&gt;CR|=DMA_SxCR_MSIZE_0|DMA_SxCR_PSIZE_0|DMA_SxCR_MINC\n|DMA_SxCR_CIRC|DMA_SxCR_DIR_0;<\/pre><\/div>\n\n\n\n<p>Then we set the number of transfer as following (128 in this case):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DMA1_Stream5-&gt;NDTR=128;<\/pre><\/div>\n\n\n\n<p>Then we set the peripheral and memory location as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DMA1_Stream5-&gt;PAR=(uint32_t)&amp;(DAC-&gt;DHR12R1);\nDMA1_Stream5-&gt;M0AR=(uint32_t)function;\n<\/pre><\/div>\n\n\n\n<p>Disable direct mode and FIFO threshold to full:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DMA1_Stream5-&gt;FCR |= DMA_SxFCR_DMDIS;\nDMA1_Stream5-&gt;FCR |= (DMA_SxFCR_FTH_0 | DMA_SxFCR_FTH_1);\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Configure TIM6 for DAC:<\/h2>\n\n\n\n<p>First we enable clock access to timer6 as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">RCC-&gt;APB1ENR|=RCC_APB1ENR_TIM6EN;<\/pre><\/div>\n\n\n\n<p>We set the prescaler and ARR values as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">TIM6-&gt;PSC=0;\nTIM6-&gt;ARR=1;<\/pre><\/div>\n\n\n\n<p>Then set the update event is selected as a trigger output (TRGO) in CR2 as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">TIM6-&gt;CR2|=TIM_CR2_MMS_1;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Configuring DAC for DMA:<\/h2>\n\n\n\n<p>First thing enable clock access to DAC1 and GPIOA and set PA4 as analog mode:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\nRCC-&gt;APB1ENR|=RCC_APB1ENR_DACEN;\nGPIOA-&gt;MODER|=GPIO_MODER_MODE4_0|GPIO_MODER_MODE4_1;<\/pre><\/div>\n\n\n\n<p>Configure the amplitude to be maximum of 4095:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DAC1-&gt;CR|=DAC_CR_MAMP1_3|DAC_CR_MAMP1_1|DAC_CR_MAMP1_0|DAC_CR_MAMP1_2;<\/pre><\/div>\n\n\n\n<p>Then select timer6 as trigger source as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DAC1-&gt;CR&amp;=~(DAC_CR_TSEL1_0|DAC_CR_TSEL1_1|DAC_CR_TSEL1_2);<\/pre><\/div>\n\n\n\n<p>Enable DMA request as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DAC1-&gt;CR|=DAC_CR_DMAEN1;<\/pre><\/div>\n\n\n\n<p>Finally enable DAC then the DMA and finally the timer as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DAC1-&gt;CR|=DAC_CR_EN1;\nDMA1_Stream5-&gt;CR|=DMA_SxCR_EN;\nTIM6-&gt;CR1|=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p>Variables used for sinewave lookup table<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define   SINE_RES          128\nconst uint16_t function[SINE_RES] = { 2048, 2145, 2242, 2339, 2435, 2530, 2624, 2717, 2808, 2897,\n                                      2984, 3069, 3151, 3230, 3307, 3381, 3451, 3518, 3581, 3640,\n                                      3696, 3748, 3795, 3838, 3877, 3911, 3941, 3966, 3986, 4002,\n                                      4013, 4019, 4020, 4016, 4008, 3995, 3977, 3954, 3926, 3894,\n                                      3858, 3817, 3772, 3722, 3669, 3611, 3550, 3485, 3416, 3344,\n                                      3269, 3191, 3110, 3027, 2941, 2853, 2763, 2671, 2578, 2483,\n                                      2387, 2291, 2194, 2096, 1999, 1901, 1804, 1708, 1612, 1517,\n                                      1424, 1332, 1242, 1154, 1068, 985, 904, 826, 751, 679,\n                                      610, 545, 484, 426, 373, 323, 278, 237, 201, 169,\n                                      141, 118, 100, 87, 79, 75, 76, 82, 93, 109,\n                                      129, 154, 184, 218, 257, 300, 347, 399, 455, 514,\n                                      577, 644, 714, 788, 865, 944, 1026, 1111, 1198, 1287,\n                                      1378, 1471, 1565, 1660, 1756, 1853, 1950, 2047 };<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Code:<\/h2>\n\n\n\n<p>The code which including pushing the frequency of the MCU to 100MHz:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stm32f4xx.h&quot;                  \/\/ Device header\n#define ch7 0x07\n#define   SINE_RES          128\nconst uint16_t function[SINE_RES] = { 2048, 2145, 2242, 2339, 2435, 2530, 2624, 2717, 2808, 2897,\n                                      2984, 3069, 3151, 3230, 3307, 3381, 3451, 3518, 3581, 3640,\n                                      3696, 3748, 3795, 3838, 3877, 3911, 3941, 3966, 3986, 4002,\n                                      4013, 4019, 4020, 4016, 4008, 3995, 3977, 3954, 3926, 3894,\n                                      3858, 3817, 3772, 3722, 3669, 3611, 3550, 3485, 3416, 3344,\n                                      3269, 3191, 3110, 3027, 2941, 2853, 2763, 2671, 2578, 2483,\n                                      2387, 2291, 2194, 2096, 1999, 1901, 1804, 1708, 1612, 1517,\n                                      1424, 1332, 1242, 1154, 1068, 985, 904, 826, 751, 679,\n                                      610, 545, 484, 426, 373, 323, 278, 237, 201, 169,\n                                      141, 118, 100, 87, 79, 75, 76, 82, 93, 109,\n                                      129, 154, 184, 218, 257, 300, 347, 399, 455, 514,\n                                      577, 644, 714, 788, 865, 944, 1026, 1111, 1198, 1287,\n                                      1378, 1471, 1565, 1660, 1756, 1853, 1950, 2047 };\nvoid SysClockConfig(void) \/\/set the core frequency to 100MHz\n{\n#define PLL_M      4\n#define PLL_N      168\n#define PLL_P      2\n#define PLL_Q      9\n\n__IO uint32_t StartUpCounter = 0, HSEStatus = 0;\n\n\n  RCC-&gt;CR |= ((uint32_t)RCC_CR_HSEON);\n\n\n  do\n  {\n    HSEStatus = RCC-&gt;CR &amp; RCC_CR_HSERDY;\n    StartUpCounter++;\n  } while((HSEStatus == 0) &amp;&amp; (StartUpCounter != 3000));\n\n  if ((RCC-&gt;CR &amp; RCC_CR_HSERDY) != RESET)\n  {\n    HSEStatus = (uint32_t)0x01;\n  }\n  else\n  {\n    HSEStatus = (uint32_t)0x00;\n  }\n\n  if (HSEStatus == (uint32_t)0x01)\n  {\n\n    RCC-&gt;APB1ENR |= RCC_APB1ENR_PWREN;\n    PWR-&gt;CR &amp;= (uint32_t)~(PWR_CR_VOS);\n\n\n    RCC-&gt;CFGR |= RCC_CFGR_HPRE_DIV1;\n\n\n    RCC-&gt;CFGR |= RCC_CFGR_PPRE2_DIV2;\n\n\n    RCC-&gt;CFGR |= RCC_CFGR_PPRE1_DIV4;\n\n\n    RCC-&gt;PLLCFGR = PLL_M | (PLL_N &lt;&lt; 6) | (((PLL_P &gt;&gt; 1) -1) &lt;&lt; 16) |\n                   (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q &lt;&lt; 24);\n\n\n    RCC-&gt;CR |= RCC_CR_PLLON;\n\n\n    while((RCC-&gt;CR &amp; RCC_CR_PLLRDY) == 0)\n    {\n    }\n\n    \/* Configure Flash prefetch, Instruction cache, Data cache and wait state *\/\n    FLASH-&gt;ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_3WS;\n\n    \/* Select the main PLL as system clock source *\/\n    RCC-&gt;CFGR &amp;= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\n    RCC-&gt;CFGR |= RCC_CFGR_SW_PLL;\n\n    \/* Wait till the main PLL is used as system clock source *\/\n    while ((RCC-&gt;CFGR &amp; (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL)\n    {;}\n  }\n  else\n  { \/* If HSE fails to start-up, the application will have wrong clock\n         configuration. User can add here some code to deal with this error *\/\n  }\n\n}\n\n\nint main(void)\n{\nSysClockConfig();\n\/*fill the buffer*\/\n\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\nRCC-&gt;APB1ENR|=RCC_APB1ENR_DACEN;\nRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM6EN;\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA1EN;\n\nDMA1_Stream5-&gt;CR&amp;=~DMA_SxCR_EN;\nwhile(((DMA1_Stream5-&gt;CR)&amp;DMA_SxCR_EN)==1);\nDMA1_Stream5-&gt;CR|=(ch7&lt;&lt;25);\nDMA1_Stream5-&gt;CR|=DMA_SxCR_MSIZE_0|DMA_SxCR_PSIZE_0|DMA_SxCR_MINC\n|DMA_SxCR_CIRC|DMA_SxCR_DIR_0;\nDMA1_Stream5-&gt;NDTR=128;\nDMA1_Stream5-&gt;PAR=(uint32_t)&amp;(DAC-&gt;DHR12R1);\nDMA1_Stream5-&gt;M0AR=(uint32_t)function;\n\nDMA1_Stream5-&gt;FCR |= DMA_SxFCR_DMDIS;\nDMA1_Stream5-&gt;FCR |= (DMA_SxFCR_FTH_0 | DMA_SxFCR_FTH_1);\n\nTIM6-&gt;PSC=0;\nTIM6-&gt;ARR=1;\nTIM6-&gt;CR2|=TIM_CR2_MMS_1;\n\nGPIOA-&gt;MODER|=GPIO_MODER_MODE4_0|GPIO_MODER_MODE4_1;\n\nDAC1-&gt;CR|=DAC_CR_MAMP1_3|DAC_CR_MAMP1_1|DAC_CR_MAMP1_0|DAC_CR_MAMP1_2;\nDAC1-&gt;CR&amp;=~(DAC_CR_TSEL1_0|DAC_CR_TSEL1_1|DAC_CR_TSEL1_2);\nDAC1-&gt;CR|=DAC_CR_DMAEN1;\nDAC1-&gt;CR|=DAC_CR_TEN1;\n\/\/DAC1-&gt;CR|=DAC_CR_BOFF1;\nDAC1-&gt;CR|=DAC_CR_EN1;\nDMA1_Stream5-&gt;CR|=DMA_SxCR_EN;\nTIM6-&gt;CR1|=TIM_CR1_CEN;\n\nwhile(1)\n\t{\n\t\t\/*\n\t\tDo something else here\n\t\t*\/\n\t}\n\n\n\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Results:<\/h2>\n\n\n\n<p>After compiling and uploading the code to STM32F407 and probe PA5 using oscilloscope, you should get this sinewave with frequency near 82KHz.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"480\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint8.png\" alt=\"\" class=\"wp-image-855\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint8.png 800w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint8-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint8-768x461.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint8-750x450.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint8-400x240.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint8-250x150.png 250w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide, we took a look at the internal DAC of STM32F4 (here) and we where are able to generate sawtooth using DAC and software trigger. In this guide, we shall use timer and DMA to generate sinewave using DAC. In this guide, we shall cover the following: 1. Trigger Sources of DAC: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-851","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/851"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=851"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions"}],"predecessor-version":[{"id":3032,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/851\/revisions\/3032"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}