The Analog Comparator has been ignored in the Arduino library software. I discussed it in my Far Inside The Arduino book for the ATmega328P parts, like the Uno and Nano. The Analog Comparator in Arduino Nano Every’s ATmega4809 is more versatile and has more features. There is now an internal DAC that can be connected to one of the comparator inputs allowing comparing a single input voltage against an adjustable value. Consider this figure from the new book:
The yellow trace is a trapezoidal waveform generated by sweeping a PWM output run through a low pass filter. On the same Arduino board the comparator is used to compare the waveform against a fixed 2.5 volts from the DAC. The comparator output is shown in the purple trace. The comparator output goes to a pin that is not brought out on the Arduino Nano Every board, however the Event System was used to direct the comparator output to an existing pin on the board. Analog Comparator interrupts can be triggered by rising, falling, or both edges. In this case both edges are used and another GPIO pin is toggled with each interrupt. This is the blue trace.
This Analog Converter has adjustable hysteresis. Since the trapezoid is noisy, if hysteresis is turned off the output would be too noisy to get those clean waveforms when we capture on both edges. This example would not have been possible on the ATmega328P microcontroller.
The following code was used to initialize the comparator, voltage reference, and event system:
EVSYS.CHANNEL0 = EVSYS_GENERATOR_AC0_OUT_gc; // Connect AC to pin 5 EVSYS.USEREVOUTB = EVSYS_CHANNEL_CHANNEL0_gc; VREF.CTRLA = VREF_AC0REFSEL_AVDD_gc; // Set voltage reference for AC DAC to VDD (5 volts) AC0_MUXCTRLA = 3; // A1 is positive, DAC is negative AC0_DACREF = 128; // Mid scale, 2.5 volts AC0_CTRLA = 7; // Enable AC with 50mv hysteresis, interrupt on both edges AC0_INTCTRL = 1; // Enable AC interrupt