Download - Analog to Digital Conversion
![Page 1: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/1.jpg)
ANALOG TO DIGITAL CONVERSION
INTRODUCTION:
All quantities in nature are continuous(ANALOG)
Ex: Voltage, Current, Time, etc
As being continuous, they have infinite points
Processing such large number of quantities requires very high processing
power and time
So they are converted into discrete quantity(DIGITAL)
These require less time to process, further more the processor understands
only digital information.
The digital quantity obtained upon conversion consists of discrete levels as shown
The factor that influences the A/D conversion is resolution.
Consider a 8-bit ADC,
It consists of discrete levels of voltages of 28 = 256 levels.
In case of 10-bit ADC,
It consists of discrete levels of voltages of 210
=1024 levels.
As the resolution increases, the approximated value stays closer to the
actual value, in other terms, as the resolution increases the conversion error decreases.
These conversions are implemented using a Microcontroller (ATmega16 for now)
![Page 2: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/2.jpg)
ATmega16:
This is a popular Microcontroller as it has the following features: (FOR ADC)
16 Kb flash memory
1 Kb SRAM
512 Bytes EEPROM
8-channel,10-bit ADC channels
Up to 16MHz operation
Operating Voltage:4.55.5V
HARDWARE OVERVIEW:
ADC in ATmega16:
Converts Analog voltage into a 10-bit digital value.
ADMUX: Register which selects the input channels.
ADCH, ADCL: Registers which store the converted values.
Condition for Correct operation,
0V < VIN < VARef
Where VIN is the input voltage,
VARef is the reference voltage.
ADC converted value = round ((VIN/VARef)*1023).
The range of output values is 0 1023.
A Quantization error is produced due to the conversion from analog values
to digital values.
MODES:
![Page 3: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/3.jpg)
Single Conversion Mode: The A/D conversion should be initiated
each time.
Free Running Mode: The A/D conversion should be initiated at the
beginning and conversion is started previous one is completed.
A/D conversion is not instantaneous; it depends on the clock frequency
used by the ADC.
Conversion Time is inversely related to clock Frequency of ADC.
PRESCALER: Hardware which divides the clock frequency to an
acceptable frequency
(50 KHz 200 KHz).
REGISTER OVERVIEW:
1. ADMUX: Analog to Digital conversion MUltipleXer
2. ADCSRA: Analog to Digital Control and Status Register
3. ADCL: Analog to Digital Conversion data register(Low)
4. ADCH: Analog to Digital Conversion data register(High)
1. ADMUX: Selects the input channels,
As there are 8-channels, it as possible to store it in a 3 registers.
It also stores the reference value.
BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0
REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
MUX3, MUX2, MUX0: Allow you to select the channel when the following set of values
are given
![Page 4: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/4.jpg)
MUX2 MUX1 MUX0 CHANNEL
0 0 0 ADC0
0 0 1 ADC1
0 1 0 ADC2
0 1 1 ADC3
1 0 0 ADC4
1 0 1 ADC5
1 1 0 ADC6
1 1 1 ADC7
If these bits are changed during a conversion, there is no effect till the conversion is
complete.
REFS1, REFS0: Allow the selection of Reference voltage.
REFS1 REFS0 Vref
0 0 Aref, Internal reference off
0 1 AVcc with external capacitor at
Aref
1 0 Reserved
1 1 Internal 2.56V with capacitor at
Aref
ADLAR: (ADc Left Adjustment Register)
This register allows you to adjust the way in which you converted data are to be stored.
(ADCL {0—7}, ADCH {8---15})
If ADLAR = 0,
The 10-bit converted value is stored as
![Page 5: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/5.jpg)
15 14 13 12 11 10 9 8
ADC9 ADC8
ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0
7 6 5 4 3 2 1 0
If ADLAR = 1,
The 10-bit converted value is stored as
15 14 13 12 11 10 9 8
ADC9 ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2
ADC1 ADC0
7 6 5 4 3 2 1 0
ADCSRA:
Stores the control and status bits of the ADC.
BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0
ADEN ADSC ADFR ADIF ADIE ADPS2 ADPS1 ADPS0
ADEN (ADC Enable):
ADEN = 1, If ADC has started,
ADEN = 0, If ADC has stopped.
During conversion process if the ADEN bit is cleared, the ADC is terminated.
ADSC (ADC Start Conversion):
FREE RUNNING MODE: set ADSC to start the conversion, the
next conversions start automatically.
SINGLE CONVERSION MODE: you must set the ADSC to start
each conversion, after a conversion ADSC bit is cleared by hardware. In this mode you
need to set the ADSC for every conversion.
ADFR (ADC Free Running select):
If ADFR = 1; Free Running Mode,
If ADFR = 0; Single Conversion Mode.
ADIF (ADC Interrupt Flag):
ADIF bit is set to one after ADC conversion is finished.
ADIE (ADC Interrupt Enable):
ADIE =1 and when Global Interrupts are enabled,
ADIF is set and corresponding Interrupt is executed.
ADPS (ADC Prescaler Select):
As the ADC requires a clock frequency of 50 KHz to 200 KHz.
The system clock is fed to ADC by dividing it with a Prescaler in
order to adjust it to 50 KHz200 KHz.
![Page 6: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/6.jpg)
ADPS2 ADPS1 ADPS0 Division
Factor
0 0 0 2
0 0 1 2
0 1 0 4
0 1 1 8
1 0 0 16
1 0 1 32
1 1 0 64
1 1 1 128
Example: System Clock is 1MHz,
Then Prescaler selected is 8
(1,000,000/8) = 125 KHz,
ADPS2 ADPS1 ADPS0 Division
Factor
0 1 1 8
ADCL, ADCH:
These registers are used to store the last converted value.
ADCH holds the 2 most significant bits.
ADCL holds the remaining bits.
When ADCL is read, ADC Data register is not updated until
ADCH is also read i.e. both ADCH, ADCL should be read in order to get the value.
ADCL should be read before ADCH.
STEPS IN BRIEF
Select channel by ADMUX register.
Select mode: Free Running or Single conversion mode by selecting the
ADFR bit in ADCSRA register).
Check interrupts (ADIE bit), if interrupt enabled perform the Interrupt
at conversion (ADIF followed by the action).
Set the ADC clock Frequency (50 KHz< f <200 KHz) by adjusting the
Prescaler (ADPS2, ADPS1, ADPS0 in ADCSRA register).
Enable the ADC (ADEN = 1).
Start the conversion by setting the ADSC bit in ADCSRA register.
Wait till the conversion is complete, by reading the ADIF,
If (ADIF = 1) then the conversion is complete.
Store the converted values in ADCL, ADCH registers.
Read ADCL first, then ADCH.
![Page 7: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/7.jpg)
PROGRAMMING:
For Standard programming we choose AVR Studio 4
Getting started in C programming,
Start AVR Studio 4,
Choose New Project,
![Page 8: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/8.jpg)
Choose AVR-GCC for C Programming,
Enter your Project name, Location of file to be saved.
![Page 9: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/9.jpg)
Select AVR Simulator,
Select Microcontroller (ATmega16 in this case),
![Page 10: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/10.jpg)
You get the interface, Type your program in the text editor in center of window.
Include the header file avr/io.h; else your compiler will throw errors!!!
![Page 11: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/11.jpg)
Start your main function,
Type your code
![Page 12: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/12.jpg)
Press F7 to build the Project,
And see the results in the bottom of the screen as
Your compiled program is in the saved folder,
![Page 13: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/13.jpg)
On opening the folder you can see the .c file
Go to the Default folder,
You can find the .hex file; this is to be downloaded into your Microcontroller
![Page 14: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/14.jpg)
PROGRAMMING FOR ADC
Define the clock frequency first in the program by
#define F_CPU xxxxxxxUL
Include the required headers <avr/io.h> in this case
Start your program with void main() {
![Page 15: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/15.jpg)
Enter the ADMUX register value, according to the requirement.
In this case selecting ADC2 channel for ADC input and taking AVcc = Aref
Enter the ADCSRA register value according to the requirement.
In this case, selecting Prescaler of 8, choosing free running mode, enabling the ADC and
starting the ADC with no interrupts enabled
![Page 16: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/16.jpg)
For this demo program, let us display the ADC input value at PORTC, PORTB, the output
would be in a 10-bit binary fashion,
so defining PORTC as ADCL output (Lower 8 Bytes)
![Page 17: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/17.jpg)
defining PORTB as ADCH output (Upper 2 Bytes)
Starting an infinite loop because we want to observe the output until the microcontroller is
given power, if no loop is defined then the ADC would be running but PORTC, PORTB
will not get updated i.e. the PORTC, PORTB will never change the value
Wait for the conversion to complete, when the conversion is complete ADIF flag would be
set to one
![Page 18: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/18.jpg)
Now the converted values are stored in ADCL, ADCH registers.
Displaying the value of registers at PORTC, PORTB.
Remember to read ADCL first and then ADCH.
And read both the registers or else the next converted value will not get stored.
Close the braces
![Page 19: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/19.jpg)
Press F7 to build the program, if errors are present they will be shown in the results.
Warnings can be neglected.
Check for the program size.
Your program is now compiled and stored in .hex format which is ready to be burnt in
your microcontroller.
Now go to the folder where you saved your project
![Page 20: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/20.jpg)
Open the folder and you can see your .c file of your project
Open the default folder
You can now see your .hex file and its size also
![Page 21: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/21.jpg)
Your programming is now complete, but how do you know whether your program is
correct or not???
You will have to simulate the program
You have two ways
AVR Simulator: you can get the register simulation, without the
hardware connected.
Proteus ISIS: you can get the graphical simulation i.e. you can
connect external hardware to it but you cannot know the internal registers status
Therefore we will use both and let us first use AVR simulator
Open the AVR Studio4 and open your project and notice this button in the I/O on the right
of your screen
Press it and you will get a menu, choose
Tree view
You’ll notice this
![Page 22: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/22.jpg)
Expand the required registers, AD_Converter and PORTC in this case
The expanded view will be like this
![Page 23: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/23.jpg)
Now go the debug option
And select start debugging or simply press SHIFT+ALT+CTRL+F5,
You will notice a yellow arrow; this represents your current line under inspection
![Page 24: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/24.jpg)
Press F11 to execute the next line and observe the I/O view and repeat the process of
pressing F11 till your program is complete and notice the registers.
In this case
ADMUX = 01000010 is being displayed
The dark ones represent 1, and the blank ones represent 0
If satisfied continue to simulation in AVR Simulator or else check your program
![Page 25: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/25.jpg)
SIMULATION IN PROTEUS ISIS
Proteus ISIS is a graphical circuit simulator where you can check your results with the
hardware included such as LEDs, motors, sensors, etc
Let’s get started with proteus Isis
Open PROTEUS ISIS
![Page 26: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/26.jpg)
You’ll notice the program like this
Now let’s know the toolbars of ISIS
![Page 27: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/27.jpg)
Important tools:
Component mode: selecting this allows you to place the required components
Voltage Sources: this allows you to select Vcc, Gnd, BiDirectional bus, etc
Graph mode: This allows you to plot graphical visualization of the data simulated
Meter mode: This allows you to select oscilloscope, Virtual terminal, ammeter
(AC/DC), Voltmeter (AC/DC)
For placing the components Press the Component mode and press
You will get a window like this,
![Page 28: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/28.jpg)
Enter the required component in the keywords
For example let us enter resistor
![Page 29: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/29.jpg)
You will get a list of possible components; choose your required one, now choosing a
standard resistor
Press ok and you will get the home screen of Proteus now right click on the screen to
place the component
Now let us do a demo simulation to glow a led,
So choose an ANIMATED LED from the component by pressing p and enter
ANIMATED LED
![Page 30: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/30.jpg)
Place it on the home screen as you did it for the resistor,
Now lets choose a push to on switch, for making the led on or off
Press P and enter SPST Push Button
![Page 31: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/31.jpg)
Place it on the home screen
Now we need Vcc and Gnd to light up the led so,
Now press the SOURCES to enter into voltage sources mode:
You will notice options like this
![Page 32: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/32.jpg)
Choose POWER for Vcc and place it on the screen
The symbol stands for standard +5V supply
Now choose GROUND
And place it on the home screen, and join the circuit by pressing the component, you will
notice a red box (or) red circle right click on it and move your mouse till the next
component and the wire will be joined
![Page 33: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/33.jpg)
Now press the PLAY button on the left bottom of the screen to simulate the circuit
You can now start your simulation
The Play button turns green,
If any errors, it will be displayed in the messages
Now press the push button, and led will glow.
Now let’s simulate our ADC Program which we had written in AVR Studio4
The parts which we need are
ATmega16 microcontroller
potentiometer as the source for variable voltage
8-leds
8 1KΩ resistors
Vcc
Gnd
So enter these components in the search box by pressing P and place it on the home screen
![Page 34: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/34.jpg)
![Page 35: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/35.jpg)
![Page 36: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/36.jpg)
![Page 37: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/37.jpg)
![Page 38: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/38.jpg)
![Page 39: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/39.jpg)
![Page 40: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/40.jpg)
![Page 41: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/41.jpg)
![Page 42: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/42.jpg)
![Page 43: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/43.jpg)
![Page 44: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/44.jpg)
![Page 45: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/45.jpg)
![Page 46: Analog to Digital Conversion](https://reader033.vdocuments.us/reader033/viewer/2022051402/5695d1651a28ab9b02965d91/html5/thumbnails/46.jpg)