cancel
Showing results for 
Search instead for 
Did you mean: 

using the ADC on STM32L412

deep_rune
Associate III

Im trying to upgrade the chip I'm using from an STM32F013 to an STM32L412 because I need more speed. I trying to get the ADC running, but having some problems - I check for the EOC flag to be set with a while loop but it stays in the loop forever. I really can't see what I have missed - can anyone help? My setup and code is below

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_ADC1_Init();
 
  ADC1->CR |= 1>>0; // enable ADC
 
  while (1)
  {
	  ADC1->CR |= ADC_CR_ADSTART;
	 	  	  while(((ADC1->ISR) & 0b00000100)==0)
	 	  	  {
	 	  	  }
	 	  	Result1 = ADC1->DR;
	 		ADC1->CR |= ADC_CR_ADSTART;
  }
}
 
 
static void MX_ADC1_Init(void)
{
  ADC_MultiModeTypeDef multimode = {0};
  ADC_ChannelConfTypeDef sConfig = {0};
 
  /** Common config 
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.NbrOfDiscConversion = 1;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure the ADC multi-mode 
  */
  multimode.Mode = ADC_MODE_INDEPENDENT;
  if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Regular Channel 
  */
  sConfig.Channel = ADC_CHANNEL_5;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
}

1 ACCEPTED SOLUTION

Accepted Solutions

> I check for the EOC flag to be set with a while loop but it stays in the loop forever.

Sounds like clock is not enabled. Read the ADC clock setup relevant portions of the ADC and RCC chapters in RM, then read out and check the related ADC and RCC registers content.

JW

View solution in original post

2 REPLIES 2

> I check for the EOC flag to be set with a while loop but it stays in the loop forever.

Sounds like clock is not enabled. Read the ADC clock setup relevant portions of the ADC and RCC chapters in RM, then read out and check the related ADC and RCC registers content.

JW

TDK
Guru

Do the registers otherwise look correct? Is HAL_ADC_MspInit enabling the clock?

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