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.