Learn, Implement and Share

Note

This article is a part of Arduino / ATmega328p Embedded C Firmware Programming Tutorial. Consider exploring the course home page for articles on similar topics.

Arduino Tutorial Embedded C Register Level Arduino Master Class

Arduino Tutorial Embedded C Register Level Arduino Master Class

Also visit the Release Page for Register Level Embedded C Hardware Abstraction Library and Code for AVR.

Introduction

The ATmega48A/PA/88A/PA/168A/PA/328/P features a 10-bit successive approximation ADC. The ADC is connected to an 8-channel Analog Multiplexer which allows eight single-ended voltage inputs constructed from the pins of Port A. The single-ended voltage inputs refer to 0V (GND).

ATmega328P ADC Block Diagram
ATmega328P ADC Block Diagram

The ADC uses registers ADMUX, ADCSRA, ADCL, ADCH, ADCSRB, and DIDR0 to configure the hardware and to do analog to digital conversion. Along with register configuration, ADC hardware also needs a separate analog supply voltage pin, AVCC. AVCC must not differ more than ±0.3V from VCC. Internal reference voltages can be set at 1.1V or AVCC. When the voltage reference is selected externally the AREF pin is connected with a capacitor for better noise performance.

What You Will Learn

  • What are the ADC Registers in Arduino?
  • What are the ADC Registers in AVR ATmega328p?
  • Which AVR Registers control and configures Analog to Digital Converter?
  • What is the function of the ADC Registers?

ADMUX – ADC Multiplexer Selection Register

Bit76543210
0x7CREFS1REFS0ADLARMUX3MUX2MUX1MUX0
Read/WriteR/WR/WR/WRR/WR/WR/WR/W
Initial Value00000000

• Bit 7:6 – REFS[1:0]: Reference Selection Bits
These bits select the voltage reference for the ADC. The internal voltage reference options may not be used if an external reference voltage is being applied to the AREF pin.

REFS1REFS0Voltage Reference Selection
00AREF, Internal VREF turned off
01AVCC with external capacitor at AREF pin
10Reserved
11Internal 1.1V Voltage Reference with external capacitor at AREF pin

• Bit 5 – ADLAR: ADC Left Adjust Result
The ADLAR bit affects the presentation of the ADC conversion result in the ADC Data Register. Write one to ADLAR to left adjust the result. Otherwise, the result is right adjusted.

• Bits 3:0 – MUX[3:0]: Analog Channel Selection Bits
The value of these bits selects which analog inputs are connected to the ADC.

MUX[3:0]Single Ended Input
0000ADC0
0001ADC1
0010ADC2
0011ADC3
0100ADC4
0101ADC5
0110ADC6
0111ADC7
1000ADC8(1)
1001(reserved)
1010(reserved)
1011(reserved)
1100(reserved)
1101(reserved)
11101.1V (VBG)
11110V (GND)

ADCSRA – ADC Control and Status Register A

Bit76543210
0x7AADENADSCADATEADIFADIEADPS2ADPS1ADPS0
Read/WriteR/WR/WR/WR/WR/WR/WR/WR/W
Initial Value00000000

• Bit 7 – ADEN: ADC Enable
Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned off. Turning the ADC off while a conversion is in progress, will terminate this conversion.

• Bit 6 – ADSC: ADC Start Conversion
In Single Conversion mode, write this bit to one to start each conversion. In Free Running mode, write this bit to one that will take 25 ADC clock cycles instead of the normal 13 This first conversion performs initialization of the ADC. ADSC will read as one as long as a conversion is in progress. When the conversion is complete, it returns to zero.

• Bit 5 – ADATE: ADC Auto Trigger Enable
When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC will start a conversion on a positive edge of the selected trigger signal. The trigger source is selected by setting the ADC Trigger Select bits, ADTS in
ADCSRB.

• Bit 4 – ADIF: ADC Interrupt Flag
This bit is set when an ADC conversion completes and the Data Registers are updated. The ADC Conversion Complete Interrupt is executed if the ADIE bit and the I-bit in SREG are set. ADIF is cleared by hardware when executing the corresponding interrupt handling vector.

• Bit 3 – ADIE: ADC Interrupt Enable
When this bit is written to one and the I-bit in SREG is set, the ADC Conversion Complete Interrupt is activated.

• Bits 2:0 – ADPS[2:0]: ADC Prescaler Select Bits
These bits determine the division factor between the system clock frequency and the input clock to the ADC.

ADPS2ADPS1ADPS0Division Factor
0002
0012
0104
0118
10016
10132
11064
111128

ADCL and ADCH – The ADC Data Register

ADLAR = 0

Bit15141312111098
0x79ADC9ADC8
Read/WriteRRRRRRRR
Bit76543210
0x78ADC7ADC6ADC5ADC4ADC3ADC2ADC1ADC0
Read/WriteRRRRRRRR

ADLAR = 1

Bit15141312111098
0x79ADC9ADC8ADC7ADC6ADC5ADC4ADC3ADC2
Read/WriteRRRRRRRR
Bit76543210
0x78ADC1ADC0
Read/WriteRRRRRRRR

When an ADC conversion is complete, the result is found in these two registers. A total of 10 bits of conversion result is stored in these two registers.

ADCSRB – ADC Control and Status Register B

Bit76543210
ACMEADTS2ADTS1ADTS0
Read/WriteRR/WRRRR/WR/WR/W
Initial Value00000000

• Bit 2:0 – ADTS[2:0]: ADC Auto Trigger Source
If ADATE in ADCSRA is written to one, the value of these bits selects which source will trigger an ADC conversion.

ADTS2ADTS1ADTS0Trigger Source
000Free Running mode
001Analog Comparator
010External Interrupt Request 0
011Timer/Counter0 Compare Match A
100Timer/Counter0 Overflow
101Timer/Counter1 Compare Match B
110Timer/Counter1 Overflow
111Timer/Counter1 Capture Event

DIDR0 – Digital Input Disable Register 0

Bit76543210
0x7EADC5DADC4DADC3DADC2DADC1DADC0D
Read/WriteRRR/WR/WR/WR/WR/WR/W
Initial Value00000000

• Bit 5:0 – ADC5D…ADC0D: ADC5…0 Digital Input Disable
When this bit is written logic one, the digital input buffer on the corresponding ADC pin is disabled. The corresponding PIN Register bit will always read as zero when this bit is set. When an analog signal is applied to the ADC5…0 pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the digital input buffer.


Crazy Engineer

MAKER - ENGINEER - YOUTUBER

2 Comments

Shriniketan · February 25, 2021 at 8:49 am

very good explanation

    Crazy Engineer · March 3, 2021 at 6:20 pm

    Thank You for choosing this tutorial. Keep Exploring the full series.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.