Section 6.5 Analog Comparator on the ATmega328P
As discussed in Sectionย 6.2, an analog comparator is capable of comparing two analog voltages and either setting or clearing an output voltage based on the solution to the inequality given in (6.5.1).
\begin{equation}
V_{out} = \begin{cases} \textrm{VCC}, \amp \text{if } V_p \geq V_n\\
0, \amp \text{otherwise} \end{cases}\tag{6.5.1}
\end{equation}
On the ATmega328P, there is an analog comparator that can be used in this manner. The positive pin is
AIN0 which is located on port D. The negative pin can be AIN1 (also located on port D), or selected from one of the ADC input channels, based on the values configured in ADCSRA and ADMUX. The value of the comparator output is saved in the analog comparator control and status register (ACSR) in the analog comparator output (ACO) bit.
The analog comparator can be used to trigger interrupts when the output of the analog comparator toggles, has a falling edge, or has a rising edge.
When using analog values on pins
AIN0 and/or AIN1, the digital input buffer on whichever of those pins is in use should be disabled. This can be accomplished by setting the corresponding bit in the digital input disable register 1 (DIDR1) register.
