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.
3. ADMUX (ADC Multiplexer Selection Register):
When ADC is turned off (ADEN bit in ADCSRA register is made zero) and the ACME bit in SFIOR register is set, the ADC channel (ADC0-ADC7) can be selected as negative input pin by configuring the MUX [2:0] bits. The following table shows the bits setting to select ADC channel as negative input pins:
NOTE: x= don’t care.
Objective: To design three proximity sensors using IR pair and the in-built analog comparator of Atmega16.
As shown in circuit diagram the positive voltage is applied to AIN0 (PB2) pin by using a variable resistor. The analog output of the three IR Tx and Rx pair (as shown in below diagram), are connected at ADC0 (PA0), ADC1 (PA1) and AIN1 (PB3) pins and compared output of the sensors are taken on pins PD0, PD1 and PD2 respectively.
1. Clear the ACO bit.
2. Turn off the ADC (clear ADEN bit in ADCSRA register).3. Send zero to ACME bit in SFIOR register if AIN1 pin is used as negative input of comparator.
4. Send one to ACME bit in SFIOR register if ADC channel is used as negative input of comparator.
5. Set the MUX [2:0] in ADMUX register to select the channel as shown in above table.
6. Give a time delay of 1or 2 microseconds to synchronize analog comparator with ACO bit.
7. Detect the ACO bit output and send the output on corresponding output pin (PD0, PD1, or PD2 for this experiment).