Developed By:
Akshay Daga
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) :
Fig. 2: Bit Configuration of SFIOR in Analog comparator of AVR Microcontroller
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):
Fig. 3: Bit Configuration of ACSR in Analog comparator of AVR Microcontroller
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):
Fig. 4: Bit Configuration of ADMUX in Analog comparator of AVR Microcontroller
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:
Fig. 5: Bit Setting to select ADC channel as negative input pins in AVR's Anaog Comparator
NOTE: x= don’t care.
Objective: To design three proximity sensors using IR pair and the in-built analog comparator of Atmega16.
Circuit description:
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.
Fig. 6: Circuit Diagram of Proximity sensor designed using in-built analog comparator of ATmega16.
Programming steps:
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).
Circuit:
Code:
//Program to use inbuilt analog comparator of AVR microcontroller
#include<avr/io.h>
#include<util/delay.h>
int main()
{
DDRD=0x07; // set PD0, PD1 and PD2 as output pin
ACSR=0x00; //to set ACO bit as zero.
ADCSRA&=~(1<<ADEN); // to disable ADC
while(1)
{
SFIOR|=(1<<ACME); // bit is set to select the ADC channel
ADMUX=0x00; // ADC0 channel is selected
_delay_us(1); // delay for synchronization
if(bit_is_set(ACSR,ACO)) // if bit is set to 1
PORTD|=(1<<PD0); // send one to PD0 bit
else // if bit is set to zero
PORTD&=~(1<<PD0);
{
DDRD=0x07; // set PD0, PD1 and PD2 as output pin
ACSR=0x00; //to set ACO bit as zero.
ADCSRA&=~(1<<ADEN); // to disable ADC
while(1)
{
SFIOR|=(1<<ACME); // bit is set to select the ADC channel
ADMUX=0x00; // ADC0 channel is selected
_delay_us(1); // delay for synchronization
if(bit_is_set(ACSR,ACO)) // if bit is set to 1
PORTD|=(1<<PD0); // send one to PD0 bit
else // if bit is set to zero
PORTD&=~(1<<PD0);
SFIOR|=(1<<ACME);
ADMUX=0x01; // ADC1 channel is selected
_delay_us(1);
if(bit_is_set(ACSR,ACO))
PORTD|=(1<<PD1);
else
PORTD&=~(1<<PD1);
ADMUX=0x01; // ADC1 channel is selected
_delay_us(1);
if(bit_is_set(ACSR,ACO))
PORTD|=(1<<PD1);
else
PORTD&=~(1<<PD1);
SFIOR&=~(1<<ACME); // to select AIN1 pin as negative input
_delay_us(1);
if(bit_is_set(ACSR,ACO))
PORTD|=(1<<PD2);
else
PORTD&=~(1<<PD2);
}
}
_delay_us(1);
if(bit_is_set(ACSR,ACO))
PORTD|=(1<<PD2);
else
PORTD&=~(1<<PD2);
}
}
Không có nhận xét nào:
Đăng nhận xét