Anda di halaman 1dari 3

AnalogtodigitalConversion ThislaboratoryactivityshowshowtoconfiguretheADCmoduleofaPICmicrocontroller.Aftera successfulconfiguration,conversionandreadingofananalogsignalwillnowbepossible.Theanswer fromADCcanberepresentedbyaratioofthevoltagetothereference.

V ADC = Vref

1023

ConvertingtheanswerfromtheADCbacktovoltagerequiressolvingforV.

ADC V= Vref 1023


Twoofthethreefactorsontherightsideoftheequationareconstantsandmaybecalculatedin advance.Thiseliminatestheneedtoactuallydivide,butstillrequiresfixedorfloatingpointmultiplyto solvetheequationonthefly. However,sometimes,suchaswhenreadingasensor,calculatingthevoltageisonlythefirststep.There maybeadditionalmathtocalculatethemeaningfuldatafromthesensor.Forexample,whenreadinga thermistor,calculatingthevoltageisonlythefirststeponthewaytogettingthetemperature. ThereareothermeanstoconvertADCvalues,includingastraighttablelookuporapiecewiselinear interpolation.Eachoftheserepresentsdifferentspeed/memorytradeoffs. ThePIC16F877ahasabuiltinAnalogtoDigitalConverter(ADC)with10bitsresolutiononanyofthe8 channels.TheconvertercanbereferencedtothedevicesVDDoranexternalvoltagereference. Theanaloginputchargesasampleandholdcapacitor.Theoutputofthesampleandhold capacitoristheinputintotheconverter.Theconverterthengeneratesadigitalresultofthisanaloglevel viasuccessiveapproximation.ThisA/Dconversion,oftheanaloginputsignal,resultsinacorresponding 10bitdigitalnumber. Theanalogreferencevoltages(positiveandnegativesupply)aresoftwareselectabletoeither thedevicessupplyvoltages(AVDD,AVss)orthevoltagelevelontheAN3/VREF+andAN2/VREFpins. TheA/DconverterhasauniquefeatureofbeingabletooperatewhilethedeviceisinSLEEP mode. TheA/Dmodulehasfourregisters.Theseregistersare: A/DResultHighRegister(ADRESH) A/DResultLowRegister(ADRESL) A/DControlRegister0(ADCON0) A/DControlRegister1(ADCON1)

TheADCON0register,showninFigure1,controlstheoperationoftheA/Dmodule.TheADCON1 registerconfiguresthefunctionsoftheportpins.Theportpinscanbeconfiguredasanaloginputs(AN3 andAN2canalsobethevoltagereferences)orasdigitalI/O.

Figure1.10bitADCblockdiagram.

Operation: TheADRESH:ADRESLregisterscontainsthe10bitresultoftheA/Dconversion.WhentheA/D conversioniscomplete,theresultisloadedintothisA/Dresultregisterpair,theGO/DONEbit (ADCON0<2>)iscleared,andA/Dinterruptflagbit,ADIF,isset. AftertheA/Dmodulehasbeenconfiguredasdesired,theselectedchannelmustbeacquiredbeforethe conversionisstarted.TheanaloginputchannelsmusthavetheircorrespondingTRISbitsselectedas inputs. SamplingtimeisalsoconfiguredinADCON1register.ADCON1selectstheratiobetweenprocessorclock speedandconversionspeed.ThisisimportantbecausetheADCneedsatleast1.6sperbit.Accuracy degradesiftheclockspeedistoohigh.Astheprocessorclockspeedincreases,anincreasinglylarge dividerisnecessarytokeeptheconversionspeed.FourMHzisfastestat8:1ratiowithaconversion speedof2sperbit.AfterthisacquisitiontimehaselapsedtheA/Dconversioncanbestarted. ItshouldbenotedatthispointthatsinceADCmoduleofPICmicrousesacapacitortoholdthesampled voltage,impedancemustbeconsidered.FormoreinformationaboutacquisitiontimeandA/D acquisitionrequirements,refertothedatasheet.Thefollowingstepsshouldbefollowedfordoingan A/Dconversion: 1. ConfiguretheA/Dmodule: Configureanalogpins/voltagereference/anddigitalI/O(ADCON1) SelectA/Dinputchannel(ADCON0) SelectA/Dconversionclock(ADCON0) TurnonA/Dmodule(ADCON0) 2. ConfigureA/Dinterrupt(ifdesired): CleartheADIFbit SettheADIEbit SettheGIEbit 2

3. Waittherequiredacquisitiontime. 4. Startconversion: SettheGO/DONEbit(ADCON0) 5. WaitforA/Dconversiontocomplete,byeither: PollingfortheGO/DONEbittobeclearedorADIFbittobeset OR WaitingfortheA/Dinterrupt 6. ReadA/DResultregisterpair(ADRESH:ADRESL),cleartheADIFbit,ifrequired. 7. Fornextconversion,gotostep1orstep2asrequired. SampleCode: RefertothesampleMPLABXprojectincluded. Schematic:

Problems: 1. ModifythesoftwaresuchthatitcouldcontrolthePWMdutycycleonCCP1pin.Assumethe circuitbelowasifcontrollingthespeedofthemotor. a. ProvideastepbystepprocedureofhowtosetupPWM. b. Createyourownheaderfile(driver)forPWMusage(ie.Pindefinitions,PWM_init(), PWM_val(),etc.).

Anda mungkin juga menyukai