{"id":1196,"date":"2022-09-11T04:43:24","date_gmt":"2022-09-11T04:43:24","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1196"},"modified":"2022-09-11T04:43:27","modified_gmt":"2022-09-11T04:43:27","slug":"getting-started-with-stm32l053-dac-with-dma-and-timer-trigger","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1196","title":{"rendered":"Getting started with STM32L053: DAC with DMA and Timer trigger"},"content":{"rendered":"\n<div class=\"wp-block-image\"><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\n<p>In the previous guide, we took a look at the internal DAC of STM32L053 (<a href=\"https:\/\/blog.embeddedexpert.io\/?p=1192\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1192\" target=\"_blank\" rel=\"noreferrer noopener\">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>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>DAC trigger sources.<\/li><li>DAC with timer trigger calculations.<\/li><li>Configure DMA for DAC.<\/li><li>Configure timer for DAC.<\/li><li>Configure DAC with DMA.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Trigger Sources of DAC:<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><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\"><li>Timer triggered.<\/li><li>External Triggered.<\/li><li>Software triggered.<\/li><\/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\n<figure class=\"wp-block-image\"><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>\n\n\n\n<p>In this guide, we shall use timer6 to trigger the DAC.<\/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\n<figure class=\"wp-block-image\"><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>\n\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\n<figure class=\"wp-block-image\"><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>\n\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 =\u00a0<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 the 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 STM32L053 Reference manual DMA section:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"454\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-1024x454.png\" alt=\"\" class=\"wp-image-1197\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-1024x454.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-300x133.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-768x340.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-1536x681.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-2048x907.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-1150x510.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-750x332.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-400x177.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.25.12-AM-250x111.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We can find that Channel2 C9S is responsible for DAC Channel1<\/p>\n\n\n\n<p>Then we start of by enabling clock access to DMA1:<\/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;AHBENR|=RCC_AHBENR_DMAEN;<\/pre><\/div>\n\n\n\n<p>Configure the DMA to do the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Memory and peripheral size to 16-bit (half-word).<\/li><li>Memory increment mode<\/li><li>Circular.<\/li><li>Read from memory.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"399\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-1024x399.png\" alt=\"\" class=\"wp-image-1198\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-1024x399.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-300x117.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-768x300.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-1536x599.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-2048x799.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-1150x448.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-750x292.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-400x156.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-11-at-7.29.25-AM-250x97.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_Channel2-&gt;CCR=DMA_CCR_MSIZE_0|DMA_CCR_PSIZE_0|DMA_CCR_MINC|DMA_CCR_CIRC|DMA_CCR_DIR;<\/pre><\/div>\n\n\n\n<p>Then set the length:<\/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_Channel2-&gt;CNDTR=SINE_RES;<\/pre><\/div>\n\n\n\n<p>Memory address that holds the data:<\/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_Channel2-&gt;CMAR=(uint32_t)function;<\/pre><\/div>\n\n\n\n<p>Peripheral address:<\/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_Channel2-&gt;CPAR=(uint32_t)(&amp;DAC1-&gt;DHR12R1);<\/pre><\/div>\n\n\n\n<p>Set the C2S to be 9:<\/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;}\">DMA_CSELR|=(0x09&lt;&lt;4);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Configure Timer6 for DAC:<\/h2>\n\n\n\n<p>In order to configure timer6, we need just those four lines:<\/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;}\">\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM6EN;\n\tTIM6-&gt;PSC=0;\n\tTIM6-&gt;ARR=1;\n\tTIM6-&gt;CR2|=TIM_CR2_MMS_1;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. Configure DAC for DMA:<\/h2>\n\n\n\n<p>First enable clock access to DAC1:<\/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_DACEN;<\/pre><\/div>\n\n\n\n<p>Since Timer6 is responsible for triggering the DMA, we shall set trigger selection for timer6 which is 000<\/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>Set amplitude to be 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;<\/pre><\/div>\n\n\n\n<p>Enable trigger:<\/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_TEN1;<\/pre><\/div>\n\n\n\n<p>Enable output buffer:<\/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_BOFF1;<\/pre><\/div>\n\n\n\n<p>Enable DMA request:<\/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 DMA1_Channel2,DAC and Timer6:<\/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;}\">\n\tDMA1_Channel2-&gt;CCR|=DMA_CCR_EN;\n\tDAC1-&gt;CR|=DAC_CR_EN1;\n\tTIM6-&gt;CR1|=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p>The lookup table for sinewave:<\/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\nuint16_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 };\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Code:<\/h2>\n\n\n\n<p>Hence, the entire code:<\/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;stm32l0xx.h&quot;\n#include &quot;core.h&quot;\n#define   SINE_RES          128\nuint16_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 };\n\n\n\n\n#define DMA_CSELR  (*(volatile unsigned int *)(0x400200a8))\n\nint main(void)\n{\n\t\/*Set the core frequency to 32MHz*\/\n\tconfig_core_32mhz();\n\n\t\/*Set PA4 to Analog mode*\/\n\tRCC-&gt;IOPENR|=RCC_IOPENR_GPIOAEN;\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE4;\n\n\t\/*Configure the DAC*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_DACEN;\n\tDAC1-&gt;CR|=DAC_CR_MAMP1_3|DAC_CR_MAMP1_1|DAC_CR_MAMP1_0;\n\tDAC1-&gt;CR|=DAC_CR_TEN1;\n\tDAC1-&gt;CR|=DAC_CR_BOFF1;\n\tDAC1-&gt;CR|=DAC_CR_DMAEN1;\n\n\t\/*TIMER6 configuration*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM6EN;\n\tTIM6-&gt;PSC=0;\n\tTIM6-&gt;ARR=1;\n\tTIM6-&gt;CR2|=TIM_CR2_MMS_1;\n\n\tRCC-&gt;AHBENR|=RCC_AHBENR_DMAEN;\n\tDMA1_Channel2-&gt;CCR=DMA_CCR_MSIZE_0|DMA_CCR_PSIZE_0|DMA_CCR_MINC|DMA_CCR_CIRC|DMA_CCR_DIR;\n\tDMA1_Channel2-&gt;CNDTR=SINE_RES;\n\tDMA1_Channel2-&gt;CMAR=(uint32_t)function;\n\tDMA1_Channel2-&gt;CPAR=(uint32_t)(&amp;DAC1-&gt;DHR12R1);\n\tDMA_CSELR|=(0x09&lt;&lt;4);\n\n\tDMA1_Channel2-&gt;CCR|=DMA_CCR_EN;\n\tDAC1-&gt;CR|=DAC_CR_EN1;\n\tTIM6-&gt;CR1|=TIM_CR1_CEN;\n\twhile(1)\n\t{\n\n\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p>How to set STM32L053 to 32MHz, please refer to this topic: <a href=\"https:\/\/blog.embeddedexpert.io\/?p=1172\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1172\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Result:<\/h2>\n\n\n\n<p>When you probe pin PA4 using oscilloscope, you should see this:<\/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\/09\/DS1Z_QuickPrint1.png\" alt=\"\" class=\"wp-image-1199\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/DS1Z_QuickPrint1.png 800w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/DS1Z_QuickPrint1-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/DS1Z_QuickPrint1-768x461.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/DS1Z_QuickPrint1-750x450.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/DS1Z_QuickPrint1-400x240.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/DS1Z_QuickPrint1-250x150.png 250w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide, we took a look at the internal DAC of STM32L053 (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: DAC trigger sources. DAC with [&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-1196","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\/1196"}],"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=1196"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1196\/revisions"}],"predecessor-version":[{"id":1200,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1196\/revisions\/1200"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}