Beruflich Dokumente
Kultur Dokumente
TheADCinATmega16
Th ADCin i ATmega16 AT 16has h a10bitresolution. l ti The Thedigitaloutputhasn=10bits.
ADC Unit MajorAspectsofADCunit WhataretherelevantADCregisters? g a)ADCMUX b)ADCH/ADCL c)ADCCSRA d)SFIOR WhatarethestepstousetheADC? HowtousetheADCinterrupt?
SelectingreferencevoltageVref
T bl :ADCreference Table f voltage lt selection l ti
Selectinginputsourceandgainfactor
Table:ADCinputsource
Analogueinputvoltagecan beselectedas
8ADCpinsADC7toADC0, thedifferentialinput betweentwoofADCpins.
ADCLeftAdjustflagandADCH/Lregisters
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.
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; }
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
//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; }