cancel
Showing results for 
Search instead for 
Did you mean: 

ADC reread problem

KOPRO
Associate III

Hi, the charging logic is implemented based on ADC values and I've designed the logic to capture the peak value from the PWM waveform using an oscilloscope. The main logic works fine during the first charging cycle, but when I try to charge for the second time, there's a period where the ADC value drops below 100.

Here are the states:

main_state 0: idle
main_state 4: charging
main_state 5: charging complete
Initially, when the ADC voltage value drops to around 1800, the NFC card is tagged to start charging. The charging stops when the ADC value hits 1800 again, and the state returns to idle (main_state 0).

In the idle state, after tagging the NFC card and the ADC value goes to around 1800, the ADC value doesn't go above 40, even though the scope shows that the voltage is normal. The expected ADC value should be above 1000, but it doesn't rise above 40.

What could be the reason for this issue? Should I reset the ADC to read the correct values, or is there another problem?

 ---------------------------------

ADC read Code

void adcRead(ST_ADC_DATA *p_adcData){  ADC_ChannelConfTypeDef sConfig = {0};//Votage  sConfig.Channel = ADC_CHANNEL_12;  sConfig.Rank = 1;  sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){Error_Handler();}HAL_ADC_Start(&hadc1);HAL_ADC_PollForConversion(&hadc1, 1000);  p_adcData->voltage = HAL_ADC_GetValue(&hadc1);HAL_ADC_Stop(&hadc1);osDelay(10);//Temp  sConfig.Channel = ADC_CHANNEL_6;  sConfig.Rank = 1;if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){Error_Handler();}HAL_ADC_Start(&hadc1);HAL_ADC_PollForConversion(&hadc1, 1000);  p_adcData->temp = HAL_ADC_GetValue(&hadc1);HAL_ADC_Stop(&hadc1);return;}

 

ADC init code

/* USER CODE BEGIN Header *//**
  ******************************************************************************
  * @file    adc.c
  * @brief   This file provides code for the configuration
  *          of the ADC instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  *//* USER CODE END Header *//* Includes ------------------------------------------------------------------*/#include "adc.h"/* USER CODE BEGIN 0 *///ADC1 IN6 = PA6, Temp Seonsor Read//ADC1 IN12 = PC2, Voltage Level Read/* USER CODE END 0 */
ADC_HandleTypeDef hadc1;/* ADC1 init function */void MX_ADC1_Init(void){/* USER CODE BEGIN ADC1_Init 0 *//* USER CODE END ADC1_Init 0 */
  ADC_ChannelConfTypeDef sConfig = {0};/* USER CODE BEGIN ADC1_Init 1 *//* USER CODE END ADC1_Init 1 *//** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */  hadc1.Instance = ADC1;  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;  hadc1.Init.Resolution = ADC_RESOLUTION_12B;  hadc1.Init.ScanConvMode = 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.State = HAL_ADC_STATE_RESET;if (HAL_ADC_Init(&hadc1) != HAL_OK){Error_Handler();}/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  *///  sConfig.Channel = ADC_CHANNEL_12;//  sConfig.Rank = 1;//  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;//  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)//  {//    Error_Handler();//  }//  sConfig.Channel = ADC_CHANNEL_6;//  sConfig.Rank = 2;//  sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;//  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)//  {//    Error_Handler();//  }/* USER CODE BEGIN ADC1_Init 2 *///ADC1 IN6 = PA6, Temp Seonsor Read//ADC1 IN12 = PC2, Voltage Level Read/* USER CODE END ADC1_Init 2 */}void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle){
  GPIO_InitTypeDef GPIO_InitStruct = {0};if(adcHandle->Instance==ADC1){/* USER CODE BEGIN ADC1_MspInit 0 *//* USER CODE END ADC1_MspInit 0 *//* ADC1 clock enable */__HAL_RCC_ADC1_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**ADC1 GPIO Configuration
    PC2     ------> ADC1_IN12
    PA6     ------> ADC1_IN6
    */    GPIO_InitStruct.Pin = GPIO_PIN_2;    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;    GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    GPIO_InitStruct.Pin = GPIO_PIN_6;    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;    GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USER CODE BEGIN ADC1_MspInit 1 *///ADC1 IN6 = PA6, Temp Seonsor Read//ADC1 IN12 = PC2, Voltage Level Read/* USER CODE END ADC1_MspInit 1 */}}void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle){if(adcHandle->Instance==ADC1){/* USER CODE BEGIN ADC1_MspDeInit 0 *//* USER CODE END ADC1_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_ADC1_CLK_DISABLE();/**ADC1 GPIO Configuration
    PC2     ------> ADC1_IN12 / Voltage Level Read
    Votage ADC Range =
    	1260~1270 = 6V
    	1840~1850 = 9V
    	2300~Over = 12V
    PA6     ------> ADC1_IN6 / Temp Seonsor Read
    */HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2);HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6);/* USER CODE BEGIN ADC1_MspDeInit 1 *///ADC1 IN6 = PA6, Temp Seonsor Read//ADC1 IN12 = PC2, Voltage Level Read/* USER CODE END ADC1_MspDeInit 1 */}}/* USER CODE BEGIN 1 *//* USER CODE END 1 */

 

2 REPLIES 2
SofLit
ST Employee

Hello,

Please be more descriptive about your problem otherwise nobody can help you.

A gentle reminder: https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

Thank you.

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.
Sarra.S
ST Employee

Hello @KOPRO

I noticed that you edited your original post to delete it, If you've found a solution to your question, we would greatly appreciate it if you could share it with the community, your experience could benefit others who might encounter a similar issue!

Thank you!  

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.