STM32LxxADC单通道多次转换代码分析(非DMA)

stm32l0c8xADC特性如下:

有多达16 个通道和3个内部通道(温度传感器、参考电压),PA0、PA4、PA5为高速通道,其他为标准通道。

有单次转换模式和扫描模式,扫描模式下对选定的通道自动进行扫描转换。

ADC频率独立于CPU频率外,即使CPU速度低,ADC也可以达到允许最大采样速率1.14 MSPS。 

能耗很低频率(10kSPS,25μA;1MSPS,200μA)。 

自动关闭功能保证ADC在有效转换阶段期间外能够关闭。

可由DMA控制器控制,工作电压可低至1.65v

模拟看门狗功能

等等。。。

以下是基于上述mcu的ADC单通道多次转换(非DMA)代码,代码已经通过调试,欢迎指正交流。

1、声明相关变量 

float ADC_ConvertedValueLocal;
uint32_t ADC_ConvertedValue;
uint32_t DMA_Transfer_Complete_Count=0;
ADC_HandleTypeDef hadc;
IWDG_HandleTypeDef hiwdg;

2、ADC初始化,需要初始化ADC配置及ADC所用的GPIO通道

      本文选用PA1引脚作为采样输入引脚:

/*
   *  Instance                  = ADC1.
   *  OversamplingMode          = Disabled
   *  ClockPrescaler            = PCLK clock with no division.
   *  LowPowerAutoPowerOff      = Disabled (For this exemple continuous mode is enabled with software start)
   *  LowPowerFrequencyMode     = Enabled (To be enabled only if ADC clock is lower than 2.8MHz) 
   *  LowPowerAutoWait          = Disabled (New conversion starts only when the previous conversion is completed)       
   *  Resolution                = 12 bit (increased to 16 bit with oversampler)
   *  SamplingTime              = 7.5 cycles od ADC clock.
   *  ScanConvMode              = Forward 
   *  DataAlign                 = Right
   *  ContinuousConvMode        = Enabled
   *  DiscontinuousConvMode     = Enabled
   *  ExternalTrigConvEdge      = None (Software start)
   *  EOCSelection              = End Of Conversion event
   *  DMAContinuousRequests     = ENABLE
   */
static void MX_ADC_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
    */
  hadc.Instance = ADC1;
  hadc.Init.OversamplingMode = DISABLE;
  hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = ENABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.DMAContinuousRequests = ENABLE;
  hadc.Init.EOCSelection = ADC_EOC_SINGLE_SEQ_CONV;
  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = DISABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    Error_Handler();
  }


    /**Configure for the selected ADC regular channel to be converted. 
    */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }


}

3、GPIOA与ADC初始化后,即可在main中调用函数获得转化后的值

void get_ADC_value()  
{    int  i;  
     float dat[6], ad_ch1;   
     /* ###  Start conversion ################################# */
     HAL_ADC_Start_IT(&hadc);    
     for(i=0;i<4;i++)    
     {	
       dat[i]  =(float)ADC_ConvertedValue*3.3/4096;
     }  
        ad_ch1=(dat[0]+dat[1]+dat[2]+dat[3])/4 ;   
         ADC_ConvertedValueLocal=ad_ch1;
 } 
/**  * AD转换结束回调函数  */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{ 
   ADC_ConvertedValue=HAL_ADC_GetValue(hadc);
}  



 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(STM32L0xx)