15. ADC Programming
15. ADC Programming
1
1. ADC Devices
• Physical quantities like temperature, velocity, pressure, light and etc. are converted
to electrical signals (voltage) using transducers (sensors)
• An analogue-to-digital converter (ADC) is needed to change analogue signals to
digital (binary) numbers so that a microcontroller can read and process them
Resolution
• Resolution of an ADC is the number of output bits n; n can be 8, 10, 12, 16 or 24
bits
• Resolution provides step-size, i.e., smallest change that can be discerned by an ADC
2
• Widely used resolutions for ADCs and corresponding number of steps and step-sizes
are shown in the table below for a reference voltage of 5V:
n-bit Number of steps Step-size
8 256 5V/256 = 19.53 mV
10 1024 5V/1024 = 4.88 mV
12 4096 5V/4096 = 1.2 mV
16 65536 5V/65536 = 0.076 mV
Conversion time
• Conversion time is the time it takes the ADC to convert the analogue input to a digital
(binary) number
• It depends on the ADC clock source as well as ADC method
3
Vref
• Vref is equal to the maximum input voltage that can be applied to the ADC
• Vref and the resolution determine the step-size
• 𝑆𝑡𝑒𝑝 𝑠𝑖𝑧𝑒 = 𝑉𝑟𝑒𝑓ൗ2𝑛
• The tables below indicates Vref, and corresponding Vin range and step-size for 8-bit
and 10 ADC’s:
Vref (V) Vin Range (V) Step-size Vref (V) Vin Range (V) Step-size
5.0 0 to 5V 5V/256 = 19.53 mV 5.0 0 to 5V 5V/1024 = 4.88 mV
4.0 0 to 4V 4V/256 = 15.62 mV 4.096 0 to 4.096V 4.096V/1024 = 4 mV
3.0 0 to 3V 3V/256 = 11.71 mV 3.0 0 to 3V 3V/1024 = 2.93 mV
2.56 0 to 2.56V 2.56V/256 = 10 mV 2.56 0 to 2.56V 2.56V/1024 = 2.5 mV
2.0 0 to 2V 2V/256 = 7.81 mV 2.048 0 to 2.048V 2.048V/1024 = 2 mV
1.28 0 to 1.28V 1.28V/256 = 5 mV 1.28 0 to 1.28V 1.28/1024 = 1.25 mV
1.0 0 to 1V 1V/256 = 3.90 mV 1.024 0 to 1.024V 1.024V/1024 = 1 mV
4
Digital data output
𝑉𝑖𝑛 𝑉𝑖𝑛∙2𝑛
• The digital output (in decimal) is: 𝐷𝑜𝑢𝑡 = =
𝑆𝑡𝑒𝑝 𝑠𝑖𝑧𝑒 𝑉𝑟𝑒𝑓
• For an 8-bit ADC, digital output is D7 – D0 and for a 10-bit ADC D9 – D0
7
Analog noise cancelling techniques
• Digital circuitry inside and outside the device generate noise, which might affect
the accuracy of analogue measurements
• If conversion accuracy is critical, the noise level can be reduced by applying the
following techniques:
o Keep analogue signal paths as short as possible
o The AVCC pin on the device should be connected to the digital VCC supply voltage via
an LC network as shown in the next slide
8
9
Registers Associated with the ADC
• There are registers associated with the ADC, namely, ADC Multiplexer Selection
Register (ADMUX), ADC Control and Status Register A (ADCSRA), ADC Control and
Status Register B (ADCSRB) and ADC Data Register (ADCL and ADCH)
10
• AREF is set externally
• AVCC is the same as Vcc
• Internal 1.1V is fixed regardless of Vcc value
11
Bits 3:0 – MUX3:0: Analog Channel Selection Bits
• The value of these bits selects which analogue input is connected to the ADC, as
shown in the table below (1000 is for internal temperature sensor):
12
ADCSRA – ADC Control and Status Register A
14
• When one is written to ADATE, the ADC will start a conversion on a positive edge
of the selected trigger signal ADTS
• The trigger source is selected by setting the ADC Trigger Select bits, ADTS in
ADCSRB
15
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
16
17
18
ADCSRB – ADC Control and Status Register B
20
ADCL and ADCH – The ADC Data Register
• When an ADC conversion is complete, the result is found in these two registers
21
• When ADCL is read, the ADC Data Register is not updated until ADCH is read
• ADCL must be read first, then ADCH
Example 1
Write a C program to repeatedly read Channel 0 (ADC0) of the ADC and display the
result on PORTB and PORTD
23
Solution
#include <avr/io.h>
#define F_CPU 16000000
int main()
{
DDRB = 0xFF; //make PORTB output
DDRD = 0xFF; //make PORTD output
DDRC = 0x00; //make PORTC input for ADC
ADCSRA = 0x87; //enable ADC and select CK/128
ADMUX = 0xC0; //1.1V Vref, ADC0, right justified
while(1)
{
ADCSRA |= (1 << ADSC); //start conversion
while ((ADCSRA & (1 << ADIF)) == 0); //wait for conversion to finish
ADCSRA |= (1 << ADIF); //clear ADIF bit
PORTD = ADCL; //place the low byte to PORTD
PORTB = ADCH; //place the high byte to PORTB
}
return 0;
} 24
Using Full 10-bit Conversion Result
• To use 10-bits result (ADLAR = 0), we can do using one of the two methods:
Method 1:
• Combine ADCL and ADCH as illustrated below:
int val, Lval, Hval;
Lval = ADCL; //read ADCL first
Hval = ADCH; //read ADCH
ADCval = Hval << 8 + Lval; //shift high value 8 times
Method 2:
• Simply use ADC (omitting L/H) as illustrated below:
int ADCval;
ADCval = ADC; //shorter way
25
Programming the ADC using interrupts
• To avoid tying down the microcontroller, interrupts can be used
• To program the ADC using interrupts, the ADIE bit must be set HIGH
• When the ADIF bit changes to HIGH upon completion of conversion, if the ADIE
bit is HIGH, the CPU will jump to the ADC interrupt handler
• The solution of Example 1 is shown in the next slide using the interrupts method
26
#include <avr/io.h>
#define F_CPU 16000000
#include <avr/interrupt.h>
ISR (ADC_vect)
{
PORTD = ADCL; //place the low byte to PORTD
PORTB = ADCH; //place the high byte to PORTB
ADCSRA |= (1 << ADSC); //start conversion
}
int main()
{
DDRB = 0xFF; //make PORTB output
DDRD = 0xFF; //make PORTD output
DDRC = 0x00; //make PORTC input for ADC
ADCSRA = 0x8F; //enable ADC, enable ADC interrrupt and select CK/128
ADMUX = 0xC0; //1.1V Vref, ADC0, right justified
ADCSRA |= (1 << ADSC); //start conversion
sei(); //enable interrupts
while(1); //wait forever
return 0;
}
27
4. Signal Conditioning
• Most common transducers produce an output in the form of voltage, current,
charge, capacitance and resistance
• These signals must be converted to voltage in order to send input to an ADC
• This conversion is called signal conditioning
• Signal conditioning can be current-to-voltage conversion or signal prescalling
(amplification or attenuation)
• For example, a thermistor changes resistance with temperature and the change
of resistance must be translated into voltage that can be used by an ADC
28