cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303 ADC Issue.

markbell9
Associate II
Posted on September 19, 2016 at 20:31

I�m having difficulty getting the ADC to work on the F303 board in the the value returned is always 0.    The input line is PA0,  ADC1,  Chanel 1.

The STM32cubeMX generated initialization code is as follows:

  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.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;

  HAL_ADC_Init(&hadc1);

  This function returns HAL_OK

  sConfig.Channel = ADC_CHANNEL_1;

  sConfig.Rank = 1;

  sConfig.SingleDiff = ADC_SINGLE_ENDED;

  sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;

  sConfig.OffsetNumber = ADC_OFFSET_NONE;

  sConfig.Offset = 0;

  HAL_ADC_ConfigChannel(&hadc1, &sConfig);

  This function returns HAL_OK

The following code is in HAL_ADC_MspInit() in stm32f3xx_hal_msp.c:

void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(hadc->Instance==ADC1)

  {

  /* USER CODE BEGIN ADC1_MspInit 0 */

  /* USER CODE END ADC1_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_ADC12_CLK_ENABLE();

 

    /**ADC1 GPIO Configuration    

    PA0     ------> ADC1_IN1

    */

    GPIO_InitStruct.Pin = DEFROSTER_SENSE_Pin;

    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(DEFROSTER_SENSE_GPIO_Port, &GPIO_InitStruct);

  /* USER CODE BEGIN ADC1_MspInit 1 */

  /* USER CODE END ADC1_MspInit 1 */

  }

}

My normal (periodic) processing:

 

  HAL_ADC_Start(&hadc1);

  This function returns HAL_OK

   HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

   This function returns HAL_OK

    nADCValue = HAL_ADC_GetValue(&hadc1);  

    This function always returns 0.

Help appreciated.

#stm32f303-adc
2 REPLIES 2
Amel NASRI
ST Employee
Posted on September 20, 2016 at 11:04

Hi bell.mark,

I suggest you refer to some ADC examples available in the STM32CubeF3 package.

If the example is OK for you, you may compare your own project with the one provided in the Cube package.

You find them under STM32Cube_FW_F3_V1.6.0\Projects\your_selected_board\Examples\ADC.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

markbell9
Associate II
Posted on September 20, 2016 at 14:52

Thanks Mayla -- for some reason the example folder was not in my current folder.   I have downloaded the latest software.