0
\$\begingroup\$

I am trying to simply read the value of the PCO ADC on a Nucleo-L476RG Board. I connected a power supply to the PC0 Pin with 2 V and the grounds of the board and the powersupply. When I read the value of the ADC with the code down below I get a value of arround 700. At a reference voltage of 3.3 V and 2 V on the PC0 Pin I should get a value of 2482.

I checked the voltages on the AVDD/VREF, the AGND Pins and the PC0 Pin and the voltage levels where all like they should. So I think it must be a problem with my code.

Can you see what is wrong with my code?

void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  /* MSI is enabled after System reset, activate PLL with MSI as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 40;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLP = 7;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }

  SystemCoreClockUpdate();

  if (HAL_GetREVID() == 0x1001)
      __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
}


HAL_StatusTypeDef adc_init(void)
{
    GPIO_InitTypeDef gpioInit;
    HAL_StatusTypeDef AdcStatus;

    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_ADC_CLK_ENABLE();


    gpioInit.Pin = GPIO_PIN_0;
    gpioInit.Mode = GPIO_MODE_ANALOG;
    gpioInit.Pull = GPIO_PULLDOWN;
    gpioInit.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOC, &gpioInit);

    HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(ADC1_2_IRQn);

    ADC_ChannelConfTypeDef adcChannel;

    AdcHandle.Instance = ADC1;

    AdcStatus = HAL_ADC_DeInit(&AdcHandle);
    if (AdcStatus != HAL_OK)
        return AdcStatus;

    AdcHandle.Init.ClockPrescaler           = ADC_CLOCK_SYNC_PCLK_DIV1;
    AdcHandle.Init.Resolution               = ADC_RESOLUTION_12B;
    AdcHandle.Init.ScanConvMode             = ADC_SCAN_DISABLE;
    AdcHandle.Init.LowPowerAutoWait         = DISABLE;
    AdcHandle.Init.ContinuousConvMode       = ENABLE;
    AdcHandle.Init.DiscontinuousConvMode    = DISABLE;
    AdcHandle.Init.NbrOfDiscConversion      = 0;
    AdcHandle.Init.NbrOfConversion          = 1;
    AdcHandle.Init.ExternalTrigConvEdge     = ADC_SOFTWARE_START;
    AdcHandle.Init.ExternalTrigConv         = ADC_SOFTWARE_START;
    AdcHandle.Init.DataAlign                = ADC_DATAALIGN_RIGHT;
    AdcHandle.Init.NbrOfConversion          = 1;
    AdcHandle.Init.DMAContinuousRequests    = DISABLE;
    AdcHandle.Init.EOCSelection             = DISABLE;
    AdcHandle.Init.Overrun                  = ADC_OVR_DATA_PRESERVED;
    AdcHandle.Init.OversamplingMode         = DISABLE;

    AdcStatus = HAL_ADC_Init(&AdcHandle);

    if (AdcStatus != HAL_OK)
        return AdcStatus;


    adcChannel.Channel          = ADC_CHANNEL_1;
    adcChannel.Rank             = ADC_REGULAR_RANK_1;
    adcChannel.SamplingTime     = ADC_SAMPLETIME_247CYCLES_5;
    adcChannel.OffsetNumber     = ADC_OFFSET_NONE;
    adcChannel.Offset           = 0;
    adcChannel.SingleDiff       = ADC_SINGLE_ENDED;

    AdcStatus = HAL_ADC_ConfigChannel(&AdcHandle, &adcChannel);

    if (AdcStatus != HAL_OK)
        return AdcStatus;


    AdcStatus = ADC_Enable(&AdcHandle);

    if (AdcStatus != HAL_OK)
        return AdcStatus;

    return HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED);
}


uint32_t adc_get_bits(uint32_t Timeout)
{
    uint32_t AdcValue = 0;


    if (HAL_ADC_Start(&AdcHandle) != HAL_OK)
        return 0;

    if (HAL_ADC_PollForConversion(&AdcHandle, Timeout) == HAL_OK)
        AdcValue = HAL_ADC_GetValue(&AdcHandle);
    else
        return 0;

    if (HAL_ADC_Stop(&AdcHandle) != HAL_OK)
        return 0;

    return AdcValue;
}
\$\endgroup\$
  • \$\begingroup\$ If you change the input voltage to the ADC, do you get different results? \$\endgroup\$ – DigitalNinja Nov 11 '17 at 1:47
  • \$\begingroup\$ If I use 2 V as AVDD and 2 V as the voltage that should be measured then i get back a 0 from the HAL_ADC_GetValue function. \$\endgroup\$ – Killerawft Nov 13 '17 at 8:17
  • \$\begingroup\$ When I use the LL Driver for the ADC i get values at around 20. \$\endgroup\$ – Killerawft Nov 13 '17 at 8:24
  • \$\begingroup\$ I solved the problem by using the GPIO_MODE_ANALOG_ADC_CONTROL mode fore the analog GPIO. \$\endgroup\$ – Killerawft Nov 13 '17 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.