adc.docx
《adc.docx》由会员分享,可在线阅读,更多相关《adc.docx(16页珍藏版)》请在冰豆网上搜索。
![adc.docx](https://file1.bdocx.com/fileroot1/2022-12/7/ed572242-6f96-4375-9d63-7d53aebc0d2e/ed572242-6f96-4375-9d63-7d53aebc0d2e1.gif)
adc
//-----------------------------------------------------------------------------
//F12x_ADC0_ExternalInput_Mux.c
//-----------------------------------------------------------------------------
//Copyright2005SiliconLaboratories,Inc.
//
//
//Thiscodeexampleillustratesusingtheinternalanalogmultiplexerto
//measureanalogvoltagesonupto8differentanaloginputs.Resultsare
//printedtoaPCterminalprogramviatheUART.
//
//Theinputsaresequentiallyscanned,beginningwithinput0(AIN0.0),up
//toinputnumber-1(maximumANALOG_INPUTS=8,whichwill
//scanallanaloginputsAIN0.0-AIN0.7).
//
//
//ADCSettlingTimeRequirements,SamplingRate:
//----------------------------------------------
//
//Thetotalsampletimeperintputiscomprisedofaninputsettingtime
//(Tsettle),followedbyaconversiontime(Tconvert):
//
//Tsample=Tsettle+Tconvert
//
//Settlingandconversiontimesmayoverlap,astheADCholdsthevalueonce
//conversionbegins.Thiscodeexampletakesadvantageofthistoincrease
//thesettlingtimeabovetheminimumrequired.Inotherwords,when
//convertingthevaluefromanaloginputAin(n),theinputmuxisswitched
//overtothenextinputAin(n+1)tobeginsettling.
//
//|--------SettlingAin(n)--------|=ConversionAin(n)=|
//|--------SettlingAin(n+1)--------|=ConversionAin(n+1)=|
//|--------SettlingAin(n+2)--------|
//ISR:
Timer2^^^
//ISR:
ADC0^^
//
//TheADCinputvoltagemustbeallowedadequatetimetosettlebeforethe
//conversionismade.Thissettlingdependsontheexternalsource
//impedance,internalmuximpedance,andinternalcapacitance.
//Settlingtimeisgivenby:
//
//|2^n|
//Tsettle=ln|---|*Rtotal*Csample
//|SA|
//
//Inthisapplication,assumea100kohmpotentiometerasthevoltagedivider.
//Theexpressionevaluatesto:
//
//|2^12|
//Tsettle=ln|----|*105e3*10e-12=10.2uS
//|0.25|
//
//Inaddition,onemustallowatleast1.5uSafterchanginganalogmux
//inputsorPGAsettings.Thesettlingtimeinthisexample,then,is
//dictatedbythelargeexternalsourceresistance.
//
//Theconversionis16periodsoftheSARclock.At2.5MHz,
//thistimeis16*400nS=6.4uS.
//
//
//Tsample,minimum=Tsettle+Tconvert
//=10.2uS+6.4uS
//=16.6uS
//
//Timer2issettochangethemuxinputandstartaconversionevery20uS.
//
//General:
//--------
//
//Thesystemisclockedusingtheinternal24.5MHzoscillatormultiplied
//upto49MHzbytheon-chipPLL.ResultsareprintedtotheUARTfromaloop
//withtheratesetbyadelaybasedonTimer2.Thisloopperiodicallyreads
//theADCvaluefromaglobalarray,Result.
//
//TheADCmakesrepeatedmeasurementsat20uSintervalsbasedonTimer2.
//TheendofeachADCconversioninitiatesaninterruptwhichcallsan
//averagingfunction.samplesareaveragedthentheResult
//valuesupdated.
//
//Foreachpowerof4of,yougain1bitofeffectiveresolution.
//Forexample,=256gainyou4bitsofresolution:
4^4=256.
//
//TheADCinputmultiplexerissetforasingle-endedinput.Theexample
//sequentiallyscansthroughtheinputs,startingatAIN0.0.
//inputsareread.Theamplifierissetforunitygainsoavoltagerangeof
//0toVref(2.43V)maybemeasured.AlthoughvoltagesuptoVddmaybe
//appliedwithoutdamagingthedevice,onlytherange0toVrefmaybe
//measuredbytheADC.Theinputisavailableatthe8-positionboard-edge
//connector,J20,ontheC8051FX20-TB.
//
//A100kohmpotentiometermaybeconnectedasavoltagedividerbetween
//VREFandAGNDasshownbelow:
//
//---------
//|
//8o|AGND----|
//o|VREF----|<-|
//o|AIN0.1||
//o|||
//o|--------
//o|
//o|
//1o|
//|
//---------
//
//HowToTest:
//
//1)Downloadcodetoa'F12xdevicethatisconnectedtoaUARTtransceiver
//2)ConnectserialcablefromthetransceivertoaPC
//3)OnthePC,openHyperTerminal(oranyotherterminalprogram)andconnect
//totheCOMportatand8-N-1
//4)Connectavariablevoltagesource(between0andVref)
//toAIN0.1,orapotentiometervoltagedividerasshownabove.
//5)HyperTerminalwillprintthevoltagesmeasuredbythedeviceif
//everythingisworkingproperly.Notethatsomeoftheanaloginputsare
//floatingandwillreturnnonzerovalues.
//
//FID:
12X000023
//Target:
C8051F12x
//Toolchain:
KeilC517.50/KeilEVALC51
//CommandLine:
None
//
//
//Release1.0
//-InitialRevision(clm)
//-31-May-06
//
//-----------------------------------------------------------------------------
//Includes
//-----------------------------------------------------------------------------
#include//SFRdeclarations
#include
//-----------------------------------------------------------------------------
//16-bitSFRDefinitionsfor'F12x
//-----------------------------------------------------------------------------
sfr16ADC0=0xbe;//ADC0data
sfr16RCAP2=0xca;//Timer2capture/reload
sfr16RCAP3=0xca;//Timer3capture/reload
sfr16TMR2=0xcc;//Timer2
sfr16TMR3=0xcc;//Timer3
//-----------------------------------------------------------------------------
//GlobalConstants
//-----------------------------------------------------------------------------
#defineBAUDRATE115200//BaudrateofUARTinbps
#defineSYSCLK49000000//OutputofPLLderivedfrom(INTCLK*2)
#defineINT_DEC256//Integrateanddecimateratio
#defineSAR_CLK2500000//DesiredSARclockspeed
#defineSAMPLE_DELAY250//Delayinmsbeforedisplayingsample
#defineANALOG_INPUTS8//NumberofAINpinstomeasure
//(min=1,max=8)
sbitLED=P1^6;//LED='1'meansON
sbitSW1=P3^7;//SW1='0'meansswitchpressed
//-----------------------------------------------------------------------------
//FunctionPrototypes
//-----------------------------------------------------------------------------
voidOSCILLATOR_Init(void);
voidPORT_Init(void);
voidUART1_Init(void);
voidADC0_Init(void);
voidTIMER2_Init(void);
voidADC0_ISR(void);
voidTIMER2_ISR(void);
voidWait_MS(unsignedintms);
//-----------------------------------------------------------------------------
//GlobalVariables
//-----------------------------------------------------------------------------
longResult[ANALOG_INPUTS];//ADC0decimatedvalue,oneforeach
//analoginput
unsignedcharamux_input=0;//indexofanalogMUXinputs
unsignedcharamux_convert=0;
//-----------------------------------------------------------------------------
//main()Routine
//-----------------------------------------------------------------------------
voidmain(void)
{
unsignedchari;
longmeasurement;//measuredvoltageinmV
WDTCN=0xde;//Disablewatchdogtimer
WDTCN=0xad;
OSCILLATOR_Init();//Initializeoscillator
PORT_Init();//InitializecrossbarandGPIO
UART1_Init();//InitializeUART1
TIMER2_Init();//InitializeTimer2tooverflowat1mS
ADC0_Init();//InitADC
SFRPAGE=ADC0_PAGE;
AD0EN=1;//EnableADC
EA=1;//Enableglobalinterrupts
while
(1)
{
EA=0;//Disableinterrupts
SFRPAGE=UART1_PAGE;
printf("\f");
for(i=0;i{
//The12-bitADCvalueisaveragedacrossINT_DECmeasurements.
//TheresultisthenstoredinResult,andisright-justified
//ThemeasuredvoltageappliedtoAIN0.1isthen:
//
//Vref(mV)
//measurement(mV)=---------------*Result(bits)
//(2^12)-1(bits)
measurement=Result[i]*2430/4095;
printf("AIN0.%buvoltage:
%ld\tmV\n",i,measurement);
}
EA=1;//Re-enableinterrupts
Wait_MS(SAMPLE_DELAY);//Waitbeforedisplayingnewvalues
}
}
//-----------------------------------------------------------------------------
//InitializationSubroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
//ReturnValue:
None
//Parameters:
None
//
//Thisfunctioninitializesthesystemclocktousetheinternaloscillator
//at24.5MHzmultipliedbytwousingthePLL.
//
//-----------------------------------------------------------------------------
voidOSCILLATOR_Init(void)
{
intloop;//Softwaretimer
charSFRPAGE_SAVE=SFRPAGE;//SaveCurrentSFRpage
SFRPAGE=CONFIG_PAGE;//SetSFRpage
OSCICN=0x83;//Setinternaloscillatortorun
//atitsmaximumfrequency
CLKSEL=0x00;//Selecttheinternalosc.as
//theSYSCLKsource
//TurnonthePLLandincreasethesystemclockbyafactorofM/N=2
SFRPAGE=CONFIG_PAGE;
PLL0CN=0x00;//Setinternalosc.asPLLsource
SFRPAGE=LEGACY_PAGE;
FLSCL=0x10;//SetFLASHreadtimefor50MHzclk
//orless
SFRPAGE=CONFIG_PAGE;
PLL0CN|=0x01;//EnablePowertoPLL
PLL0DIV=0x01;//SetPre-dividevaluetoN(N=1)
PLL0FLT=0x01;//SetthePLLfilterregisterfor
//areferenceclockfrom19-30MHz
//andanoutputclockfrom45-80MHz
PLL0MUL=0x02;//MultiplySYSCLKbyM(M=2)
for(loop=0;loop<256;loop++);//Waitatleast5us
PLL0CN|=0x02;//EnablethePLL
while(!
(PLL0CN&0x10));//WaituntilPLLfrequencyislocked
CLKSEL=0x02;//SelectPLLasSYSCLKsource
SFRPAGE=SFRPAGE_SAVE;//RestoreSFRpage
}
//-----------------------------------------------------------------------------
//PORT_Init
//-------------------------------------------