EFM32片内外设--ADC 基本例程

来源:互联网 发布:lua脚本语言编程 编辑:程序博客网 时间:2024/05/16 18:05

ADC的最基本的例程。

硬件环境:TG STK, 输入通道选择PD5,TG STK 外扩20pin引脚的第14pin, 即为PD5. 可以用来外接输入电压。  参考电源选择内部的Vdd。

软件环境: IAR

例程:

#include "efm32.h"
#include "efm32_chip.h"
#include "efm32_gpio.h"
#include "efm32_cmu.h"
#include "efm32_adc.h"


#define ADC_Convert_Speed   400000                   //ADC转换速率
#define ADC_Reference           adcRefVDD            //ADC参考电压选择
#define ADC_Input                    adcSingleInpCh5   //ADC输入通道选择
#define ADC_Resolution           adcRes12Bit          //ADC转换分辨率选择

unsigned long ADC_Sensor_Get_ADC(void)
{
    /* Enable clocks required */
    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 200kHz, use default HFPERCLK */
    init.prescale = ADC_PrescaleCalc(ADC_Convert_Speed, 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  = ADC_Reference;
    singleInit.input      = ADC_Input;
    singleInit.resolution = ADC_Resolution;
   
    /* The datasheet specifies a minimum aquisition time when sampling vdd/3 */
    /* 32 cycles should be safe for all ADC clock frequencies */
    singleInit.acqTime = adcAcqTime64;
   
    ADC_InitSingle(ADC0, &singleInit);
   
    unsigned long ulADC_Value = 0;
   
    /* Start ADC convert by Software trigger */
    ADC_Start(ADC0, adcStartSingle);
    /* Wait while conversion is active */
    while (ADC0->STATUS & ADC_STATUS_SINGLEACT) ;
   
    /* Get ADC result */
    ulADC_Value = ADC_DataSingleGet(ADC0);
   
    CMU_ClockEnable(cmuClock_ADC0, false);
   
    return ulADC_Value;
}

int main(void)
{
  CHIP_Init();

  while (1)
  {
      unsigned long ulDelay = 100000;
      while(ulDelay--);
      ADC_Sensor_Get_ADC();
  }
}

 

原创粉丝点击