Advertisement

Quick Links

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
available on most of the tinyAVR
This technical brief describes how the Analog-to-Digital Converter module works on megaAVR
microcontrollers. It covers the following use cases:
• ADC Single Conversion:
Initialize the ADC, start conversion, wait until the conversion is done, read 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 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.
Note:  The code examples were developed on ATmega4809 Xplained Pro (ATMEGA4809-XPRO).
©
2018 Microchip Technology Inc.
Getting Started with ADC
®
microcontrollers (MCUs). A 10-bit single-ended ADC peripheral is
®
®
and megaAVR
MCUs.
TB3209
®
0-series
DS90003209A-page 1

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the TB3209 and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Summary of Contents for Microchip Technology TB3209

  • Page 1: Introduction

    TB3209 Getting Started with ADC 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 ®...
  • Page 2: Table Of Contents

    9. Appendix........................17 The Microchip Web Site....................24 Customer Change Notification Service................24 Customer Support......................24 Microchip Devices Code Protection Feature..............24 Legal Notice........................25 Trademarks........................25 Quality Management System Certified by DNV.............26 Worldwide Sales and Service..................27 DS90003209A-page 2 © 2018 Microchip Technology Inc.
  • Page 3: Relevant Devices

    Downward migration may require code modification due to fewer available instances of some peripherals. • Horizontal migration to the left reduces the pin count and therefore, the available features. DS90003209A-page 3 © 2018 Microchip Technology Inc.
  • Page 4: Megaavr ® 0-Series

    0-series Overview Flash 48 KB ATmega4808 ATmega4809 32 KB ATmega3208 ATmega3209 16 KB ATmega1608 ATmega1609 8 KB ATmega808 ATmega809 Pins 28/32 Devices with different Flash memory size typically also have different SRAM and EEPROM. DS90003209A-page 4 © 2018 Microchip Technology Inc.
  • Page 5: Overview

    ��� = �� presented right adjusted. The result for a 10-bit conversion is given as: � ��� where V is the voltage on the selected input pin and V is the selected voltage reference. DS90003209A-page 5 © 2018 Microchip Technology Inc.
  • Page 6: Adc Single Conversion

    The ADC Clock Prescaler can be used to divide the clock frequency. In this particular example, the clock is divided by 4. The ADC can use V , external reference or internal reference for its positive reference. The internal reference is used in this example. DS90003209A-page 6 © 2018 Microchip Technology Inc.
  • Page 7 Figure 3-2. ADC0.CTRLC Voltage Reference Selection ADC0.CTRLC |= ADC_PRESC_DIV4_gc; ADC0.CTRLC |= ADC_REFSEL_INTREF_gc; The ADC resolution is set by the RESSEL bit in the ADC0.CTRLA register. The ADC is enabled by setting the ENABLE bit in the ADC0.CTRLA register. DS90003209A-page 7 © 2018 Microchip Technology Inc.
  • Page 8 ADC0.COMMAND = ADC_STCONV_bm; When the conversion is done, the RESRDY bit in the ADC0.INTFLAGS gets set by the hardware. The user must wait for that bit to get set before reading the ADC result. DS90003209A-page 8 © 2018 Microchip Technology Inc.
  • Page 9 The user must clear the RESRDY bit by writing ‘1’ to it before starting another conversion. ADC0.INTFLAGS = ADC_RESRDY_bm; The conversion result can be read from the ADC0.RES register. adcVal = ADC0.RES; Tip:  The full code example is also available in the Appendix section. DS90003209A-page 9 © 2018 Microchip Technology Inc.
  • Page 10: Adc Free Running

    The ADC conversion is started by setting the STCONV bit in the ADC0.COMMAND register: ADC0.COMMAND = ADC_STCONV_bm; Then the ADC results can be read in a while loop: while(1) (ADC0_conersionDone()) adcVal = ADC0_read(); Tip:  The full code example is also available in the Appendix section. DS90003209A-page 10 © 2018 Microchip Technology Inc.
  • Page 11: Adc Sample Accumulator

    The user can read that value and divide it by the number of samples, in order to get an average value. adcVal = ADC0.RES; adcVal = adcVal >> ADC_SHIFT_DIV64; The user must clear the RESRDY bit by writing ‘1’ to it before starting another conversion. ADC0.INTFLAGS = ADC_RESRDY_bm; DS90003209A-page 11 © 2018 Microchip Technology Inc.
  • Page 12 TB3209 ADC Sample Accumulator Tip:  The full code example is also available in the Appendix section. DS90003209A-page 12 © 2018 Microchip Technology Inc.
  • Page 13: Adc Window Comparator

    For this example, a low threshold is set in the ADC0.WINLT register. Figure 6-1. ADC-WINLT - set Low Threshold ADC0.WINLT = WINDOW_CMP_LOW_TH_EXAMPLE; The Conversion Window mode is set in the ADC0.CTRLE register: Figure 6-2. ADC0.CTRLE - set Window Comparator mode ADC0.CTRLE = ADC_WINCM_BELOW_gc; DS90003209A-page 13 © 2018 Microchip Technology Inc.
  • Page 14 Figure 6-3. ADC0.INTFLAGS - hardware-set WCOMP bit The user must clear that bit by writing ‘1’ to it. ADC0.INTFLAGS = ADC_WCMP_bm; Tip:  The full code example is also available in the Appendix section. DS90003209A-page 14 © 2018 Microchip Technology Inc.
  • Page 15: Adc Event Triggered

    /* 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 */ Tip:  The full code example is also available in the Appendix section. DS90003209A-page 15 © 2018 Microchip Technology Inc.
  • Page 16: References

    More information about the ADC operation modes can be found in the following Application Note: ® megaAVR 0-series Manual (DS40002015) ® ® AN2573 - ADC Basics with tinyAVR 0- and 1-series, and megaAVR 0-series (DS00002573) DS90003209A-page 16 © 2018 Microchip Technology Inc.
  • Page 17: Appendix

    ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_RESRDY_bm; return ADC0.RES; main(void) ADC0_init(); adcVal = ADC0_read(); while Example 9-2. ADC Free Running Code Example #include <avr/io.h> #include <stdbool.h> uint16_t adcVal; DS90003209A-page 17 © 2018 Microchip Technology Inc.
  • Page 18 ADC0_init(); ADC0_start(); while(1) (ADC0_conersionDone()) adcVal = ADC0_read(); /* In FreeRun mode, the next conversion starts automatically */ Example 9-3. ADC Sample Accumulator Code Example #define ADC_SHIFT_DIV64 #include <avr/io.h> uint16_t adcVal; void ADC0_init(void); uint16_t ADC0_read(void); DS90003209A-page 18 © 2018 Microchip Technology Inc.
  • Page 19 Example 9-4. 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) DS90003209A-page 19 © 2018 Microchip Technology Inc.
  • Page 20 /* 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; DS90003209A-page 20 © 2018 Microchip Technology Inc.
  • Page 21 /* 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; DS90003209A-page 21 © 2018 Microchip Technology Inc.
  • Page 22 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; main(void) DS90003209A-page 22 © 2018 Microchip Technology Inc.
  • Page 23 TB3209 Appendix ADC0_init(); LED0_init(); RTC_init(); EVSYS_init(); /* Enable Global Interrupts */ sei(); while DS90003209A-page 23 © 2018 Microchip Technology Inc.
  • Page 24: The Microchip Web Site

    Microchip’s Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property. • Microchip is willing to work with the customer who is concerned about the integrity of their code. DS90003209A-page 24 © 2018 Microchip Technology Inc.
  • Page 25: Legal Notice

    SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries. GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip Technology Inc., in other countries.
  • Page 26: Quality Management System Certified By Dnv

    TB3209 © 2018, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. ISBN: 978-1-5224-3990-5 Quality Management System Certified by DNV ISO/TS 16949 Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California ®...
  • Page 27: Worldwide Sales And Service

    Tel: 46-31-704-60-40 New York, NY Sweden - Stockholm Tel: 631-435-6000 Tel: 46-8-5090-4654 San Jose, CA UK - Wokingham Tel: 408-735-9110 Tel: 44-118-921-5800 Tel: 408-436-4270 Fax: 44-118-921-5820 Canada - Toronto Tel: 905-695-1980 Fax: 905-695-2078 DS90003209A-page 27 © 2018 Microchip Technology Inc.

Table of Contents