1. 程式人生 > >EFM32 ADC使用例程

EFM32 ADC使用例程

alc ash efault man nbsp active get relative sin

/************************************************************
** 函數名稱: ADCConfig
** 功能描述: ADC配置
** 輸入參數: NULL
** 輸出參數: NULL
** 返回值: NULL
** 作者: Donny
** 日期: 2018.5.21
**************************************************************/
static void ADCConfig(void)
{
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_ADC0, true);
ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT;

/* Init common settings for both single conversion and scan mode */
init.timebase = ADC_TimebaseCalc(0);
/* Might as well finish conversion as quickly as possibly since polling */
/* for completion. */
/* Set ADC clock to 7 MHz, use default HFPERCLK */
init.prescale = ADC_PrescaleCalc(200000, 0);

/* WARMUPMODE must be set to Normal according to ref manual before */
/* entering EM2. In this example, the warmup time is not a big problem */
/* due to relatively infrequent polling. Leave at default NORMAL, */

ADC_Init(ADC0, &init);

/* Init for single conversion use, measure VDD/3 with 1.25 reference. */
singleInit.reference = adcRefVDD;
singleInit.input = adcSingleInpCh3;
singleInit.resolution = adcRes12Bit;

/* The datasheet specifies a minimum aquisition time when sampling vdd/3 */
/* 32 cycles should be safe for all ADC clock frequencies */
singleInit.acqTime = adcAcqTime32;

ADC_InitSingle(ADC0, &singleInit);
}

/************************************************************
** 函數名稱: ADC_Switch_Channel
** 功能描述: ADC通道切換
** 輸入參數: NULL
** 輸出參數: NULL
** 返回值: NULL
** 作者: Donny
** 日期: 2018.5.21
**************************************************************/
void ADC_Switch_Channel(ADC_SingleInput_TypeDef input)
{
/* Make sure single conversion is not in progress */
ADC0->CMD = ADC_CMD_SINGLESTOP;
/* Clear the input channel */
ADC0->SINGLECTRL &= ~_ADC_SINGLECTRL_INPUTSEL_MASK;
/* Update the input channel */
ADC0->SINGLECTRL |= input << _ADC_SINGLECTRL_INPUTSEL_SHIFT;
}

/************************************************************
** 函數名稱: ADC_Get
** 功能描述: ADC值獲取
** 輸入參數: NULL
** 輸出參數: NULL
** 返回值: NULL
** 作者: Donny
** 日期: 2018.5.21
**************************************************************/
double AverageVoltage;
double ADC_Get()
{
uint32_t sample;
double voltage;
double SumVoltage;
// double AverageVoltage;
int i = 0;
/* Enable clocks required */

ADCConfig();
// ADC_Switch_Channel(adcSingleInpCh3);

ADC_Start(ADC0, adcStartSingle);

/* Wait while conversion is active */
while (ADC0->STATUS & ADC_STATUS_SINGLEACT) ;

/* Get ADC result */
SumVoltage = 0;
for(i=0;i<3;i++)
{
sample = ADC_DataSingleGet(ADC0);
/* Calculate supply voltage */
voltage = (sample * 3.7) / 4096; //Vdd=3.7V
SumVoltage += voltage;
}
AverageVoltage = SumVoltage / 3;
// EMU_EnterEM2(true);
return AverageVoltage;
}

main()

{

  ADC_Get();
{

}

EFM32 ADC使用例程