AVR analog comparator review

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:

Comparator is controlled by one byte ACSR.

Bit No.





0-ON, 1-OFF comparator



0 – AIN0 to the positive input of comparator; 1- reference voltage to positive comparator input



Result of comparing.



Interrupt flag



Enable comparator interrupts



Connect comparator to timer/counter T1 (1- connected, 0-not)



Interrupt conditions select

In general Comparator module is simple comparator. If voltage in input of AIN0 is bigger than in AIN1, then result of comparing is equal to 1 and 0 otherwise. Result is stored in ACSR register bit ACO.

If comparator is not used ACD bit should be set to 1 because it will reduce power consumption. Also interrupt from comparator should be disabled. As I mentioned comparator can compare voltage to reference voltage if bit ACBG is set. Then positive input of comparator becomes 1.23V. Second comparator pin can be connected to any pin where ADC input is by using multiplexer. If Multiplexer is enabled (bit ACME in SFIOR register) and ADC is off, then MUX2..0 in ADMUX register selects the input pin(refer to datasheet).