AC - Analog Comparator

The Analog Comparator (AC) of the megaAVR devices has different register sets and limited features compared to the AVR Dx devices. The positive input of the megaAVR Analog Comparator is selectable between the external pin (AIN0) and the internal reference (fixed, 1.23V typical) using the ACBG bit from the ACSR register, while the negative one can be selected between the dedicated external pin (AIN1) and the ADC input pins using the Analog Comparator Multiplexer Enable (ACME) bit in the SFIOR register. When the ACME bit is set and the ADC is switched off (the ADEN bit in the ADCSRA register is zero), the MUX[2..0] bit field in the ADMUX register selects the input pin to replace the negative input to the Analog Comparator.

Figure 1. Analog Comparator Block Diagram for megaAVR® Devices

The AVR Dx devices provide a larger selection for positive and negative inputs, using the ACn.MUXCTRL register. Each Analog Comparator is equipped with a dedicated multiplexer, thus allowing unrestricted use of the available analog inputs. A dedicated internal voltage reference (VREF) is provided for the Analog Comparator. Additionally, each comparator is equipped with an on-chip 8-bit DAC for a precise selection of the reference voltage (DACREF):

Figure 2. Analog Comparator Block Diagram for AVR® Dx Devices

Despite those differences, the functionality for the megaAVR AC can be obtained by a proper configuration of the AC peripheral of AVR Dx. The following code example shows the initialization of the AC peripheral for both families (input compare with a fixed voltage reference):

megaAVR® - AC Basic Initialization

#define AINpin PA3voidACInit(void){
	DDRA  &=~(1<<AINpin);/* set pin as input */
	PORTA &=~(1<<AINpin);/* no pull-up */
	SFIOR |=(1<<ACME);/* enable multiplexer */
	ADCSRA &=~(1<<ADEN);/* make sure ADC is OFF */
	ADMUX |=(0<<MUX2)|(1<<MUX1)|(1<<MUX0);/* select ADC3 as negative AIN */
	ACSR |=(0<<ACD)|/* Comparator ON */(1<<ACBG)|/* Connect 1.23V reference to AIN0 */(0<<ACIE)|/* Comparator Interrupt disabled */(0<<ACIC)|/* input capture disabled */(0<<ACIS1)|(0<<ACIS0);}

AVR® Dx - AC Basic Initialization

/* set DACREF to 1.23 Volts for VREF=1.5 Volts */#define DACREF_VALUE (1.23 * 256 / 1.5)voidAC0_init(void){/* Positive Input - Disable digital input buffer */
         PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc;/* Negative input uses internal reference - voltage reference must be enabled */
         VREF.CTRLA = VREF_AC0REFSEL_1V5_gc;/* Voltage reference at 1.5V */
         VREF.CTRLB = VREF_AC0REFEN_bm;/* AC0 DACREF reference enable: enabled */

         AC0.DACREF = DACREF_VALUE;/* Set DAC voltage reference *//*Select proper inputs for comparator*/
         AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc      /* Positive Input - Analog Positive Pin 0 */| AC_MUXNEG_DACREF_gc;/* Negative Input - DAC Voltage Reference */

         AC0.CTRLA = AC_ENABLE_bm             /* Enable Analog Comparator */| AC_OUTEN_bm;/* Output Buffer Enable: enabled */}