1. 程式人生 > >EFM32片內外設--ADC 基本例程

EFM32片內外設--ADC 基本例程

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();
  }
}