4 Getting Started with ADC
4.1 Introduction
Author: Victor Berzan, Microchip Technology Inc. |
The Analog-to-Digital Converter (ADC) peripheral converts an analog voltage to a numerical value. This peripheral is included in many AVR® microcontrollers (MCUs). A 10-bit single-ended ADC peripheral is available on most of the tinyAVR® and megaAVR® MCUs, while on the AVR DA family there is a 12-bit differential and single-ended ADC peripheral available.
- ADC
Single Conversion:
Initialize the ADC, start conversion, wait until the conversion is done, and read the ADC result.
- ADC
Free-Running:
Initialize the ADC, enable Free-Running mode, start conversion, wait until the conversion is done, and read the ADC result in an infinite loop.
- ADC
Sample Accumulator:
Initialize the ADC, enable accumulation of 64 samples, start conversion, wait until the conversion is done, and read the ADC result in a loop.
- ADC
Window Comparator:
Initialize the ADC, set the conversion window comparator low threshold, enable the conversion Window mode, enable the Free-Running mode, start the conversion, wait until the conversion is done and read the ADC result in an infinite loop, and light-up an LED if the ADC result is below the set threshold.
- ADC Event
Triggered:
Initialize the ADC, initialize the Real-Time Counter (RTC), configure the Event System (EVSYS) to trigger an ADC conversion on RTC overflow, toggle an LED after each ADC conversion.
4.2 Overview
The Analog-to-Digital Converter (ADC) peripheral produces 10-bit results on tinyAVR and megaAVR microcontrollers and 10-bit/12-bit result on AVR DA microcontrollers. The ADC input can either be internal (e.g,. a voltage reference), or external through the analog input pins. The ADC is connected to an analog multiplexer, which allows the selection of multiple single-ended voltage inputs. The single-ended voltage inputs refer to 0V (GND).
The ADC supports sampling in bursts where a configurable number of conversion results are accumulated into a single ADC result (Sample Accumulation). The ADC input signal is fed through a sample-and-hold circuit that ensures the input voltage to the ADC is held at a constant level during sampling.
Selectable voltage references from the internal Voltage Reference (VREF) peripheral, VDD supply voltage, or external VREF pin (VREFA).
The analog input channel is selected by writing to the MUXPOS bits in the MUXPOS
(ADCn.MUXPOS) register. Any of the ADC input pins, GND, internal Voltage Reference
(VREF), or temperature sensor, can be selected as single-ended input to
the ADC. The ADC is enabled by writing a ‘1
’ to the ADC ENABLE bit in
the Control A (ADCn.CTRLA) register. Voltage reference and input channel selections will
not go into effect before the ADC is enabled. The ADC does not consume power when the
ENABLE bit in ADCn.CTRLA is zero.
The ADC generates a 10-bit result on tinyAVR and megaAVR microcontrollers and a 10-bit/12-bit result on AVR DA microcontrollers. It can be read from the Result (ADCn.RES) register. The result is presented right adjusted.
4.3 ADC Single Conversion
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
ADC0.CTRLC |= ADC_PRESC_DIV4_gc; ADC0.CTRLC |= ADC_REFSEL_INTREF_gc;
ADC0.CTRLA |= ADC_RESSEL_10BIT_gc;
ADC0.CTRLA |= ADC_ENABLE_bm;
ADC0.COMMAND = ADC_STCONV_bm;
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { ; }
1
’ to it
before starting another
conversion.ADC0.INTFLAGS = ADC_RESRDY_bm;
adcVal = ADC0.RES;
An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here:
4.4 ADC Free-Running
ADC0.CTRLA |= ADC_FREERUN_bm;
ADC0.COMMAND = ADC_STCONV_bm;
while(1)
{
if (ADC0_conersionDone())
{
adcVal = ADC0_read();
}
}
An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here:
4.5 ADC Sample Accumulator
ADC0.CTRLB = ADC_SAMPNUM_ACC64_gc;
ADC0.COMMAND = ADC_STCONV_bm;
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) { ; }
adcVal = ADC0.RES; adcVal = adcVal >> ADC_SHIFT_DIV64;
1
’ to it before starting
another
conversion.ADC0.INTFLAGS = ADC_RESRDY_bm;
An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here:
4.6 ADC Window Comparator
In Window Comparator mode, the device can detect if the ADC result is below or above a specific threshold value. This is useful when monitoring a signal that is required to be maintained in a specific range, or for signaling low battery/overcharge, etc. The window comparator can be used in both Free-Running mode and Single Conversion mode. In this example, the window comparator is used in Free-Running mode, because a monitored signal requires continuous sampling, and the Free-Running mode reduces the CPU load by not requiring a manual start for each conversion.
ADC0.WINLT = WINDOW_CMP_LOW_TH_EXAMPLE;
ADC0.CTRLE = ADC_WINCM_BELOW_gc;
1
’ to
it.ADC0.INTFLAGS = ADC_WCMP_bm;
An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here:
4.7 ADC Event Triggered
1
’ to the Start Event Input (STARTEI) bit in the Event Control
(ADCn.EVCTRL) register.ADC0.EVCTRL |= ADC_STARTEI_bm;
Any incoming event routed to the ADC through the Event System (EVSYS) will trigger an ADC conversion. The event trigger input is edge sensitive. When an event occurs, STCONV in ADCn.COMMAND is set. STCONV will be cleared when the conversion is complete.
- The RTC overflow event must be linked to channel 0 of the Event System.
- The Event User ADC0 must be configured to take its input from channel 0.
- The STARTEI bit in the EVCTRL register of the ADC must be set to enable the ADC conversion to be triggered by events.
EVSYS.CHANNEL0 = EVSYS_GENERATOR_RTC_OVF_gc; /* Real Time Counter overflow */ EVSYS.USERADC0 = EVSYS_CHANNEL_CHANNEL0_gc; /* Connect user to event channel 0 */ ADC0.EVCTRL |= ADC_STARTEI_bm; /* Enable event triggered conversion */
An MCC generated code example for AVR128DA48, with the same functionality as the one described in this section, can be found here:
4.8 References
4.9 Appendix
ADC Single Conversion Code Example
/* RTC Period */ #define RTC_PERIOD (511) #include <avr/io.h> #include <avr/interrupt.h> uint16_t adcVal; void ADC0_init(void); uint16_t ADC0_read(void); void ADC0_init(void) { /* Disable digital input buffer */ PORTD.PIN6CTRL &= ~PORT_ISC_gm; PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable pull-up resistor */ PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm; ADC0.CTRLC = ADC_PRESC_DIV4_gc /* CLK_PER divided by 4 */ | ADC_REFSEL_INTREF_gc; /* Internal reference */ ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ | ADC_RESSEL_10BIT_gc; /* 10-bit mode */ /* Select ADC channel */ ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; } uint16_t ADC0_read(void) { /* Start ADC conversion */ ADC0.COMMAND = ADC_STCONV_bm; /* Wait until ADC conversion done */ while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) { ; } /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; } int main(void) { ADC0_init(); adcVal = ADC0_read(); while (1) { ; } }
ADC Free-Running Code Example
#include <avr/io.h> #include <stdbool.h> uint16_t adcVal; void ADC0_init(void); uint16_t ADC0_read(void); void ADC0_start(void); bool ADC0_conersionDone(void); void ADC0_init(void) { /* Disable digital input buffer */ PORTD.PIN6CTRL &= ~PORT_ISC_gm; PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable pull-up resistor */ PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm; ADC0.CTRLC = ADC_PRESC_DIV4_gc /* CLK_PER divided by 4 */ | ADC_REFSEL_INTREF_gc; /* Internal reference */ ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ | ADC_RESSEL_10BIT_gc; /* 10-bit mode */ /* Select ADC channel */ ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; /* Enable FreeRun mode */ ADC0.CTRLA |= ADC_FREERUN_bm; } uint16_t ADC0_read(void) { /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; } void ADC0_start(void) { /* Start conversion */ ADC0.COMMAND = ADC_STCONV_bm; } bool ADC0_conersionDone(void) { return (ADC0.INTFLAGS & ADC_RESRDY_bm); } int main(void) { ADC0_init(); ADC0_start(); while(1) { if (ADC0_conersionDone()) { adcVal = ADC0_read(); /* In FreeRun mode, the next conversion starts automatically */ } } }
ADC Sample Accumulator Code Example
#define ADC_SHIFT_DIV64 (6) #include <avr/io.h> uint16_t adcVal; void ADC0_init(void); uint16_t ADC0_read(void); void ADC0_init(void) { /* Disable digital input buffer */ PORTD.PIN6CTRL &= ~PORT_ISC_gm; PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable pull-up resistor */ PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm; ADC0.CTRLC = ADC_PRESC_DIV4_gc /* CLK_PER divided by 4 */ | ADC_REFSEL_INTREF_gc; /* Internal reference */ ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ | ADC_RESSEL_10BIT_gc; /* 10-bit mode */ /* Select ADC channel */ ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; /* Set the accumulator mode to accumulate 64 samples */ ADC0.CTRLB = ADC_SAMPNUM_ACC64_gc; } uint16_t ADC0_read(void) { /* Start ADC conversion */ ADC0.COMMAND = ADC_STCONV_bm; /* Wait until ADC conversion done */ while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) { ; } /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; } int main(void) { ADC0_init(); while (1) { adcVal = ADC0_read(); /* divide by 64 */ adcVal = adcVal >> ADC_SHIFT_DIV64; } }
ADC Window Comparator Code Example
#define WINDOW_CMP_LOW_TH_EXAMPLE (0x100) #include <avr/io.h> #include <stdbool.h> uint16_t adcVal; void ADC0_init(void); uint16_t ADC0_read(void); void ADC0_start(void); bool ADC0_conersionDone(void); bool ADC0_resultBelowTreshold(void); void ADC0_clearWindowCmpIntFlag(void); void LED0_init(void); void LED0_on(void); void LED0_off(void); void ADC0_init(void) { /* Disable digital input buffer */ PORTD.PIN6CTRL &= ~PORT_ISC_gm; PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable pull-up resistor */ PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm; ADC0.CTRLC = ADC_PRESC_DIV4_gc /* CLK_PER divided by 4 */ | ADC_REFSEL_INTREF_gc; /* Internal reference */ ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ | ADC_RESSEL_10BIT_gc; /* 10-bit mode */ /* Select ADC channel */ ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; /* Set conversion window comparator low threshold */ ADC0.WINLT = WINDOW_CMP_LOW_TH_EXAMPLE; /* Set conversion window mode */ ADC0.CTRLE = ADC_WINCM_BELOW_gc; /* Enable FreeRun mode */ ADC0.CTRLA |= ADC_FREERUN_bm; } uint16_t ADC0_read(void) { /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; } void ADC0_start(void) { /* Start conversion */ ADC0.COMMAND = ADC_STCONV_bm; } bool ADC0_conersionDone(void) { return (ADC0.INTFLAGS & ADC_RESRDY_bm); } bool ADC0_resultBelowTreshold(void) { return (ADC0.INTFLAGS & ADC_WCMP_bm); } void ADC0_clearWindowCmpIntFlag(void) { /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_WCMP_bm; } void LED0_init(void) { /* Make High (OFF) */ PORTB.OUT |= PIN5_bm; /* Make output */ PORTB.DIR |= PIN5_bm; } void LED0_on(void) { /* Make Low (ON) */ PORTB.OUT &= ~PIN5_bm; } void LED0_off(void) { /* Make High (OFF) */ PORTB.OUT |= PIN5_bm; } int main(void) { ADC0_init(); LED0_init(); ADC0_start(); while(1) { if (ADC0_conersionDone()) { if(ADC0_resultBelowTreshold()) { LED0_on(); ADC0_clearWindowCmpIntFlag(); } else { LED0_off(); } adcVal = ADC0_read(); } } }
ADC Event Triggered Code Example
/* RTC Period */ #define RTC_PERIOD (511) #include <avr/io.h> #include <avr/interrupt.h> volatile uint16_t adcVal; void ADC0_init(void); void LED0_init(void); void LED0_toggle(void); void RTC_init(void); void EVSYS_init(void); void ADC0_init(void) { /* Disable digital input buffer */ PORTD.PIN6CTRL &= ~PORT_ISC_gm; PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc; /* Disable pull-up resistor */ PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm; ADC0.CTRLC = ADC_PRESC_DIV4_gc /* CLK_PER divided by 4 */ | ADC_REFSEL_INTREF_gc; /* Internal reference */ ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */ | ADC_RESSEL_10BIT_gc; /* 10-bit mode */ /* Select ADC channel */ ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc; /* Enable interrupts */ ADC0.INTCTRL |= ADC_RESRDY_bm; /* Enable event triggered conversion */ ADC0.EVCTRL |= ADC_STARTEI_bm; } void LED0_init(void) { /* Make High (OFF) */ PORTB.OUT |= PIN5_bm; /* Make output */ PORTB.DIR |= PIN5_bm; } void LED0_toggle(void) { PORTB.IN |= PIN5_bm; } ISR(ADC0_RESRDY_vect) { /* Clear flag by writing '1': */ ADC0.INTFLAGS = ADC_RESRDY_bm; adcVal = ADC0.RES; LED0_toggle(); } void RTC_init(void) { uint8_t temp; /* Initialize 32.768kHz Oscillator: */ /* Disable oscillator: */ temp = CLKCTRL.XOSC32KCTRLA; temp &= ~CLKCTRL_ENABLE_bm; /* Enable writing to protected register */ CPU_CCP = CCP_IOREG_gc; CLKCTRL.XOSC32KCTRLA = temp; while(CLKCTRL.MCLKSTATUS & CLKCTRL_XOSC32KS_bm) { ; /* Wait until XOSC32KS becomes 0 */ } /* SEL = 0 (Use External Crystal): */ temp = CLKCTRL.XOSC32KCTRLA; temp &= ~CLKCTRL_SEL_bm; /* Enable writing to protected register */ CPU_CCP = CCP_IOREG_gc; CLKCTRL.XOSC32KCTRLA = temp; /* Enable oscillator: */ temp = CLKCTRL.XOSC32KCTRLA; temp |= CLKCTRL_ENABLE_bm; /* Enable writing to protected register */ CPU_CCP = CCP_IOREG_gc; CLKCTRL.XOSC32KCTRLA = temp; /* Initialize RTC: */ while (RTC.STATUS > 0) { ; /* Wait for all register to be synchronized */ } RTC.CTRLA = RTC_PRESCALER_DIV32_gc /* 32 */ | RTC_RTCEN_bm /* Enable: enabled */ | RTC_RUNSTDBY_bm; /* Run In Standby: enabled */ /* Set period */ RTC.PER = RTC_PERIOD; /* 32.768kHz External Crystal Oscillator (XOSC32K) */ RTC.CLKSEL = RTC_CLKSEL_TOSC32K_gc; /* Run in debug: enabled */ RTC.DBGCTRL |= RTC_DBGRUN_bm; } void EVSYS_init(void) { /* Real Time Counter overflow */ EVSYS.CHANNEL0 = EVSYS_GENERATOR_RTC_OVF_gc; /* Connect user to event channel 0 */ EVSYS.USERADC0 = EVSYS_CHANNEL_CHANNEL0_gc; } int main(void) { ADC0_init(); LED0_init(); RTC_init(); EVSYS_init(); /* Enable Global Interrupts */ sei(); while (1) { ; } }