Microchip tinyAVR® 0- and 1-series, and megaAVR® 0-series devices feature a 10-bit successive approximation register (SAR) Analog-to-Digital Converter (ADC) and are capable of conversion rates up to 115 ksps. It features a flexible multiplexer, which allows the ADC to measure the voltage at multiple single-ended input pins. Single-ended input channels are referred to ground. The ADC input signal is fed through a sample-and-hold circuit which ensures that the input voltage to the ADC is held at a constant level during sampling. It also features multiple internal ADC reference voltages between 0.55V and VDD.
An ADC conversion can be started by software, or by using the Event System (EVSYS) to route an event from other peripherals. A window compare feature is available for monitoring the input signal and can be configured to trigger an interrupt on user-defined thresholds for under, over, inside, or outside a window, with minimum software intervention required.
This repository describes the basic functionality of the ADC in Microchip tinyAVR® 0- and 1-series, and megaAVR® 0-series devices in Single-Ended mode. It is explained in detail in the document AN2573 - ADC Basics with tinyAVR® 0- and 1-series, and megaAVR® 0-series from Microchip. The ATtiny817 Xplained Pro board will be used in these examples.
- MPLAB® X IDE v6.20 or newer
- MPLAB® XC8 Compiler v2.50 or newer
- MPLAB® Code Configurator (MCC) v5.5.1 or newer
- ATtiny817 Xplained Pro
- Micro-USB cable (Type-A/Micro-B)
Note: The newest version of the code is generated by MCC instead of START.
This section shows how this example is set up in MCC. An overview of the MCC setup is shown in the image below:
The CLKCTRL module is located in the System Firmware drop-down menu. This example is configured to run on 3.33 MHz.
In the Clock Settings of the ADC, the Clock Pre-Scaler is set to CLK_PER divided by 4. This is to ensure that max resolution is achieved with an ADC clock frequency between 50 kHz and 1.5 MHz.
In the Hardware Settings, set the Reference Selection to INTREF. The Positive Channel Input Selection sets the ADC input pin, but the code overwites it. It can be changed on this code line #define ADC_CHANNEL 6
in the main.c
file.
The Voltage reference is selected in the VREF peripheral. It is set to 2.5V and matches the code line #define MAX_VOL 2.5
in the main.c
file.
In the USART Configuration Settings, enable Redirect Printf to UART. The rest can be left as default.
In the USART0_Peripheral, the USART Receiver can be disabled, since it is not in use.
Setting inputs and outputs can be done in the Pin Grid View tab. The LED0 pin on the ATtiny817 Xplained Pro is PB4. Set this to be an output, MCC will handle pins used by the USART and ADC peripherals.
Naming and configuring the different pins can be done in the System>Pins menu.
-
Connect the ATtiny817 Xplained Pro board to the PC using the USB cable.
-
Download the zip file or clone the example to get the source code.
-
Open the project in MPLAB X IDE.
-
Choose the use case by configuring the value of the macro
EXAMPLE_CODE
inmain.c
. Refer to the document AN2573 - ADC Basics with tinyAVR® 0- and 1-series, and megaAVR® 0-series to learn about the different use cases. -
Build the solution and program the device by using the buttons Build and Make and Program Device.
-
The ADC result is converted to voltage format and printed through the USART to the terminal.
This project is an illustration of four basic use cases based around the ADC of Microchip tinyAVR® 0- and 1-series, and megaAVR® 0-series devices.