{"id":10522,"date":"2015-12-05T05:52:39","date_gmt":"2015-12-05T10:52:39","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=10522"},"modified":"2016-12-18T19:43:31","modified_gmt":"2016-12-19T00:43:31","slug":"xmega-analog-comparator","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/xmega-analog-comparator\/","title":{"rendered":"XMega Analog Comparator"},"content":{"rendered":"<p>Previously we dealt with the XMega <a href=\"http:\/\/embedded-lab.com\/blog\/?p=9488\" target=\"_blank\"><b><i>Analog-to-Digital Converter (ADC)<\/i><\/b><\/a> block. We know that we can use the ADC to measure voltages and take decisions based on voltage values\/levels but sometimes it is enough to detect voltage levels and not to measure the exact voltage values. In such occasions where we just need to check voltage levels relative to a reference or threshold value, we need an <b><i>Analog Comparator (AC)<\/i><\/b>. An analog comparator can be used to compare two voltage levels and based on that it can be used to generate a logic output (<i>0 or 1<\/i>) to indicate which of the two levels is higher or lower than the other. That\u2019s all and there isn\u2019t much about analog comparators. The XMega family of micros come loaded with high performance dual analog comparator modules. However so far we saw that between the traditional 8-bit micros and the XMega micros, the major difference apart from programming is the overall nifty enhancements in all common hardware blocks. When it comes to the analog comparators of the XMega micros, the same is true. In this issue we will explore the XMega analog comparator block.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Block-Diagram.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10540 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Block-Diagram-580x327.png\" alt=\"Block Diagram\" width=\"580\" height=\"327\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Block-Diagram-580x327.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Block-Diagram.png 782w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><!--more--><\/p>\n<h4>A Brief Overview<\/h4>\n<p>Generally a comparator is a device that compares two quantities. Analog comparators compare voltage \u2013 an analog quantity. An analog comparator is essentially an op-amp with infinite gain. It has one output and two inputs. The output is switched to <i>VDD<\/i> or ground (0V) depending on the relative voltage levels in the two input pins. Just as with an op-amp the two inputs are labeled as inverting input (<i>INV<\/i>) and non-inverting input (<i>NINV<\/i>). When the non-inverting input voltage is higher than that of the inverting input, the output switches to <i>VDD<\/i> and when it is the other way, the output switches to ground. Thus we get two logic states.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Formula.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-10544 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Formula.png\" alt=\"Formula\" width=\"184\" height=\"60\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>In the XMega micros, the basic comparator that one may find in a typical 8-bit micro is enhanced a bit. The basic operations are same but additionally a few features have been included. For instance we can add hysteresis and can change operational speed of these comparators by altering propagation delays. Hysteresis is useful feedback mechanism for fast varying input signals (especially ringing\/oscillating signals) that cause high rates of output swings. By adding hysteresis to a comparator we can effectively reduce the frequent oscillation of the output.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Comparator-States.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10536 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Comparator-States-580x223.gif\" alt=\"Comparator States\" width=\"580\" height=\"223\" \/><\/a><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Hysteresis.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-10538 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Hysteresis.gif\" alt=\"Hysteresis\" width=\"256\" height=\"250\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>In the ATXMega micros, two levels of hysteresis can be programmed \u2013 small (<i>30mV<\/i>) and large (<i>60mV<\/i>). The datasheet documents of the XMega devices state these values and much more. As the name suggests High Speed mode yields in faster operation while the default Low Power mode is slower than the former but it is much more energy efficient. Shown below is the table for electrical specs of the ATXMega32A4U\u2019s analog comparator module. Check it out:<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Electrical-Properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10542 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Electrical-Properties-580x306.png\" alt=\"Electrical Properties\" width=\"580\" height=\"306\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Electrical-Properties-580x306.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Electrical-Properties.png 826w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Unlike old school micros, the input pins to the comparators are not tied to a specific set of I\/O pins of an I\/O port. We can literally use any pin of a port that is related to the comparator block. I have used an ATXMega32A4U micro for this issue. This micro has a pair of analog comparators under a single comparator block called <b><i>Analog Comparator A (ACA)<\/i><\/b>. Larger XMega devices have more of such comparator blocks. Each block has a pair of comparator modules. Each comparator in a block has a unique designation like <b><i>ACA.AC0<\/i><\/b>, meaning the first analog comparator of block A comparator pair attached with PORTA. I\/Os related to ACA are available to PORTA pins only. Watch the blue boxes in the image below:<\/p>\n<p style=\"text-align: center\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/XMega-A4-Pin-Map.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-10549 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/XMega-A4-Pin-Map-580x575.png\" alt=\"XMega-A4-Pin-Map\" width=\"406\" height=\"403\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/XMega-A4-Pin-Map-580x575.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/XMega-A4-Pin-Map-150x150.png 150w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/XMega-A4-Pin-Map-90x90.png 90w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/XMega-A4-Pin-Map.png 684w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><\/p>\n<p>We can also set a pair of comparators as a window comparator. Window comparators are also known as limit comparators because of their limit sensing ability. Such comparators determine when the input voltage to it is within a preselected range. There are two voltage limit values (upper limit and lower limit) or references for an input signal that define the boundaries of the voltage window. An input can be within, outside, above or below the predefined window and so there are four states.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Window-Comparator.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10548 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Window-Comparator-580x310.png\" alt=\"Window Comparator\" width=\"580\" height=\"310\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Window-Comparator-580x310.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Window-Comparator.png 667w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Window comparators have several applications. One good application is a battery charger. A window comparator will always monitor the voltage level of a battery with respect to references and based on the four aforementioned output conditions, charging can be started or stopped automatically without needing much coding and attention.<\/p>\n<p>Three internal reference sources can be tied with the comparator pins. These are as follows:<\/p>\n<ul>\n<li>Bandgap voltage source or precise 1.0V internal source<\/li>\n<li>DAC output<\/li>\n<li>64-step scalable VDD<\/li>\n<\/ul>\n<p>Having these sources frees us from adding and implementing additional hardware for reference sources.<\/p>\n<p>Comparator events can also generate interrupt requests as well as trigger the XMega event system. Using these we can use can get critical statuses quickly without much intervention of the CPU.<\/p>\n<h4>Registers<\/h4>\n<p>The table below shows the registers associated with analog comparator block:<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Registers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10545 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Registers-580x214.png\" alt=\"Registers\" width=\"580\" height=\"214\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Registers-580x214.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Registers-1024x379.png 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Registers.png 1085w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The registers and individual bits speak of themselves. Multiplexer control registers, <b><i>ACnMUXCTRL<\/i><\/b> are what we use code to select input pins. Analog comparator control registers, <b><i>ACnCTRL<\/i><\/b> are coded to setup interrupt levels and modes of interrupt. ACnCTRL registers are also programmed to select hysteresis and operating speed\/power. A status register keeps track of comparator-related events. This register comes to no use if we take comparator outputs directly from port pins or if its bit states are not needed to be read. There is also a dedicated register for window mode operation. It is used to configure everything necessary for window comparator mode operation. <b><i>CTRLA<\/i><\/b> register enables physical comparator output pins. With the <b><i>CTRLB<\/i><\/b> register we can program the scalable VDD reference. I\u2019ll discuss more on this register in a later part of this doc. Lastly regarding the last two registers which are related to constant current source, I haven\u2019t been able to test the constant current source capability of the XMega micros as no such registers exists for the ATXMega32A4U.<\/p>\n<h4>Coding<\/h4>\n<p>The code examples I showed in this issue are very simple to understand. Except the code example for the window comparator demo, the outputs from the comparators are directly taken from their respective I\/O port pins (PORTA.6 and PORTA.7). LEDs are used to visualize output states. To keep things easy I didn\u2019t use any interrupt in any of my codes. However interrupts can be easily implemented with these examples with minor changes. 8MHz clock is derived from the internal 32MHz oscillator after proper scaling and with this clock the XMega runs.<\/p>\n<p>&nbsp;<\/p>\n<h6><em>Basic Single Analog Comparator<\/em><\/h6>\n<p>The basic comparator is well explained in the beginning of this issue. I\u2019m not going to repeat the same old literature. GPIO pins <i>PORTA.0<\/i> and <i>PORTA.1<\/i> are the positive pin and the negative pin to the analog comparator <i>ACA.AC0 <\/i>respectively. GPIO pin <i>PORTA.7<\/i> is the output of <i>ACA.AC0<\/i> and this pin is connected to a LED. Check the simplified connection diagram below:<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Basic-Comparator-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10539 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Basic-Comparator-Schematic-580x405.png\" alt=\"Basic Comparator Schematic\" width=\"580\" height=\"405\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Basic-Comparator-Schematic-580x405.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Basic-Comparator-Schematic.png 763w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>In the practical demo, I tied the negative pin to a 1.9V source and varied the voltage on the positive pin. Check the code example, photos and the videos:<\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><i>#include \"CLOCK.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"IO.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"AC.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void main() <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(1)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 };<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_clock();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 asm cli;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSBCDIV0_bm))) | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_DIR = 0x80;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_PIN4CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_INTCTRL = ((PORTA_INTCTRL &amp; (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_WINCTRL = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0MUXCTRL = (AC_MUXPOS_PIN0_gc | AC_MUXNEG_PIN1_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0CTRL = (AC_INTMODE_BOTHEDGES_gc | AC_INTLVL_OFF_gc | (0 &lt;&lt; AC_HSMODE_bp)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | AC_HYSMODE_NO_gc | (1 &lt;&lt; AC_ENABLE_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1MUXCTRL = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1CTRL = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_CTRLA = ((0 &lt;&lt; AC_AC1OUT_bp) | (1 &lt;&lt; AC_AC0OUT_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}\u00a0 \u00a0 \u00a0<\/i><\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214518.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10526 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214518-580x464.jpg\" alt=\"20150704_214518\" width=\"580\" height=\"464\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214518-580x464.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214518.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214537.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10527 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214537-580x485.jpg\" alt=\"20150704_214537\" width=\"580\" height=\"485\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214537-580x485.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_214537.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Video link: <a href=\"https:\/\/www.youtube.com\/watch?v=jS9-tasyTGI\">https:\/\/www.youtube.com\/watch?v=jS9-tasyTGI<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h6><em>Dual Independent Analog Comparators<\/em><\/h6>\n<p>This example is basically the same as the previous one except this time both comparators of the ACA block are used and both are operated in high speed mode. ACA.AC0\u2019s and ACA.AC1\u2019s negative inputs are fed to the 64-step scalable VDD reference source and the bandgap voltage (precise 1.0V) reference source respectively. The positive inputs connected to PORTA.0 and PORTA.1 pins are compared with these internal sources. Since the comparators do not share any common input, they are independent of each other.<\/p>\n<p>In my discussion with the registers related to the analog comparator, I skipped the <b><i>ACA_CTRLB <\/i><\/b>register. Now we will see its use. We can program this register to get desired voltage from the 64-step scalable VDD reference source according to the formula:<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Formula-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-10543 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Formula-2.png\" alt=\"Formula 2\" width=\"269\" height=\"81\" \/><\/a><\/p>\n<p>In my example I coded the 64-step scalable VDD reference source to provide an output of 1.65V, which is roughly the half of the VDD. In the code example, you&#8217;ll notice the line:<\/p>\n<p align=\"center\"><em><span style=\"color: #ff0000\"><b>ACA_CTRLB = 0x1F;<\/b>\u00a0<\/span><\/em><\/p>\n<p>Thus the scale factor is 31 (0x1F) and the scaled VDD is half of the VCC. Since VCC\/VDD is 3.3V, the scaled voltage is 1.65V.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Dual-Comparator-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10541 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Dual-Comparator-Schematic-580x375.png\" alt=\"Dual Comparator Schematic\" width=\"580\" height=\"375\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Dual-Comparator-Schematic-580x375.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Dual-Comparator-Schematic.png 879w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><i>#include \"CLOCK.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"IO.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"AC.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void main()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(1);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_clock();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 asm cli;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSBCDIV0_bm))) | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_DIR = 0xC0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_PIN4CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_INTCTRL = ((PORTA_INTCTRL &amp; (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 unsigned char regval = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 regval = (AC_INTMODE_BOTHEDGES_gc | AC_INTLVL_OFF_gc | (1 &lt;&lt; AC_HSMODE_bp)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | AC_HYSMODE_NO_gc | (1 &lt;&lt; AC_ENABLE_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0MUXCTRL = (AC_MUXPOS_PIN0_gc | AC_MUXNEG_SCALER_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0CTRL = regval;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1MUXCTRL = (AC_MUXPOS_PIN1_gc | AC_MUXNEG_BANDGAP_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1CTRL = regval;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_WINCTRL = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_CTRLA = ((1 &lt;&lt; AC_AC1OUT_bp) | (1 &lt;&lt; AC_AC0OUT_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_CTRLB = 0x1F;<\/i><\/span><\/pre>\n<pre><i><span style=\"color: #0000ff\">}\u00a0 \u00a0<\/span> \u00a0<\/i><\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215803.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10530 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215803-580x464.jpg\" alt=\"20150704_215803\" width=\"580\" height=\"464\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215803-580x464.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215803.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215723.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10529 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215723-580x441.jpg\" alt=\"20150704_215723\" width=\"580\" height=\"441\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215723-580x441.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215723.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215709.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10528 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215709-580x465.jpg\" alt=\"20150704_215709\" width=\"580\" height=\"465\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215709-580x465.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_215709.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>Video link: <a href=\"https:\/\/www.youtube.com\/watch?v=emOd4qec0hg\">https:\/\/www.youtube.com\/watch?v=emOd4qec0hg<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h6><em>Window Mode Comparator<\/em><\/h6>\n<p>The window mode comparator\u2019s setups are similar to those of the previous example but this time there\u2019s only one positive input instead of two and so both comparators share the same positive input. Just as in previous example, I used the same internal voltage reference sources with the negative inputs. The internal diagram should look like this:<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Wincomp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-10547 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Wincomp.png\" alt=\"Wincomp\" width=\"412\" height=\"289\" \/><\/a><\/p>\n<p>Check the schematic:<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Wincomp-Schema.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10546 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Wincomp-Schema-580x462.png\" alt=\"Wincomp Schema\" width=\"580\" height=\"462\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Wincomp-Schema-580x462.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Wincomp-Schema.png 711w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>From the above schematic and block diagram we can realize that:<\/p>\n<ul>\n<li>Upper limit = 2.68V<\/li>\n<li>Lower limit = 1.00V<\/li>\n<li>The window is in between 1.00V to 2.68V.<\/li>\n<li>L1 = Red LED, L2 = Yellow LED and L3 = Green LED<\/li>\n<\/ul>\n<p>When the input (PORTA.0 pin) is less than 1.00V, L1 lights only, indicating below window condition. Similarly when the input is greater than 2.68V, L3 lights up, indicating above window condition. When the input is between these two extremes, L2 lights alone, indicating inside window condition. During transitions between levels, two LEDs may appear to light up simultaneously and well it is just because of intermediate states. For more understanding check the video out.<\/p>\n<pre><span style=\"color: #0000ff\"><i>#include \"CLOCK.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"IO.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"AC.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void main()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 unsigned char result = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(1)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 result = (ACA_STATUS &amp; 0xC0);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(result == AC_WSTATE_ABOVE_gc)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORTD_OUT = 0x01;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(result == AC_WSTATE_INSIDE_gc)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORTD_OUT = 0x02;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if(result == AC_WSTATE_BELOW_gc)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORTD_OUT = 0x04;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 };\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_clock();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 asm cli;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSBCDIV0_bm))) | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_DIR = 0xC0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_PIN4CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_INTCTRL = ((PORTA_INTCTRL &amp; (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_DIR = 0x0F;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_PIN6CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_INTCTRL = ((PORTD_INTCTRL &amp; (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 unsigned char reg_val = 0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 reg_val = (AC_INTMODE_BOTHEDGES_gc | AC_INTLVL_OFF_gc | (0 &lt;&lt; AC_HSMODE_bp)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | AC_HYSMODE_LARGE_gc | (1 &lt;&lt; AC_ENABLE_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0MUXCTRL = (AC_MUXPOS_PIN0_gc | AC_MUXNEG_SCALER_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0CTRL = reg_val;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1MUXCTRL = (AC_MUXPOS_PIN0_gc | AC_MUXNEG_BANDGAP_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1CTRL = reg_val;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_WINCTRL = ((1 &lt;&lt; AC_WEN_bp) | AC_WINTMODE_OUTSIDE_gc | AC_WINTLVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_CTRLA = (1 &lt;&lt; AC_AC1OUT_bp) | (1 &lt;&lt; AC_AC0OUT_bp);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_CTRLB = 0x33;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/i><\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212739.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10525 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212739-580x379.jpg\" alt=\"20150704_212739\" width=\"580\" height=\"379\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212739-580x379.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212739.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212713.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10524 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212713-580x355.jpg\" alt=\"20150704_212713\" width=\"580\" height=\"355\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212713-580x355.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212713.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212644.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10523 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212644-580x347.jpg\" alt=\"20150704_212644\" width=\"580\" height=\"347\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212644-580x347.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_212644.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Video link: <a href=\"https:\/\/www.youtube.com\/watch?v=eQB8MUIlMdU\">https:\/\/www.youtube.com\/watch?v=eQB8MUIlMdU<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h6><em>Comparator Hysteresis Demo<\/em><\/h6>\n<p>Finally in this example, we will observe the effect of varying and applying hysteresis to a comparator. For this I configured both comparators with same input pins \u2013 PORTA.1 (+) and internal scaled VDD (-). Both comparators have the same settings. However one comparator has a large (60mV) hysteresis while the other has a small hysteresis (30mV). When the input is slowly varied around the point of tripping, it will be seen that the one with larger hysteresis will react less to the change than the other. This effect is specifically useful in noisy environments. Check the video clip carefully for better understanding.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Hysteresis-Schematic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10537 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Hysteresis-Schematic-580x432.png\" alt=\"Hysteresis Schematic\" width=\"580\" height=\"432\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Hysteresis-Schematic-580x432.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/Hysteresis-Schematic.png 785w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff\"><i>#include \"CLOCK.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"IO.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>#include \"AC.h\"<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void main()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 while(1)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 {<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 };<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_clock();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_GPIO();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0 setup_AC();<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_clock()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 asm cli;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32KEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32KRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL |= OSC_RC32MEN_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_PSCTRL = ((CLK_PSCTRL &amp; (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | CLK_PSBCDIV0_bm))) | CLK_PSADIV_1_gc | CLK_PSBCDIV_2_2_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_DFLLCTRL = ((OSC_DFLLCTRL &amp; (~(OSC_RC32MCREF_gm | OSC_RC2MCREF_bm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OSC_RC32MCREF_RC32K_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 DFLLRC32M_CTRL |= DFLL_ENABLE_bm;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 while(!(OSC_STATUS &amp; OSC_RC32MRDY_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CPU_CCP = CCP_IOREG_gc;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 CLK_CTRL = ((CLK_CTRL &amp; (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_RC32M_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 OSC_CTRL &amp;= (~(OSC_RC2MEN_bm | OSC_XOSCEN_bm | OSC_PLLEN_bm));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_CLKEVOUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_GPIO()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_DIR = 0xC0;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_PIN4CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTA_INTCTRL = ((PORTA_INTCTRL &amp; (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_OUT = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_DIR = 0x0F;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTCFG_MPCMASK = 0xFF;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_PIN6CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 PORTD_INTCTRL = ((PORTD_INTCTRL &amp; (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>void setup_AC()<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>{<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 unsigned char reg_val;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 reg_val = (AC_MUXPOS_PIN1_gc | AC_MUXNEG_SCALER_gc);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0MUXCTRL = reg_val;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC0CTRL = (AC_INTMODE_BOTHEDGES_gc | AC_INTLVL_OFF_gc | (1 &lt;&lt; AC_HSMODE_bp)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | AC_HYSMODE_LARGE_gc | (1 &lt;&lt; AC_ENABLE_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1MUXCTRL = reg_val;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_AC1CTRL = (AC_INTMODE_BOTHEDGES_gc | AC_INTLVL_OFF_gc | (1 &lt;&lt; AC_HSMODE_bp)<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | AC_HYSMODE_SMALL_gc | (1 &lt;&lt; AC_ENABLE_bp));<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_CTRLA = (1 &lt;&lt; AC_AC1OUT_bp) | (1 &lt;&lt; AC_AC0OUT_bp);<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_CTRLB = 0x1F;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 <\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>\u00a0\u00a0\u00a0 ACA_WINCTRL = 0x00;<\/i><\/span><\/pre>\n<pre><span style=\"color: #0000ff\"><i>}<\/i><\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221447.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10534 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221447-580x306.jpg\" alt=\"20150704_221447\" width=\"580\" height=\"306\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221447-580x306.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221447.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221148.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10533 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221148-580x304.jpg\" alt=\"20150704_221148\" width=\"580\" height=\"304\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221148-580x304.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221148.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a> <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221031.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-10532 aligncenter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221031-580x313.jpg\" alt=\"20150704_221031\" width=\"580\" height=\"313\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221031-580x313.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/20150704_221031.jpg 590w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Video link: <a href=\"https:\/\/www.youtube.com\/watch?v=iLZgnKrzzv8\">https:\/\/www.youtube.com\/watch?v=iLZgnKrzzv8<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>Files:<br \/>\n<a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2015\/07\/XMega-Comparator.rar\">XMega Comparator<\/a><\/p>\n<p>References:<\/p>\n<ul>\n<li>XMega AU Manual and Datasheet<\/li>\n<li>Mikroelektronika\u2019s Website (www. Mikroe.com).<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Happy coding.<\/p>\n<p><i>Author: Shawon M. Shahryiar <\/i><\/p>\n<p><a href=\"https:\/\/www.facebook.com\/groups\/microarena\/\"><i>https:\/\/www.facebook.com\/groups\/microarena<\/i><\/a><i>\u00a0\u00a0<\/i><\/p>\n<p><a href=\"https:\/\/www.facebook.com\/MicroArena\"><i>https:\/\/www.facebook.com\/MicroArena<\/i><\/a><i>\u00a0<\/i><\/p>\n<p><i>+8801970046495 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/i><\/p>\n<p><i>04.07.2015<\/i><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously we dealt with the XMega Analog-to-Digital Converter (ADC) block. We know that we can use the ADC to measure voltages and take decisions based on voltage values\/levels but sometimes it is enough to detect voltage levels and not to measure the exact voltage values. In such occasions where we just need to check voltage levels relative to a reference or threshold value, we need an Analog Comparator (AC). An analog comparator can be used to compare two voltage levels and based on that it can be used to generate a logic output (0 or 1) to indicate which of<\/p>\n","protected":false},"author":5,"featured_media":10523,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1466,792],"tags":[415,1217,644,807,800,11,423,312,238],"class_list":["post-10522","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-xmega","tag-advanced-microcontrollers","tag-analog-comparator","tag-atmel","tag-atxmega","tag-atxmega32a4u","tag-avr","tag-avr-xmega","tag-mikroc-for-avr","tag-mikroelektronika"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/10522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=10522"}],"version-history":[{"count":8,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/10522\/revisions"}],"predecessor-version":[{"id":12869,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/10522\/revisions\/12869"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/10523"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=10522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=10522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=10522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}