EFM32 ADC使用例程
Posted 东东Donny
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EFM32 ADC使用例程相关的知识,希望对你有一定的参考价值。
/************************************************************
** 函数名称: 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使用例程的主要内容,如果未能解决你的问题,请参考以下文章