ADC Tutorial
ADC Tutorial
ADC Tutorial
1.1
What is ADC?
1.2
www.tbt.e-yantra.org
1.3
For using ADC certain registers must be configured for initialization. These
registers are used for selecting clock source, selecting channel and starting
ADC conversion process.
1.3.1
ADMUX register together with MUX5 bit in ADCSRB is used for selecting
the analog input channel and selecting the reference voltage. The detailed
structure and description of register is given below:
REFSO, REFS1 - these bits are used for selecting the reference voltage
for ADC. Table below shows various options.
www.tbt.e-yantra.org
1.3.2
ADCSRA register is used for selecting the clock frequency of ADC. It also
contains ADEN for enabling the ADC and ADSC for starting the
conversion process. The other function of register is described below:
www.tbt.e-yantra.org
ADPS0
0
1
0
1
0
1
0
1
Division factor
2
2
4
8
16
32
64
128
1.3.3
ADCSRB register contains bits for selection of trigger source and the
MUX5 bit.
MUX5 this bit together with ADMUX4:0 is used to select required ADC
channel
ADTS2:0 These bits are used for selecting the trigger source when
ADATE is set in ADCSRA
1.4
If ADLAR=1, ADC is in left adjusted mode. So result after conversion is found in ADCL and ADCH register as shown below:
Selecting ADLAR=1, will store the most significant values of the conversion
result in ADCH. Hence, only the ADCH register can be read for the 8 bit
version of the conversion result (ADC Data).
1.5
In this section, we will learn about various settings of the ADC for getting s
sensor value and if this value is less than a certain limit sound the buzzer.
In this example, we will be running ADC in single conversion mode (i.e.
making the ADSC bit to logic 1 whenever we wish to start conversion).
We will be reading the analog value from ADC channel number 9 in this
example.
1.5.1
Initialization of ADC
All the 8 pins of PORTF and PORTK act as ADC input channels.
Hence, set the direction of all the pins of both these ports as inputs
and Initialize the corresponding port pin values as 0.
www.tbt.e-yantra.org
1
2
3
4
DDRF = 0x00;
PORTF = 0x00;
DDRK = 0x00;
PORTK = 0x00;
For initializing the ADC, first enable the ADC by setting the ADEN
bit in ADCSRA.
Then we have to select the frequency of clock source for ADC and
reference voltage of ADC. For getting the accurate reading from the
ADC, clock frequency to ADC block must be between 50 kHz to 200
kHz. Hence, we select a prescalar of 128 (i.e. ADPS2:ADPS0=1).
Finally, select the external 5V connected to AREF pin as the reference
voltage by setting REFS1=0 and REFS1=0 in the ADMUX register.
So for initialization, values of ADMUX = 0x00 and ADCSRA =
0b10000111 = 0x87.
Initial value of ADCSRB is set to 0.
1
2
3
4
7
8
}
Listing 1.1: ADC initialization
www.tbt.e-yantra.org
1.5.2
Now for reading the ADC value from a sensor we do the following steps:
Select the channel of microcontroller where sensor is connected by
setting MUX5:0 values. For selecting ADC channel no.9,
MUX5:0=100001. In the ADMUX register, only MUX 4:0 bits are
present, hence MUX 4:0=00001.
We will use only the most significant 8 bits of the ADC conversion
result. Therefore set ADLAR=1 in ADMUX register.
ADMUX |= 0x21.
ADCSRB |= 0x08.
ADCSRA| = 0x40.
while((ADCSRA&0x10)==0);
complete
adc_value = ADCH;
ADCSRA = ADCSRA|0x10;
Finally listing 1.3 contains the example function for reading a ADC channel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1.6
www.tbt.e-yantra.org
10
1
2
3
6
7
10
11
12
13
14
15
16
17
18
19
}
Listing 1.3: ADC read channel
The ADC channel Read function for white line sensors will be the same as
code described in listing 1.2 except the if condition included to check if
the channel number is greater than 7. (Since the channel number of all the
three white line sensors is less than 7).
This concludes the tutorial. All the best!
www.tbt.e-yantra.org
11