Sie sind auf Seite 1von 18

AVR Atmega

BABotre,Scientist AgriElectronicsGroup, CSIRCentral lElectroEngineering l nicsResearch hInstitute, CSIRCEERI,Pilani. ;bbotre@gmail.com g Email:bhau@ceeri.ernet.in;

TheADCinATmega16
Th ADCin i ATmega16 AT 16has h a10bitresolution. l ti The Thedigitaloutputhasn=10bits.

TheADChas8inputchannels. channels Analogueinputcancomefrom8differentsources. However,itperformsconversionononlyonechannelatatime.

IfdefaultreferencevoltageVref=5Visused. stepsize:5(V)/1024(steps)= 4.88mV. accuracy:2 LSB= 9.76mV.

TheclockrateoftheADCcanbedifferentfromtheCPUclockrate. OneADCconversiontakes13ADCcycles. AnADCprescalerwilldecidetheADCclockrate.

ADC unit - relevant pins

ADC unit Block diagram


ADCSRA to configure the ADC unit ADCMUX to select the input source ADCH/ADCL register i t t to strore digital output

8 Analogue input pins

ADC Unit MajorAspectsofADCunit WhataretherelevantADCregisters? g a)ADCMUX b)ADCH/ADCL c)ADCCSRA d)SFIOR WhatarethestepstousetheADC? HowtousetheADCinterrupt?

ADC multiplexer selection register (ADCMUX)

MUX4-0: Select analogue channel and gain

ADC Left Adjust Result: 1 to left adjust the ADC result

Reference Selection bits: To select reference voltage

ReferencevoltageVrefcanbeselectedamong3choices choices. Analogueinputvoltagecanbeselectedamongdifferent pins.Differentialinputandcustomgainfactorcanalsobe chosen. ADLARflagwilldeterminehowthe10bitdigitaloutput willbestoredinoutputregisters

SelectingreferencevoltageVref
T bl :ADCreference Table f voltage lt selection l ti

Usually,mode01isused:AVCC=5Vasvoltage. However,iftheinputvoltagehasadifferentdynamicrange However range,wecan usemode00toselectanexternalreferencevoltage.

Selectinginputsourceandgainfactor
Table:ADCinputsource

Analogueinputvoltagecan beselectedas
8ADCpinsADC7toADC0, thedifferentialinput betweentwoofADCpins.

Againfactorof1, 1 10or 200canbeselectedfor differentialinput.

ADCLeftAdjustflagandADCH/Lregisters

Digitaloutputisstoredintwo8bitregistersADCHand ADCL. ADCL TheformatofADCHandADCLareinterpreteddifferently depending p gonflag gADLAR. Important:Whenretrievingdigitaloutput,registerADCL mustberead

ADCControlandStatusRegister(ADCCSRA)
ADC Prescaler Select bits: next slide ADC Interrupt Enable: 1 to enable ADC interrupt
ADC Interrupt Flag: set to 1 when conversion is completed ADC Auto Trigger Enable: 1 to enable auto-triggering of ADC by certain events

ADC Start Conversion: 1 to start conversion ADC Enable: 1 to enable the ADC unit

ADCunitcanoperateintwomodes:manualorauto trigger. Inmanual lmode, d settingflag fl ADSC=1will llstart conversion. Inautotriggermode,apredefinedeventwillstart conversion.

ADC Clock
Table:ADCprescalerselection

TheclockoftheADCisobtainedbydividingtheCPUclockanda divisionfactor. Thereare8possibledivisionfactors,decidedbythethreebits {ADPS2,ADPS1,ADPS0} Example: E l Using U i i internal t lclock l kof f1M 1Mzand daADCprescaler l bit bitsof f010 010, theclockrateofADCis:1MHz/4=250Hz.

Special function I/O register (SFIOR)


ADCAutoTriggerSource

ThreeflagsinregisterSFIORspecifytheeventthatwillautotriggeran Ato t Dconversion i

Steps to use the ADC


St 1: 1 Configure C fi th ADCusing i registers i t ADMUX, ADMUX ADCSRA, ADCSRA SFIOR. SFIOR Step the WhatistheADCsource? Whatreferencevoltagetouse? Alignleftorrighttheresultin{ADCH,ADCL}? EnableordisableADCautotrigger? EnableordisableADCinterrupt? WhatistheADCprescaler? Write1toflagADSC(registerADCCSRA). WaituntilflagADSCbecomes0. R dresult Read ltf fromregisters i t ADCLand dthen th ADCH. ADCH

Step2:StartADCoperation Step3:ExtractADCresult

Programming ADC
Step1:ConfiguretheADC WhatistheADCsource? Whatreferencevoltagetouse? Alignleftorright? EnableordisableADCautotrigger? EnableordisableADCinterrupt? Whatistheprescaler? Disable 4(010) ADC1(pinA.1) AVCC=5V Left,top8bitinADCH Disable

Steps2and3:ShownextintheCprogram.

Adc.c
#include<avr/io.h> / int main(void) { unsignedcharresult; //ConfiguretheADCmoduleoftheATmega16 ADMUX=0b01100000; //REFS1:0=01>AVCCasreference, //ADLAR=1>Leftadjust //MUX4:0=00000>ADC0asinput ADCSRA=0b10000001; //ADEN=1:enableADC, //ADSC=0:don'tstartconversionyet //ADATE=0:disableautotrigger, //ADIE=0:disable ADCinterrupt //ASPS2:0=001:prescaler =2 while(1) { //mainloop //StartconversionbysettingflagADSC ADCSRA|=(1<<ADSC); //Waituntilconversioniscompleted while(ADCSRA&(1<<ADSC)){;} //Readthetop8bits,outputtoPORTB result=ADCH; //displayresult } return0; }

Using ADC interrupt

Inthepollingapproachshownpreviously,wemustcheckADSCflagtoknow whenanADCoperationiscompleted. TheADCunitcantriggeraninterruptwhenADCoperationisdone. WeenableADCinterruptthroughADIEflaginregisterADCCSRA. IntheISR,wecanwritecodetoreadregistersADCLandADCH. ADCinterrupt i i isusually ll combined bi dwith i hautotrigger i mode d

ADC interrupt
St 1: 1 Configure C fi th ADC Step the WhatistheADCsource? Whatreferencevoltagetouse? Alignleftorright? EnableordisableADCinterrupt? Whatistheprescaler? Step2:StartADCoperation Step3:InISR,readandstoreADCresult ADC0 AVCC=5V Left,top8bitinADCH

EnableordisableADCautotrigger? Disable Enable 2(fastestconversion)

Steps to use the ADC


#include<avr/io.h> #include<avr/interrupt.h> volatileunsignedcharresult; ISR(ADC_vect) { result=ADCH; }

//Readthetop8bits,andstoreinvariableresult

int main(void) { //ConfiguretheADCmoduleoftheATmega16 ADMUX=0b01100000; //REFS1:0=01>AVCCasreference, //ADLAR=1>Leftadjust //MUX4:0=00000>ADC0asinput ADCSRA=0b10001111; //ADEN=1:enableADC, //ADSC=0:don'tstartconversionyet //ADATE=0:diableautotrigger, //ADIE=1:enableADCinterrupt //ASPS2:0 ASPS2 0=002: 002 prescaler l = 2 sei(); //enableinterruptsystemglobally while(1) { //mainloop ADCSRA| |=( (1<<ADSC); ); //startaconversion //Displayresult } return0; }

Das könnte Ihnen auch gefallen