Você está na página 1de 46

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)

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:

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.
2.
3.
4.

ADMUX: Analog to Digital conversion MUltipleXer


ADCSRA: Analog to Digital Control and Status Register
ADCL: Analog to Digital Conversion data register(Low)
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

MUX2
0
0
0
0
1
1
1
1

MUX1
0
0
1
1
0
0
1
1

MUX0
0
1
0
1
0
1
0
1

CHANNEL
ADC0
ADC1
ADC2
ADC3
ADC4
ADC5
ADC6
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
0
0

REFS0
0
1

1
1

0
1

Vref
Aref, Internal reference off
AVcc with external capacitor at
Aref
Reserved
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 {07}, ADCH {8---15})

If ADLAR = 0,
The 10-bit converted value is stored as

15
ADC7
7

14

13

ADC6
6

ADC5
5

12
ADC4
4

11
ADC3
3

If ADLAR = 1,
The 10-bit converted value is stored as

15
ADC9
ADC1
7

14
ADC8
ADC0
6

13
ADC7
5

12
ADC6
4

11
ADC5
3

10
ADC2
2

9
ADC9
ADC1
1

8
ADC8
ADC0
0

10
ADC4

9
ADC3

8
ADC2

ADCSRA:
Stores the control and status bits of the ADC.
BIT7
ADEN

BIT6
ADSC

BIT5
ADFR

BIT4
ADIF

BIT3
ADIE

BIT2
ADPS2

BIT1
ADPS1

BIT0
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.

ADPS2

ADPS1

ADPS0

0
0
0
0
1
1
1
1

0
0
1
1
0
0
1
1

0
1
0
1
0
1
0
1

Division
Factor
2
2
4
8
16
32
64
128

Example: System Clock is 1MHz,


Then Prescaler selected is 8
(1,000,000/8) = 125 KHz,
ADPS2

ADPS1

ADPS0

Division
Factor
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.

PROGRAMMING:
For Standard programming we choose AVR Studio 4
Getting started in C programming,
Start AVR Studio 4,

Choose New Project,

Choose AVR-GCC for C Programming,

Enter your Project name, Location of file to be saved.

Select AVR Simulator,

Select Microcontroller (ATmega16 in this case),

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!!!

Start your main function,

Type your code

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,

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

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() {

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

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)

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

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

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

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

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
Youll notice this

Expand the required registers, AD_Converter and PORTC in this case

The expanded view will be like this

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

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

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
Lets get started with proteus Isis
Open PROTEUS ISIS

Youll notice the program like this

Now lets know the toolbars of ISIS

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,

Enter the required component in the keywords

For example let us enter resistor

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

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

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

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

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 lets 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

Você também pode gostar