0% found this document useful (0 votes)
3 views

15. ADC Programming

adc prog

Uploaded by

Schwarz Chimunhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

15. ADC Programming

adc prog

Uploaded by

Schwarz Chimunhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

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

2. Major ADC Characteristics


• Major ADC characteristics are resolution, conversion time, Vref, digital data output,
parallel versus serial ADC type, analogue input channel, and start conversion and
end-of-conversion signals

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

Parallel versus serial ADC


• ADC chips are either parallel (many output pins) or serial (one output pin)
• It means that inside a serial ADC is a parallel-in serial-out shift register for sending
out binary data one bit at a time
• Serial ADCs are becoming widely used due to space being a critical issue

Analogue input channels


• To use one ADC as multiple ADCs (to monitor multiple quantities such as
temperature, pressure, heat, etc.), one ADC is provided with multiple channels for
analogue inputs
• The analogue inputs are multiplexed
5
Start conversion and end-of-conversion signals
• The fact that there are multiple analogue input channels and a single digital
output register creates need for start conversion (SC) and end-of-conversion
(EOC) signals
• When SC is activated, the ADC starts converting the analogue input to an n-bit
digital number
• When the data conversion is complete, the end-of-conversion signal notifies the
CPU that converted data is ready to be picked up

3. ADC Programming in the AVR


• An increasing number of microcontrollers have an on-chip ADC peripheral like
timers and USART
• An on-chip ADC eliminates the need for an external ADC connection
• The ATmega328P comes with an ADC (10-bit successive approximation register)
6
ATmega328P ADC features
• The ADC peripheral of the ATmega328P has the following characteristics:
o It is a 10-bit ADC
o It has 6 analogue input channels
o The converted output binary data is held by two registers ADCH (ADC Result High)
and ADCL (ADC Result Low)
o Since the ADCH:ADCL registers give 16 bits and the ADC data output is only 10-bits,
6-bits of the 16 are unused; we have an option of making either the upper 6-bits or
the lower 6-bits unused
o There are three options for Vref: Vref can be connected to AVCC (Analogue Vcc),
internal 1.1V reference or external AREF pin
o The conversion time is dictated by the crystal frequency connected to the XTAL pins
and ADPS0:2 bits; if 10-bit resolution is needed, 200kHz is the maximum ADC input
clock
o Interrupt on ADC conversion complete

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)

ADMUX – ADC Multiplexer Selection Register

Bit 7:6 – REFS1:0: Reference Selection Bits


• These bits select the voltage reference for the ADC as shown in the table in the
next slide

10
• AREF is set externally
• AVCC is the same as Vcc
• Internal 1.1V is fixed regardless of Vcc value

Bit 5 – ADLAR: ADC Left Adjust Result


• Write one to ADLAR to left adjust the result, otherwise, the result is right
adjusted (default)

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

Bit 7 – ADEN: ADC Enable


• Writing this bit to one enables the ADC
• Writing this bit to zero turns off the ADC even while a conversion is in progress

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 to start the first conversion
• ADSC reads as one as long as conversion is in progress and reads as zero when
conversion is complete
13
Bit 5 – ADATE: ADC Auto Trigger Enable
• Alternative to starting conversion by writing one to ADSC bit, conversion can be
triggered automatically by writing one to ADC Auto Trigger Enable (ADATE) as
shown below:

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

Bit 4 – ADIF: ADC Interrupt Flag


• This bit is set when an ADC conversion is completed 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
• Alternatively, ADIF is cleared by writing a logical one to the flag

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

Bits 2:0 – ADPS2:0: ADC Prescaler Select Bits


• These bits determine the division factor between the system clock frequency and
the input clock to the ADC
• By default the successive approximation ADC requires an input clock frequency of
between 50 kHz and 200 kHz for maximum accuracy for 10-bit resolution
• The ADC module contains a prescaler (as shown in the next slide, Slide 17) to
generate an acceptable clock frequency
• The ADC prescaler selections are as shown in the table in Slide 18

16
17
18
ADCSRB – ADC Control and Status Register B

Bit 6 – ACME: Analog Comparator Multiplexer Enable


• This bit is not used for the ADC, but for the analogue comparator (when the ADC
is switched off, ADEN in ADCSRA is zero)

Bit 2:0 – ADTS2: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 (shown in the table in the next slide) but if ADATE
is cleared, the ADTS2:0 settings will have no effect
• A conversion will be triggered by the rising edge of the selected Interrupt Flag
19
• Switching to free running mode (ADTS[2:0]=0) will not cause a trigger event, even
if the ADC Interrupt Flag is set

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

Steps in programming the ADC using polling


• To program the ADC using polling, the following steps must be taken:
1. Make the pin for the selected ADC channel an input pin
2. Turn ON the ADC module by setting HIGH the ADEN bit in the ADCSRA register
3. Select the ADC clock frequency using ADPS2:0 bits in the ADCSRA register
4. Select the reference voltage using REFS0 and REFS1 bits and select the ADC
input channel using MUX3:0 bits, all in the ADMUX register
5. Start conversion by setting HIGH the ADSC bit in the ADCSRA register
6. Wait for the conversion to be completed by polling the ADIF bit in the ADCSRA
register
22
7. After the ADIF bit has gone HIGH, read the ADCL and ADCH registers to get the
digital data output (read ADCL before ADCH for valid result)
8. To read the selected channel again, go back to Step 5
9. To select another Vref source or input channel, go back to Step 4

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

You might also like