Summary of How to use inbuilt analog comparator of AVR microcontroller
This article details designing a proximity sensor using the ATmega16 microcontroller's in-built analog comparator. It explains how the device compares input voltages on AIN0 and AIN1 (or ADC channels) to generate digital output signals, reducing external component needs. The text outlines the configuration of three specific registers: SFIOR, ACSR, and their respective control bits like ACME, ACD, and ACBG for setting up the comparison logic and enabling features like bandgap voltage selection or interrupt triggers.
Parts used in the Proximity Sensor:
ATmega16 Microcontroller
Analog Comparator (In-built)
Ain0 Pin (PB2)
Ain1 Pin (PB3)
SFIOR Register
ACSR Register
ADC Channels (PA0-PA7)
Bandgap Voltage Source
Analog comparator is a device which compares two input voltages and generates output accordingly. The article on IR sensor explains the use of comparator in sensor designing. Comparators form an integral part of circuit designing in majority of the applications. AVR microcontrollers have in-built analog comparator. Using the in-built analog comparator of AVR, the controller can be used to compare the signal and process the signal as well. This reduces the external comparator components on our circuits. In this article proximity sensor is designed using in-built analog comparator of ATmega16.
The analog comparator needs two inputs positive and negative. The positive input is given on AIN0 (PB2) pin of controller. In ATmega16 nine pins are available to connect negative input of comparator. This means microcontroller can compare maximum of nine analog signals with one positive input voltage. Although, signals are not compared simultaneously but the time difference between two consecutive comparisons is of the order of microseconds which is quite low to identify. The negative input of comparator is applied on pin AIN1 (PB3). The negative input can also be applied on ADC channels (PA0-PA7) but the in-built ADC system must be turned off. The output of comparison affects the ACO bit of ACSR of analog comparator system. When the voltage of positive pin (AIN0) is greater than negative input voltage, the ACO bit of register ACSR is set by hardware, otherwise ACO remains low.
Analog comparator registers:
There are three registers which take part in configuration of the analog comparator.
1. SFIOR (Special Function IO Register) :
ACME (Analog Comparator Multiplexer Enable) – When this bit is zero the negative analog input is applied only on AIN1 pin. When this bit is set and ADC system is disabled, the negative analog input can be given at ADC channel pins (ADC0-ADC7) which can be selected by ADMUX register.
2. ACSR (Analog Comparator Control and Status Register):
ACD (Analog Comparator Disable) – This bit disables the analog comparator when set to one.
ACBG (Analog Comparator Bandgap Select) – When this bit is set to one, a fixed internal bandgap voltage VBG(1.15 V < VBG < 1.4 V) is selected as positive input of comparator. When this bit is set to zero, voltage at pin AIN0 will be considered as positive input voltage.
ACO (Analog Comparator Output) – When voltage at pin AIN0 is higher than the negative input pin, this bit is set by hardware. The analog comparator needs one or two clock cycle to synchronize with ACO bit.
The following bits (ACI, ACIE, ACIC and ASIC) are used for further application (for eg. ADC triggering, etc.) which is beyond the scope of this article. (You can skip for the time being) The brief explanation for these bits is given below.
ACI (Analog Comparator Interrupt Flag) – This bit is set by hardware when a comparator output event triggers the interrupt mode which is defined ACIS1 and ACIS0 bits.
ACIE (Analog Comparator Interrupt Enable) – This bit is set in order to activate analog comparator interrupt.
ACIC (Analog Comparator Input Capture Enable) – This bit is used to enable the input capture function in Timer/Counter1. To enable the input capture along with ACIC bit the TICIE1 bit in TIMSK register is set to one. When this bit is set to zero, the analog comparator is disconnected with Timer system.
ASIC [1:0] (Analog Comparator Interrupt Mode Select) – This bit is used to select interrupt modes.
Quick Solutions to Questions related to Proximity Sensor:
How does the ATmega16 compare analog signals? The controller uses its in-built analog comparator to compare signals on the positive AIN0 pin against negative inputs on AIN1 or ADC channels.
Can the negative input be connected to ADC pins? Yes, but the in-built ADC system must be turned off before applying the negative input to ADC channel pins.
What happens when the positive pin voltage is higher than the negative input? The ACO bit of the ACSR register is set by hardware to indicate the positive voltage condition.
Which register enables connecting the negative input to ADC channels? The SFIOR register controls this via the ACME bit when set to one.
How can a fixed internal voltage be used as the positive input? Setting the ACBG bit in the ACSR register selects the internal bandgap voltage instead of the AIN0 pin voltage.
Does the microcontroller compare multiple signals simultaneously? No, signals are not compared simultaneously, though the time difference between comparisons is very low.
What is the function of the ACIE bit? The ACIE bit is set to activate the analog comparator interrupt functionality.
I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.ACCEPTPrivacy Policy
Manage consent
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.