码迷,mamicode.com
首页 > 其他好文 > 详细

EFM32 ADC使用例程

时间:2018-05-21 23:02:31      阅读:642      评论:0      收藏:0      [点我收藏+]

标签:The   big   should   ini   init   sample   返回值   stat   while   

/************************************************************
** 函数名称: 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使用例程

标签:The   big   should   ini   init   sample   返回值   stat   while   

原文地址:https://www.cnblogs.com/-Donny/p/9069597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!