{"id":224,"date":"2021-07-29T07:44:25","date_gmt":"2021-07-29T07:44:25","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=224"},"modified":"2021-07-29T07:44:28","modified_gmt":"2021-07-29T07:44:28","slug":"working-with-adc-and-stm32-part2-single-channel-continuous-conversion","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=224","title":{"rendered":"Working with ADC and STM32 part2: Single Channel Continuous Conversion"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"323\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-1024x323.png\" alt=\"\" class=\"wp-image-225\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-1024x323.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-300x95.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-768x243.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-1150x363.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-750x237.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-400x126.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1-250x79.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1.png 1200w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>ADC Logo<\/figcaption><\/figure>\n\n\n\n<p>In the part 1 of working with ADC and STM32 (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=200\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=200\" target=\"_blank\">link<\/a>), we talked about the ADC and how it does work, configure it to read a potentiometer using single conversion method. In this guide we shall discuss the continuous ADC conversion polling mode (i.e. wait until end of conversion). <\/p>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Difference between Single Conversion and Continuous Conversion.<\/li><li>What extra steps needed to enable continuous mode.<\/li><li>Required parts and schematics<\/li><li>Code<\/li><li>Results<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Difference between Single Conversion and Continuous Conversion <\/h2>\n\n\n\n<p>In the single conversion mode, the ADC does one conversion and stop as soon as the conversion ended. The process can&#8217;t started unless the start is triggered by software. This can be useful for certain application like monitor battery capacity like every 10 minutes. <\/p>\n\n\n\n<p>In continuous conversion mode, the ADC starts a new conversion as soon as it finishes one. This mode has the advantage of keeping the ADC value up to date for control purposes such as temperature, flow rate etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. What the extra steps needed to enable Continuous Conversion<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\"> <\/h2>\n\n\n\n<p>As we saw from the previous guide, we enabled the adc manually every time we needed to get a new adc value. To enable continuous mode, we need to enable CONT_bit (Bit 1) in Control Register 2 (CR2)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"345\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-1024x345.png\" alt=\"\" class=\"wp-image-229\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-1024x345.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-300x101.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-768x259.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-1536x518.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-1150x388.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-750x253.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-400x135.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM-250x84.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-29-at-10.29.54-AM.png 1762w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Enabling Continuous mode of the ADC<\/figcaption><\/figure>\n\n\n\n<p>Also, The SWSTART shall be set in the initialization function of the ADC<\/p>\n\n\n\n<p>Hence, the new ADC initialize function looks like this:<\/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; \/\/enable gpio a clock\nRCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN; \/\/enable adc clock\nGPIOA-&gt;MODER|=GPIO_MODER_MODER1;  \/\/set the PA1 to analog mode\nADC1-&gt;CR2=0; \/\/disable the adc\nADC1-&gt;CR2|=ADC_CR2_CONT; \t\nADC1-&gt;SQR3|=1; \/\/we are converting only one channel\nADC1-&gt;CR2|=1; \/\/enable the adc \nADC1-&gt;CR2|=ADC_CR2_SWSTART; \/\/start adc conversion\t<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Required parts and schematic<\/h2>\n\n\n\n<p>The required parts, schematics and connection is the same as part one of this guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=200\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=200\" target=\"_blank\">link<\/a>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code<\/h2>\n\n\n\n<p>The final code shall be like this<\/p>\n\n\n\n<p><\/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;}\">\/\/read the analog voltage on PA1\n\n#include &quot;stm32f4xx.h&quot;                  \/\/ Device header\n#include &quot;stdint.h&quot;\nvoid ADC_init(void);\nvoid delayMs(int delay);\nuint16_t adcValue;\n\nint main(void){\nADC_init();\n\n\t\nwhile(1){\n\n\/\/wait for the end of conversion \nwhile(!((ADC1-&gt;SR)&amp;ADC_SR_EOC)){;}\nadcValue=ADC1-&gt;DR;\ndelayMs(10);\n}\n\n}\nvoid ADC_init(void){\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN; \/\/enable gpio a clock\nRCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN; \/\/enable adc clock\nGPIOA-&gt;MODER|=GPIO_MODER_MODER1;  \/\/set the PA1 to analog mode\nADC1-&gt;CR2=0; \/\/disable the adc\nADC1-&gt;CR2|=ADC_CR2_CONT; \t\nADC1-&gt;SQR3|=1; \/\/we are converting only one channel\nADC1-&gt;CR2|=1; \/\/enable the adc \nADC1-&gt;CR2|=ADC_CR2_SWSTART; \/\/start adc conversion\t\n}\n\nvoid delayMs(int delay)\n{\nint i;\nfor(; delay&gt;0 ;delay--)\n{\nfor(i =0; i&lt;3195;i++);\n}\n\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results<\/h2>\n\n\n\n<p>After compiling the code, uploading the code to mcu, opening a debug session in keil uVision&nbsp;<\/p>\n\n\n\n<p>Rotate the potentiometer and observe the adcValue in watch window<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"242\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-1024x242.png\" alt=\"\" class=\"wp-image-230\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-1024x242.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-300x71.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-768x181.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-1536x363.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-2048x484.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-1150x272.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-750x177.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-400x94.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2-250x59.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Potentiometer rotate Counter clockwise<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"242\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-1024x242.png\" alt=\"\" class=\"wp-image-231\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-1024x242.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-300x71.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-768x181.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-1536x363.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-2048x484.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-1150x272.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-750x177.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-400x94.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1-250x59.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Potentiometer rotate clockwise\u00a0<br><br><\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the part 1 of working with ADC and STM32 (link), we talked about the ADC and how it does work, configure it to read a potentiometer using single conversion method. In this guide we shall discuss the continuous ADC conversion polling mode (i.e. wait until end of conversion). In this guide, we will cover [&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-224","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\/224"}],"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=224"}],"version-history":[{"count":5,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}