Skip to main content
Associate II
December 8, 2023
Solved

ADC on H7 series

HI,

I am trying to get ADC read but the result is only the first read repeatedly even I change the voltage on ADC channel input it remains the same, 

ADC Innitialization

static void MX_ADC3_Init(void)

{

 

/* USER CODE BEGIN ADC3_Init 0 */

 

/* USER CODE END ADC3_Init 0 */

 

ADC_ChannelConfTypeDef sConfig = {0};

 

/* USER CODE BEGIN ADC3_Init 1 */

 

/* USER CODE END ADC3_Init 1 */

 

/** Common config

*/

hadc3.Instance = ADC3;

hadc3.Init.Resolution = ADC_RESOLUTION_16B;

hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE;

hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

hadc3.Init.LowPowerAutoWait = DISABLE;

hadc3.Init.ContinuousConvMode = ENABLE;

hadc3.Init.NbrOfConversion = 1;

hadc3.Init.DiscontinuousConvMode = DISABLE;

hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc3.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;

hadc3.Init.Overrun = ADC_OVR_DATA_PRESERVED;

hadc3.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;

hadc3.Init.OversamplingMode = DISABLE;

if (HAL_ADC_Init(&hadc3) != HAL_OK)

{

Error_Handler();

}

 

/** Configure Regular Channel

*/

sConfig.Channel = ADC_CHANNEL_9;

sConfig.Rank = ADC_REGULAR_RANK_1;

sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

sConfig.SingleDiff = ADC_SINGLE_ENDED;

sConfig.OffsetNumber = ADC_OFFSET_NONE;

sConfig.Offset = 0;

sConfig.OffsetSignedSaturation = DISABLE;

if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN ADC3_Init 2 */

 

/* USER CODE END ADC3_Init 2 */

 

}

 

Main Loop

 

while(1)

{

 

HAL_ADC_Start(&hadc3);

 

uint16_t currentADCValue=HAL_ADC_GetValue(&hadc3);

 

 

 

sprintf(disp,"D1: %d\n\r",currentADCValue);

HAL_UART_Transmit(&huart8, (uint8_t*)disp, sizeof(disp), HAL_MAX_DELAY);

}

    This topic has been closed for replies.
    TDK
    TDKBest answer
    December 8, 2023
    • Disable data cache, or manage it properly.
    • ADC data should be a uint16_t.
    • Wait for the ADC to be complete before sending out data. A small delay would be sufficient.

    See a working example using HAL_ADC_DMA_Start here:

    https://github.com/STMicroelectronics/STM32CubeH7/blob/96dd7e5b69d2b10406ac3f358e44b14cececa611/Projects/NUCLEO-H743ZI/Examples/ADC/ADC_DMA_Transfer/Src/main.c

     

    "If you feel a post has answered your question, please click ""Accept as Solution""."

    4 replies

    Associate III
    December 8, 2023

    while(1)

    {

     

    HAL_ADC_Start(&hadc3);

    HAL_ADC_PollForConversion(&hadc3,HAL_MAX_DELAY);

    uint16_t currentADCValue=HAL_ADC_GetValue(&hadc3);

     

     

     

    sprintf(disp,"D1: %d\n\r",currentADCValue);

    HAL_UART_Transmit(&huart8, (uint8_t*)disp, sizeof(disp), HAL_MAX_DELAY);

    }

    Associate II
    December 8, 2023

    That worked for me, with continuous mode disabled, but when I try to read two and continuous mode enabled and scan conversion mode enabled and with DMA to circular and full word setting I get only zeros. 

    the ranking for channels and regular conversions is enabled.

    while(1)

    {

    uint32_t currentADCValue[2]={0};

    HAL_ADC_DMA_Start(&hadc3,currentADCValue,2);

    sprintf(disp,"D1: %ld D2: %ld\n\r",currentADCValue[0],currentADCValue[1]);

    HAL_UART_Transmit(&huart8, (uint8_t*)disp, sizeof(disp), HAL_MAX_DELAY);

    }

     

    TDK
    TDKBest answer
    December 8, 2023
    • Disable data cache, or manage it properly.
    • ADC data should be a uint16_t.
    • Wait for the ADC to be complete before sending out data. A small delay would be sufficient.

    See a working example using HAL_ADC_DMA_Start here:

    https://github.com/STMicroelectronics/STM32CubeH7/blob/96dd7e5b69d2b10406ac3f358e44b14cececa611/Projects/NUCLEO-H743ZI/Examples/ADC/ADC_DMA_Transfer/Src/main.c

     

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    ST Technical Moderator
    December 8, 2023

    Hello @anonymous_stm 

    It should be HAL_ADC_Start_DMA instead of HAL_ADC_DMA_Start

    To use correctly HAL_ADC_DMA_Start() function, you need to check its definition. 

    HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)

    * @param pData Destination Buffer address.

    To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL