{"id":1995,"date":"2023-09-23T15:07:53","date_gmt":"2023-09-23T15:07:53","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1995"},"modified":"2023-09-23T15:07:55","modified_gmt":"2023-09-23T15:07:55","slug":"getting-started-with-stm32l053-comparator","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1995","title":{"rendered":"Getting started with STM32L053: Comparator"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"513\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/M0-Corenew.jpg-1.webp\" alt=\"\" class=\"wp-image-1996\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/M0-Corenew.jpg-1.webp 513w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/M0-Corenew.jpg-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/M0-Corenew.jpg-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/M0-Corenew.jpg-1-400x400.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/M0-Corenew.jpg-1-250x250.webp 250w\" sizes=\"(max-width: 513px) 100vw, 513px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall see what is the comparator peripheral and how in general the comparator works. <\/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>What is a comparator.<\/li><li>Comparator peripheral in STM32L053.<\/li><li>Developing the driver.<\/li><li>Connection.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 What is Comparator:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>A&nbsp;<strong>comparator<\/strong>&nbsp;is an electronic circuit, which compares the two inputs that are applied to it and produces an output. The output value of the comparator indicates which of the inputs is greater or lesser. Please note that comparator falls under non-linear applications of ICs.<\/p>\n\n\n\n<p>An op-amp consists of two input terminals and hence an op-amp based comparator compares the two inputs that are applied to it and produces the result of comparison as the output. This chapter discusses about&nbsp;<strong>op-amp based comparators<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 Types of Comparators<\/h2>\n\n\n\n<p>Comparators are of two types :<strong>&nbsp;Inverting<\/strong>&nbsp;and&nbsp;<strong>Non-inverting<\/strong>. This section discusses about these two types in detail.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Inverting Comparator<\/h3>\n\n\n\n<p>An&nbsp;<strong>inverting comparator<\/strong>&nbsp;is an op-amp based comparator for which a reference voltage is applied to its non-inverting terminal and the input voltage is applied to its inverting terminal. This comparator is called as&nbsp;<strong>inverting<\/strong>&nbsp;comparator because the input voltage, which has to be compared is applied to the inverting terminal of op-amp.<\/p>\n\n\n\n<p>The&nbsp;<strong>circuit diagram<\/strong>&nbsp;of an inverting comparator is shown in the following figure.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/linear_integrated_circuits_applications\/images\/inverting_comparators.jpg\" alt=\"Inverting Comparators\" \/><\/figure><\/div>\n\n\n\n<p>The\u00a0<strong>operation<\/strong>\u00a0of an inverting comparator is very simple. It produces one of the two values,\u00a0+Vsat\u00a0and\u00a0\u2212Vsat\u00a0at the output based on the values of its input voltage\u00a0Vi\u00a0and the reference voltage\u00a0Vref. (<a rel=\"noreferrer noopener\" href=\"https:\/\/www.tutorialspoint.com\/linear_integrated_circuits_applications\/linear_integrated_circuits_applications_comparators.htm\" data-type=\"URL\" data-id=\"https:\/\/www.tutorialspoint.com\/linear_integrated_circuits_applications\/linear_integrated_circuits_applications_comparators.htm\" target=\"_blank\">source<\/a>).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Comparator Peripheral in STM32L053:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>STM32L0x3 devices embed two ultra-low-power comparators COMP1, and COMP2 that can be used either as standalone devices (all terminal are available on I\/Os) or combined with the timers.<\/p>\n\n\n\n<p>The comparators can be used for a variety of functions including:<\/p>\n\n\n\n<p>\u2022 Wakeup from low-power mode triggered by an analog signal,<\/p>\n\n\n\n<p>\u2022 Analog signal conditioning,<\/p>\n\n\n\n<p>\u2022 Cycle-by-cycle current control loop when combined with the DAC and a PWM output from a timer.<\/p>\n\n\n\n<p>COMP main features<\/p>\n\n\n\n<p>\u2022 COMP1 comparator with ultra low consumption<\/p>\n\n\n\n<p>\u2022 COMP2 comparator with rail-to-rail inputs, fast or slow mode<\/p>\n\n\n\n<p>\u2022 Each comparator has positive and configurable negative inputs used for flexible voltage selection:<\/p>\n\n\n\n<p>\u2013 I\/O pins<\/p>\n\n\n\n<p>\u2013 DAC<\/p>\n\n\n\n<p>\u2013 Internal reference voltage and three submultiple values (1\/4, 1\/2, 3\/4) provided by scaler (buffered voltage divider)<\/p>\n\n\n\n<p>\u2022 Programmable speed \/ consumption (COMP2 only)<\/p>\n\n\n\n<p>\u2022 The outputs can be redirected to an I\/O or to timer inputs for triggering:<\/p>\n\n\n\n<p>\u2013 Capture events<\/p>\n\n\n\n<p>\u2022 COMP1, and COMP2 can be combined in a window comparator. Each comparator has interrupt generation capability with wakeup from Sleep and Stop modes (through the EXTI controller)<\/p>\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=\"856\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-1024x856.png\" alt=\"\" class=\"wp-image-1997\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-1024x856.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-300x251.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-768x642.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-1150x961.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-750x627.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-400x334.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM-250x209.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.44.27-PM.png 1378w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Developing the Driver:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Before we start developing the driver, we need to find which bus the comparator is connected to. We can find this in the datasheet of STM32L053:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"856\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-1024x856.png\" alt=\"\" class=\"wp-image-1998\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-1024x856.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-300x251.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-768x642.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-1150x961.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-750x627.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-400x334.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM-250x209.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.47.08-PM.png 1378w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>From the block diagram, we can find that COMP1 and COMP2 are being clocked by APB2 bus.<\/p>\n\n\n\n<p>However, in the reference manual of STM32L052, comparator section, it mentioned that COMP1 and COMP2 are being clocked at the same time with SYSCNFG:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"183\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-1024x183.png\" alt=\"\" class=\"wp-image-1999\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-1024x183.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-300x54.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-768x137.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-1536x275.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-2048x367.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-1150x206.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-750x134.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-400x72.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.49.48-PM-250x45.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, the COMP1 has the ability to get the analog signals from PA0 and PA1.<\/p>\n\n\n\n<p>Hence, we shall first set PA0 and PA1 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;}\">\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;\n\n\t\/*Set PA0 and PA1 as analog Mode*\/\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0|GPIO_MODER_MODE1;<\/pre><\/div>\n\n\n\n<p>Since we need an output of the comparator, we need to find which pin is connected to COMP1.<\/p>\n\n\n\n<p>We can find that from Alternate function port A table in the datasheet:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"629\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-1024x629.png\" alt=\"\" class=\"wp-image-2000\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-1024x629.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-300x184.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-768x472.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-1536x943.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-2048x1258.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-1150x706.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-750x461.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-400x246.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.53.55-PM-250x154.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Since PA0 and PA1 are being used by the comparator, we shall use PA6 for the comparator output:<\/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\/*Set PA6 to alternate function*\/\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE6_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE6_0;\n\n\t\/*Set PA6 AF to AF7*\/\n\t#define AF07\t7\n\tGPIOA-&gt;AFR[0]|=(AF07&lt;&lt;GPIO_AFRL_AFSEL6_Pos);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Now we can configure the comparator:<\/p>\n\n\n\n<p>Enable clock access to SYSCNFG:<\/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\/*Enable clock access to COMP1 (Same as SYSCFG)*\/\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_SYSCFGEN;<\/pre><\/div>\n\n\n\n<p>Set the window pin to be PA1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"755\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-1024x755.png\" alt=\"\" class=\"wp-image-2001\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-1024x755.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-300x221.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-768x567.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-1536x1133.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-2048x1511.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-1150x848.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-750x553.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-400x295.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.57.09-PM-250x184.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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=\"117\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-1024x117.png\" alt=\"\" class=\"wp-image-2002\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-1024x117.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-300x34.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-768x88.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-1536x175.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-2048x233.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-1150x131.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-750x85.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-400x46.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.58.02-PM-250x28.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\t\/*Set the window pin to be PA1*\/\n\tCOMP1-&gt;CSR&amp;=~ COMP_CSR_COMP1WM;<\/pre><\/div>\n\n\n\n<p>Set the minus pin to be PA0:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"154\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-1024x154.png\" alt=\"\" class=\"wp-image-2003\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-1024x154.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-300x45.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-768x115.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-1536x230.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-2048x307.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-1150x173.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-750x113.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-400x60.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.59.25-PM-250x38.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\/*Set the minus pin to be PA0*\/\n\tCOMP1-&gt;CSR|=COMP_CSR_COMP1INNSEL_0;\n\tCOMP1-&gt;CSR&amp;=~COMP_CSR_COMP1INNSEL_1;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Finally enable comparator 1:<\/p>\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=\"126\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-1024x126.png\" alt=\"\" class=\"wp-image-2004\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-1024x126.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-300x37.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-768x95.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-1536x189.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-2048x252.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-1150x142.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-750x92.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-400x49.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-6.00.12-PM-250x31.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The while loop shall be empty.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Connection:<\/h2>\n\n\n\n<p>You need the following extra components:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1x LED.<\/li><li>1x 270Ohm resistor.<\/li><li>2x 1K Potentiometer.<\/li><li>Hookup wires.<\/li><\/ul>\n\n\n\n<p>The connection as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-806x1024.png\" alt=\"\" class=\"wp-image-2005\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-806x1024.png 806w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-236x300.png 236w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-768x975.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-1209x1536.png 1209w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-1150x1460.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-750x952.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-400x508.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM-250x317.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-23-at-5.35.52-PM.png 1378w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/figure>\n\n\n\n<p>Note: Vcc should be 3.3V.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Code:<\/h2>\n\n\n\n<p>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;stm32l0xx.h&quot;\n\nint main(void)\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;\n\n\t\/*Set PA0 and PA1 as analog Mode*\/\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0|GPIO_MODER_MODE1;\n\n\t\/*Set PA6 to alternate function*\/\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE6_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE6_0;\n\n\t\/*Set PA6 AF to AF7*\/\n\t#define AF07\t7\n\tGPIOA-&gt;AFR[0]|=(AF07&lt;&lt;GPIO_AFRL_AFSEL6_Pos);\n\n\n\t\/*Enable clock access to COMP1 (Same as SYSCFG)*\/\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_SYSCFGEN;\n\n\t\/*Set the window pin to be PA1*\/\n\tCOMP1-&gt;CSR&amp;=~ COMP_CSR_COMP1WM;\n\n\t\/*Set the minus pin to be PA0*\/\n\tCOMP1-&gt;CSR|=COMP_CSR_COMP1INNSEL_0;\n\tCOMP1-&gt;CSR&amp;=~COMP_CSR_COMP1INNSEL_1;\n\n\t\/*Enable Comparator 1*\/\n\tCOMP1-&gt;CSR|=COMP_CSR_COMP1EN;\n\n\n\n\n\twhile(1)\n\t{\n\t\t\/*Empty while 1 loop*\/\n\t}\n\n}\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Results:<\/h2>\n\n\n\n<p>When you upload the code, you should get this:<\/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=\"Comparator Peripheral in STM32L053\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/iZiX_2NpcTQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see what is the comparator peripheral and how in general the comparator works. In this guide, we shall cover the following: What is a comparator. Comparator peripheral in STM32L053. Developing the driver. Connection. Code. Results. 1.1 What is Comparator: A&nbsp;comparator&nbsp;is an electronic circuit, which compares the two inputs that are [&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-1995","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\/1995"}],"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=1995"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1995\/revisions"}],"predecessor-version":[{"id":2006,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1995\/revisions\/2006"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}