Skip to main content
MAern.1
Visitor II
July 22, 2020
Question

Unstable ADC readings with STM32F030

  • July 22, 2020
  • 1 reply
  • 581 views

Hello all

I have a problem with unstable ADC readings with my STM32F030

I use CubeMX to set up my device.

My ADC init :

static void MX_ADC_Init(void)
{
 
 /* USER CODE BEGIN ADC_Init 0 */
 
 /* USER CODE END ADC_Init 0 */
 
 ADC_ChannelConfTypeDef sConfig = {0};
 
 /* USER CODE BEGIN ADC_Init 1 */
 
 /* USER CODE END ADC_Init 1 */
 /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
 */
 hadc.Instance = ADC1;
 hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
 hadc.Init.Resolution = ADC_RESOLUTION_10B;
 hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
 hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
 hadc.Init.LowPowerAutoWait = DISABLE;
 hadc.Init.LowPowerAutoPowerOff = DISABLE;
 hadc.Init.ContinuousConvMode = DISABLE;
 hadc.Init.DiscontinuousConvMode = DISABLE;
 hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
 hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 hadc.Init.DMAContinuousRequests = DISABLE;
 hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
 if (HAL_ADC_Init(&hadc) != HAL_OK)
 {
 Error_Handler();
 }
 /** Configure for the selected ADC regular channel to be converted. 
 */
 sConfig.Channel = ADC_CHANNEL_0;
 sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
 sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
 if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /** Configure for the selected ADC regular channel to be converted. 
 */
 sConfig.Channel = ADC_CHANNEL_1;
 if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /** Configure for the selected ADC regular channel to be converted. 
 */
 sConfig.Channel = ADC_CHANNEL_3;
 if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN ADC_Init 2 */
 while(HAL_ADCEx_Calibration_Start(&hadc) != HAL_OK){}; // Calibration of ADC
 /* USER CODE END ADC_Init 2 */
 
}

and here the part in my Main:

(STATE_ADC_channel = ADC_CHANNEL_0)

config_ext_ADC_channel(STATE_ADC_channel,true);
 
 HAL_ADC_Start(&hadc);
LED_RED_ON; 
// HEAT_CHARGE_OFF;	// FET_OFF 
 
 HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
 
 adc_data = HAL_ADC_GetValue(&hadc);
LED_RED_OFF; 
 if(adc_data <= min_state) // Master Pack oder Ladegerät gefunden
 {
 
// HEAT_CHARGE_ON;	// FET_ON 
 detect_pos_pulse++; // count heat pulse on
 }
 else
 {
 detect_neg_pulse++; // count heat pulse off
 }
 
 config_ext_ADC_channel(STATE_ADC_channel,false);

i must read the ADC as fast as possible because of detecting some 500us pulses.

Now if i debug with the breakpoint in line :

if(adc_data <= min_state)

i get this on my scope :

0693W000001tMhzQAE.jpg

UPDATE: better scope picture

Blue is LED_RED on off

Green is my signal to measure.

ADC result switches between 0 and 560 with the exact same scope screen.

Can someone please guide me in the right direction to solve this problem.

Thanks a lot

Markus

This topic has been closed for replies.

1 reply

TDK
July 22, 2020

I'm not seeing a problem. Green signal goes from 0 to about midway, which is about 0 to 500 (with 10 bit ADC), in line with your readings.

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