4.5 ADC Example

This section presents a simple configuration example for the ADC0 module on an AVR128DA48 device from the AVR DA series of devices. The information needed to program this controller can be found in the device data sheet, as mentioned in Section 1: Data Sheet Structure and Naming Conventions.

To fully configure the ADC, the user can follow the recommended initialization steps that can be found in the Functional Description subsection from the ADC – Analog-to-Digital Converter section of the data sheet.

After the ADC prescaler is configured, the ADC module is enabled, and a conversion is started. In the code below, the configurations are made using the bit position macros.

Configure ADC Using Bit Positions

	/* ADC register configuration using bit position macros */
	ADC0.CTRLC |= (1 << ADC_PRESC0_bp) | (1 << ADC_PRESC1_bp); /* Configuring the prescaler bits. Configuration: DIV8 */
	ADC0.CTRLA |= (1<< ADC_ENABLE_bp);         /* Enabling the ADC */
	ADC0.COMMAND |= (1<< ADC_STCONV_bp);       /* Starting a conversion */

Alternatively, the ADC can be configured using the bit masks, as in the code below.

Configure ADC Using Bit Masks

	/* ADC register configuration using bit masks macros */
	ADC0.CTRLC |= ADC_PRESC0_bm | ADC_PRESC1_bm;               /* Configuring the prescaler bits. Configuration: DIV8 */
	ADC0.CTRLA |= ADC_ENABLE_bm;               /* Enabling the ADC */
	ADC0.COMMAND = ADC_STCONV_bm;              /* Starting a conversion */

To change the prescaler configuration, a group configuration mask can be used. The original configuration must be cleared first, as shown in the code below.

Change the ADC Prescaler Configuration Using a Group Configuration Mask

	/* Changing an ADC prescaler configuration, ensuring to clear the original configuration */
	ADC0.CTRLC = (ADC0.CTRLC & ~ADC_PRESC_gm) | ADC_PRESC_DIV4_gc;