1. 程式人生 > >關於stm32f3xx的ADC學習總結

關於stm32f3xx的ADC學習總結

首先用stm32cube匯出ADC初始化程式。

然後在此基礎上將單通道改為4通道。在此基礎上更改了程式,如下:

void MX_ADC1_Init(ADC_HandleTypeDef *hadc1,char index)
{
  ADC_MultiModeTypeDef multimode;
  ADC_ChannelConfTypeDef sConfig;
    /**Common config 
    */
  hadc1->Instance = ADC1;
  hadc1->Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc1->Init.Resolution = ADC_RESOLUTION_12B;
  hadc1->Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1->Init.ContinuousConvMode = DISABLE;
  hadc1->Init.DiscontinuousConvMode = DISABLE;
  hadc1->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1->Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1->Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1->Init.NbrOfConversion = 1;
  hadc1->Init.DMAContinuousRequests = DISABLE;
  hadc1->Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1->Init.LowPowerAutoWait = DISABLE;
  hadc1->Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  if (HAL_ADC_Init(hadc1) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }
    /**Configure the ADC multi-mode 
    */
  multimode.Mode = ADC_MODE_INDEPENDENT;
  if (HAL_ADCEx_MultiModeConfigChannel(hadc1, &multimode) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }
    /**Configure Regular Channel 
    */
  switch(index)
  {
 case 1:sConfig.Channel = ADC_CHANNEL_1;break;
 case 2:sConfig.Channel = ADC_CHANNEL_2;break;
 case 3:sConfig.Channel = ADC_CHANNEL_3;break;
 case 4:sConfig.Channel = ADC_CHANNEL_4;break;
  }
  
  sConfig.Rank = 1;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(hadc1, &sConfig) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }
}

獲取4路AD值(採集AD程式是自己寫的):

uint32_t Get_AD(uint32_t *ad1,uint32_t *ad2,uint32_t *ad3, uint32_t *ad4)
{
uint32_t data; 
static uint32_t time;

switch(time)
{
case 1: MX_ADC1_Init(&hadc1,time);
HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK)
{
data = HAL_ADC_GetValue(&hadc1);
*ad1=data;
}
else
{
return -1;
}
break;
case 2: MX_ADC1_Init(&hadc1,time);
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK)
{
data = HAL_ADC_GetValue(&hadc1);
*ad2=data;
}
else
{
return -1;
}
break;
case 3: MX_ADC1_Init(&hadc1,time);
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK)
{
data = HAL_ADC_GetValue(&hadc1);
*ad3=data;
}
else
{
return -1;
}
break;
case 4: MX_ADC1_Init(&hadc1,time);
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK)
{
data = HAL_ADC_GetValue(&hadc1);
*ad4=data;
}
else
{
return -1;
}
time=0;
break;
}
time++;
if(time==1)
return 1;
else return -1;
}

剛開始時,初始化時4路ADC只能採集到一路。後來總結原因可能是因為沒啟用DMA一次掃描多路AD埠,這樣導致初始化後只能採集到一路。所以沒次在採集AD之前需要初始化相對於的AD口。

void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)//初始化GPIO口和ADC的時鐘否則無法啟用ADC

採集的四路的數值:


---------------------------------------------------------------------------------------------------------------------------------                                                                                                                                       如有錯誤,望請指教!