How to Use AVR® ADC in Atmel START


This tutorial goes over how to set up and use the Analog to Digital Converter (ADC) on the ATMega4809 Xplained Pro board. In addition to setting up the ADC, it also discusses one of its key features, the Window Comparator. By the end of the tutorial, you will be able to use the ADC, the Real Time Counter (RTC), and the Event System all in conjunction in order to generate Window Comparator interrupts without needing to write any code.


Hardware Tools

Software Tools

Tool About Installers
Windows Linux Mac OSX
Atmel® Studio
Integrated Development Environment
Atmel® START (ASF4)
Integrated Software Framework
Web Based

 Connection Diagram

For this tutorial, a potentiometer is being used and connected to the mikroBUS™ click™ socket on the Xplained Pro board. On the potentiometer side with two pins one needs to go to ground and the other to 3.3 V. On the opposite side of the potentiometer, this pin needs to be connected to be connected to PD6 on the Xplained Pro Board.



This tutorial will read a connected potentiometer. The potentiometer values print to the serial terminal as long as they are outside of a window of 100-200. 'My Atmel START' project is opened and configured for the ATMega4809 Xplained pro board.


Task 1: Add and Configure USART Peripheral

Navigate to the Add software component button in Atmel START and import a USART peripheral. Next, change the RX and TX pins to PC1 and PC0 respectively. Finally, navigate to the 'Printf support' checkbox and enable it. Without this checkbox, printf cannot be used in code and only USART_write can be compiled.


Note: These are the virtual COM ports on the ATMEGA4809. This information can be found in the 'PINMUX configuration' tab of Atmel START.



Task 2: Add and Configure Timer

Navigate to the Add software component button in Atmel START and import an RTC peripheral. Next, click on the 'RTC Enable' checkbox and change the period of the RTC to '3200'.


Note: The clock is running at 32.77 kHz and by setting the RTC period to '3200', this will later mean that the ADC will be set to trigger roughly every tenth of a second.


Task 3: Add and Configure ADC Basic Attributes

Navigate to the Add software component button and import an ADC peripheral. Next, change the driver mode from 'Basic' to 'Window'. This gives you the ability to use the window compare functionality of the ADC.


Task 3a

Next, find the 'Signals' checkboxes and check 'PD6'. Connect the potentiometer to the analog pin on the mikroBUS click socket featured on the ATMEGA4809 Xplained Pro Board.



Task 3b

Navigate to the 'ADC Enable' checkbox and verify that the ADC is enabled.


Task 3c

Change the 'ADC Resolution' field to '8-bit' mode. Print out the values of the potentiometer to the Data Visualizer. For this task, you DO NOT need 10 bits of resolution, so this value can be set to 8 bits.


Task 3d

Lastly, configure the 'Analog Channel Selection' field so that it is set to 'ADC input pin 6'. If you recall from the previous steps, we are using the PD6 pin for the analog reading. This pin corresponds to analog input pin 6, therefore this field needs to be set in order to tell the ADC which channel to read from. If everything is set up correctly, your basic configuration should look something like what is shown below.



Task 4: Configure the Window Comparator

Set up the ADC so that an interrupt is triggered only when a value is outside of the 100-200 window. First, start by setting the Window Compare Mode to 'Outside'. Next, set the high and low threshold values to '200' and '100' respectively.


Task 5: Configure Window Compare Interrupts

Enable the ability for the ADC to trigger interrupts when the window criteria is satisfied. Navigate to the 'Window Comparator Interrupt Enable' checkbox and click it. Next, verify that the 'Include harness for IRQ on conversion satisfying window criteria' box is checked.

Note: By enabling this box, Atmel START automatically writes the interrupt service routine for the window compare interrupt. This includes clearing the flag bit and setting up the correct interrupt vector.


Task 6: Enable Event Configuration

Under the "Event Configuration" section there is a checkbox labeled 'Start Event Input Enable'. Enable this checkbox so that this peripheral can be used by the Event System which we will later add so that the previously added RTC can be used to trigger the ADC reads. Once finished, your ADC configuration should look something like this:



Task 7: Add and Configure the Event System

Navigate to the Add software component button in Atmel START and import an Event System peripheral. Once added, there will be a new button on the left-hand side that says 'Events' with a lightning bolt icon. Click this button to view the 'Event System Configurator'.


Task 7a

Once in the Configurator, route the 'RTC counter overflow' to 'channel 0'. Once connected, route channel 0 to the 'ADC Trigger on Conversion Event'. Your configurator should look something like the picture below when finished.

Note: By doing this, you are routing the RTC so that it triggers an ADC conversion every time the counter overflows.



Task 8: Enable Global Interrupts

The last step in the START configuration is to enable global interrupts. Without this, the window compare interrupt will not execute. Navigate back to the START Dashboard and locate the 'CPUINT' tab. Within this tab click on the 'Global Interrupt Enable' checkbox. This will be all of the START configurations that you need to do. Therefore, the project can now be generated.



Task 9a: Print the Results In Data Visualizer

The only line of code that you need to write for this project is to print out the result of our window comparison. Navigate to the adc_window.c file which can be found in the 'Solution Explorer' under the src folder. At the bottom of this file, you should see an ISR for the ADC window compare. Before the interrupt flag is cleared, print out the result of your ADC conversion.



Task 9a

Navigate to the 'Tools' tab and open the Data Visualizer. Within the Data Visualizer open a serial port and change the COM Port to the port that your device is connected to.


If everything is configured correctly, you should be able to turn your connected potentiometer and see data being printed out only if the result is outside of the 100-200 window. When inside this window, the printed values of your potentiometer stop being output.



Through core independent analog capabilities featured on the ATMEGA4809 ADC, you were able to set up a monitor of your potentiometer that does not need any code to operate. The only time that code is being executed is when the peripheral interrupts to print out the value. This means that your system can sleep outside of these interrupts, or focus on other tasks when your ADC conditions are not met.


This tutorial walked you through the set up of the Window Comparator capabilities on the ADC. This implementation required no code to trigger the ADC reads. Due to the ADC's hardware implementation, your system can respond much faster to environmental events and can also consume much less power doing so. Due to the fact that your CPU is not involved until a certain criterion is met, your system can become more sophisticated and focus on other tasks.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.