3 Level Crossing Detector

This example shows a basic initialization and setup for the AC peripheral. The application monitors an analog input signal, compares it to a fixed voltage, and notifies the user via interrupt and an output pin every time the input signal crosses the fixed voltage level. The comparator can be used to monitor battery voltage (or any other DC level).

Figure 3-1. Analog Comparator as Voltage Monitor

To monitor an external voltage, the AC input must be connected to this voltage using an I/O pin. This pin needs to have the digital input buffer and the pull-up resistor disabled, to have the highest possible input impedance. For the ATmega4809, PORTD pin 2 (PD2/AINP0) is used as AC positive input:

Figure 3-2. AC Positive Input

This translates to the following code:

PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc;

In the selected application, the AC uses the analog pin as positive input and an internal reference for its negative input. The Voltage Reference (VREF) peripheral must be configured before using the internal voltage reference on negative input.

Figure 3-3. VREF Block Diagram

The VREF provides control registers for selecting between multiple internal reference levels. The internal references are generated from an internal band gap.

Figure 3-4. VREF.CTRLA - AC Voltage Reference Selection

A value of 1.5V is selected as reference voltage:

VREF.CTRLA = VREF_AC0REFSEL_1V5_gc;

To enable VREF voltage generation, the output buffer must be enabled after the selection of reference voltage. Do this by setting the AC0REFEN bit from the VREF Control B register:

Figure 3-5. VREF.CTRLB - Enable the AC0REFEN bit

This translates to the following code:

VREF.CTRLB = VREF_ADC0REFEN_bm;

After configuring the peripherals and the modules required by the AC, the selection of inputs is done using the MUXCTRLA register as follows:

Figure 3-6. AC Input Selection

The positive input pin 0 (AINP0) and internal DAC reference are used as AC inputs:

AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc | AC_MUXNEG_DACREF_gc;
The analog value used by AC (VDACREF) is derived from internal reference using the DACREF register, and the output voltage is defined by:
V D A C R E F = D A C R E F 256 × V R E F
The DACREF value can be calculated using the following macro:
#define DACREF_VALUE	         (VDACREF * 256 / VREF)
where VDACREF represents the desired value on the analog comparator input and VREF represents the value selected as internal reference (1.5 Volts).
Important: Configure the DACREF register to select 0.8V on the negative input to implement this application. The user must select a proper combination of R1 and R2 resistors (see Figure 3-1) in such a way that when battery voltage low threshold is reached, voltage V1 will be 0.8V:
V 1 = 0.8 V = V B A T × R 2 ( R 1 + R 2 )
Assuming  V B A T = 3 V    and R2 = 10 k Ω , the value of R1 must be 27.5 k Ω

The output of the comparator is present on the external pin, and the interrupts are enabled on the negative edge to notify the application when the critical level is reached. That is done using the CTRLA register:

Figure 3-7. AC0.CTRLA - Set AC, Enable Interrupts and Output

These settings translate to the following code:

AC0.CTRLA = AC_ENABLE_bm | AC_INTMODE_NEGEDGE_gc | AC_OUTEN_bm;

The AC peripheral interrupt must be enabled to complete the setup and make the selected interrupt available to the application. Do this by using the CMP bit from the INTCTRL register:

Figure 3-8. AC0.INTCTRL - Enable AC Interrupt
Tip: The full code example is available in the Appendix section.

An MCC generated code example for AVR128DA48 with the same functionality as the one described in this section can be found here: