{"id":1437,"date":"2022-12-29T04:24:09","date_gmt":"2022-12-29T04:24:09","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1437"},"modified":"2022-12-29T04:29:22","modified_gmt":"2022-12-29T04:29:22","slug":"getting-started-with-stm32f103-adc-single-channel-continuous-conversion-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1437","title":{"rendered":"Getting Started with STM32F103: ADC Single Channel Continuous Conversion Mode"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-1024x768.jpeg\" alt=\"\" class=\"wp-image-1438\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-1024x768.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-300x225.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-768x576.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-1536x1152.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-2048x1536.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-1150x863.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-750x563.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-400x300.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_56488870-1-250x188.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1426\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1426\" target=\"_blank\">here<\/a>), we took a look how to configure ADC of STM32F103 to do one conversion only. <br>In this guide, we shall use continuous mode for the ADC.<\/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>Single vs continuous conversion mode.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Single Vs Continuous conversion mode:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 Single Conversion:<\/h3>\n\n\n\n<p>In Single conversion mode the ADC does one conversion. This mode is started either by setting the ADON bit in the ADC_CR2 register (for a regular channel only) or by external trigger (for a regular or injected channel), while the CONT bit is 0.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 Continuous Conversion:<\/h3>\n\n\n\n<p>In continuous conversion mode ADC starts another conversion as soon as it finishes one. This mode is started either by external trigger or by setting the ADON bit in the ADC_CR2 register, while the CONT bit is 1.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Code:<\/h2>\n\n\n\n<p>From the previous guide, we shall take the code and modify it.<\/p>\n\n\n\n<p>In order to enable continuous mode, we need to set CON bit in CR2 to 1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"382\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-1024x382.png\" alt=\"\" class=\"wp-image-1440\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-1024x382.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-300x112.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-768x286.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-1536x573.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-2048x763.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-1150x429.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-750x280.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-400x149.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-29-at-7.16.49-AM-250x93.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;}\">\t\/*Enable Continuous mode *\/\n\n\tADC1-&gt;CR2|=ADC_CR2_CONT;<\/pre><\/div>\n\n\n\n<p>And move the launching adc to main section 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;}\">\t\n\t\/*Power up the adc*\/\n\tADC1-&gt;CR2|=ADC_CR2_ADON;\n\n\t\/*Wait a little bit*\/\n\t\n\tfor (int i=0;i&lt;1000;i++);\n\t\/*Launch the ADC*\/\n\tADC1-&gt;CR2|=ADC_CR2_ADON;\n\n\t\/*Launch the ADC conversion*\/\n\tADC1-&gt;CR2|=ADC_CR2_SWSTART;<\/pre><\/div>\n\n\n\n<p>Within while 1 loop:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Wait for EOC of conversion.<\/li><li>Read the DR register.<\/li><\/ul>\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;}\">while(1)\n\t{\n\t\t\/*wait for EOC*\/\n\t\twhile(!(ADC1-&gt;SR &amp;ADC_SR_EOC));\n\t\tadc_data=ADC1-&gt;DR;\n\n\t}<\/pre><\/div>\n\n\n\n<p>Hence, the entire code 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;}\">#include &quot;stm32f1xx.h&quot;\n\n\n\nuint32_t adc_data;\n\nint main()\n{\n\t\/*Enable clock access to GPIOA*\/\n\t\/\/enable clock access to GPIOA\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_IOPAEN;\n\t\/*Set PA0 to analog Mode*\/\n\tGPIOA-&gt;CRL&amp;=~GPIO_CRL_CNF0;\n\tGPIOA-&gt;CRL&amp;=~GPIO_CRL_MODE0;\n\n\t\/*Enable clock access to ADC1*\/\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN;\n\n\t\/*Set the trigger to be software mode*\/\n\tADC1-&gt;CR2 |= (7UL &lt;&lt; ADC_CR2_EXTSEL_Pos);\n\n\t\/*Enable Continuous mode *\/\n\n\tADC1-&gt;CR2|=ADC_CR2_CONT;\n\n\t\/*Power up the adc*\/\n\tADC1-&gt;CR2|=ADC_CR2_ADON;\n\n\t\/*Wait a little bit*\/\n\n\tfor (int i=0;i&lt;1000;i++);\n\t\/*Launch the ADC*\/\n\tADC1-&gt;CR2|=ADC_CR2_ADON;\n\n\t\/*Launch the ADC conversion*\/\n\tADC1-&gt;CR2|=ADC_CR2_SWSTART;\n\n\twhile(1)\n\t{\n\t\t\/*wait for EOC*\/\n\t\twhile(!(ADC1-&gt;SR &amp;ADC_SR_EOC));\n\t\tadc_data=ADC1-&gt;DR;\n\n\t}\n\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p>Open new debug session and put adc_data into live expression and you should get the results.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Single Channel Single Conversion ADC on STM32F103\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/cHBKTEXCRYs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to configure ADC of STM32F103 to do one conversion only. In this guide, we shall use continuous mode for the ADC. In this guide, we shall cover the following: Single vs continuous conversion mode. Code. Results. 1. Single Vs Continuous conversion mode: 2.1 Single Conversion: [&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-1437","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\/1437"}],"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=1437"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":1442,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1437\/revisions\/1442"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}