Вы находитесь на странице: 1из 7

GRAIG

Robotics and Automation Initiative Group


Home GRAIG Resources Workshops Forums Events Links

ADC in AVR
AVR microcontrollers are one of those microcontrollers which are easy to get up and running. What makes it a lucrative choice is the power, availability and its price. One of the features that make it quite useful is the Analog to Digital Converter. ADC is a device which converts an analog value to a digital one. The PortA pins of the ATMega16 are multiplexed as the ADC channels. Where can ADCs be used?? Lots and lots and lots of applications. We can use them for distance measurement using an IR LED and a photodiode, light intensity measurement using LDR, temperature measurement using a thermistor etc etc. How does it work?? Eg: I have an 8-bit ADC And the reference voltage for the ADC is 5V. So if the analog input to the ADC is 5V then the digital converted value is 1111 1111 (Decimal 255) If the input is 2.5V the digital output will be 0111 1111 (Decimal 127) As with all features of a microcontroller to use the ADC too we would need to set some bits in relevant registers. With great power comes great responsibility or maybe in this case we should say complexity. And with this increasing complexity we get a lot of flexibility. In short the ATMega16 has a lot of modes and settings to use the ADC. Over here we shall get started with knowing how to get a digital reading from any of the 8 channels present. Starting with the basic lets have a look at the features of the ADC 10-bit resolution 8 channel Adjustable reference voltage Gain adjustment Some basic guidelines for using the ADC. -Connect AVCC +5V. -Connect a .22 micro farad capacitor from AVCC pin to the ground

-We have an option of using an external or internal reference voltage. If using external reference voltage, then it has to be applied at the AREF pin. Lets define our task here: Convert an analog voltage applied to an ADC channel to digital format To do this we need to - Initialize the ADC according to our requirements, - Start the conversion - Read the digital value The result of the ADC conversion is stored in the ADCH and ADCL registers. A 10-bit conversion result is stored as shown. ADCH ADCL Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 9 Bit 1 Bit 8 Bit 0

To initialize we will set the appropriate registers starting from the ADMUX register. The ADMUX looks like this. 7 6 REFS1 REFS0 5 ADLAR 4 3 2 1 0 MUX4 MUX3 MUX2 MUX1 MUX0

REFS1 and REFS0 help us to set the voltage reference source. We have options of using the following voltage references 1. Voltage at AREF pin (REFS1=0 & REFS0=0) 2. AVCC as the reference pin (REFS1=0 & REFS0=1) 3. Internal 2.56 Voltage reference (REFS1=1 & REFS0=1) For our purpose we shall set it as 00 where AREF is at 5V. ADLAR bit when set gives us a left adjusted result i.e. we get only the highest 8 bits of the conversion ADLAR=1 ADCH ADCL ADLAR=0 ADCH ADCL Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 9 Bit 1 Bit 8 Bit 0 Bit 9 Bit 1 Bit 8 Bit 0 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2

We set ADLAR bit as 8 bit resolution is good enough for most purposes. And if we were to use all the 10 bits we would have to read 2 registers which would take more time and make the program lengthier. Next we take on the ADCSRA register. 7 ADEN 6 ADSC 5 ADATE 4 3 ADIF ADIE 2 ADPS2 1 ADPS1 0 ADPS0

Set the ADEN bit to enable the ADC. ADIF is the ADC interrupt flag which is set when ADC conversion is complete and ADIE (ADC interrupt enable is set). The ADSC bit is used to start conversion.

ADPS2:0 is used to set the prescaler for the ADC clock. The ADC needs a clock frequency between 50kHz to 200kHz. If we are using a 16MHz crystal we shall set ADPS20 = 111 so that will give us a prescaler of 128 and therefore set the ADC clock to 125kHz. (16M/128 = 128k) Enough of theory. Lets implement this using a C program. Development tools used: Software Winavr A simple hardware setup for using this program is as follows.

R1,R2=10kOhm variable resistor R3=1kOhm C1=10 micro farad electrolytic C2,C4=15 pico farad C3=0.1 micro farad C5=0.1 micro farad Q1=16Mhz crystalProgram:#include<avr/io.h>void init_ADC(); // adc initialisation functionint a2d();void main(){int i;DDRC=0xff; init_ADC();while(1) {

i=a2d(); PORTC=(char)i; //considering only 8 bits out of 10bits of adc } } void init_ADC() { ADMUX=027; //ADC7, Left adjust,AVCC reference voltage ADCSRA=087; //Enable ADC, prescaler 128 } int a2d() { ADCSRA=ADCSRA|(1<<6); while( (ADCSRA&(1<<ADIF))==0); return ADC; } Now vary the pot and see the analog value at the LED.
This entry was posted on Thursday, February 28th, 2008 at 7:45 pm and is filed under AVR. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Like Be the first to like this post.

3 Responses to ADC in AVR


1.

Tom says: November 13, 2008 at 3:40 pm Thanks, you code managed to solve my problem which i had been working on for ages Reply

2.

boko says: March 15, 2010 at 3:13 am Thanks, works well Reply

3.

swharden says: July 27, 2010 at 11:11 pm beautiful!

Reply

Leave a Reply Cancel reply


Top of Form

Enter your comment here...

guest

Guest Log In Log In Log In

Email (required) Your email address will not be published.

Name (required)

Website

You are commenting using your Twitter account. (Log Out)

You are commenting using your Facebook account. (Log Out)


Connecting to %s

Notify me of follow-up comments via email. Notify me of new posts via email.
Post Comment 1307794927 8 0

c248b3e666

Bottom of Form

Top of Form

Search

Search
Bottom of Form

a
Select Category

Recent Posts
PCB Design Method and Software A Good Site About Line Follower PHOENIX..THE PERFECT LINE FOLLOWER Ultimate Line Follower MIN4A

Blog Stats
57,877 hits

Popular
graigroup.files.wordpress swapniljariwala.co.nr/upl homepage.tinet.ie/~ei9gq/ graigroup.files.wordpress homepage.tinet.ie/~ei9gq/ graigroup.files.wordpress triindia.co.in/resources/ triindia.co.in/resources/ expresspcb.com/ExpressPCB

Meta
Register Log in Entries RSS Comments RSS WordPress.com Theme: Contempt by Vault9. Blog at WordPress.com.

Вам также может понравиться