Analog comparator is pretty simple peripheral, but it can give big benefits. Comparator module is included in all Mega series of AVR microcontrollers. Simply speaking Comparator allows to compare voltage values applied to two pins of microcontrollers. Result of comparison is a logical level which can be read by program(ACO- Analog Comparator Output bit). There is also ability to generate an interrupt which depends on comparator resulting level. And more- timer T1 can capture this level what allows measuring the signal length of analog signals.
In order to use pins as comparator inputs they have to be prepared and configured as input(DDRx=0) and pull-up resistors mus be disconnected (PORTx=0). In some models like Atmega48x/88x/168x, Atmega165x, Atmega325x,3250x,645x/6450x and Atmega640x/128x/1281x/2560x/2561x there is ability to disconnect digital buffers of AIN0 and AIN1pins that allows to use these pins only for analog signal input – this minimises overal power consumption.
Digital buffers can be disconnected by writing “1” where bits AIN0D and AIN1D are in register DIDR1:
Input signals in microcontrollers may be digital or analog. Digital signal may have two values “0” and “1” while analog any value in given interval. while AVR microcontroller may operate with only digital signals, analog signals have to be converted to digital. Mega series of AVR have built in ADC inside chip what makes this task much easier. Lets take Atmega8 as example. If Atmega8 DIP, then it has 6 ADC inputs, if package is TQFP-32 or MLF, then there are two additional ADC inputs. All ADC channels have 10 bit resolution. Bellow you see simplified Atmega8 ADC unit. Input signals come from pins PC0-PC5. Then enters multiplexer and according to ADMUX register signals is sent from one pin to ADC converter.