cancel
Showing results for 
Search instead for 
Did you mean: 

ADC conversion stops working with timer interrupt

AMaub
Associate II

Hi,

I am new on mcus and STM32 board. Now I am stuck on a test project. I have a STM32F56ZG nucleo board and want to sample values from 4 ADC inputs; process them and then output the result on DAC channel 1. I am using DMA triggered by timer 6. There is a fonction that process the values to output to the DAC DMA buffer. When this function is in the main function everything works fine. But if I choose to trigger this function with another timer, then the ADC convertion stops. I tried several timers (TIM14, TIM1, TIM2) but without any succes. Any idea?

Thanks for your help.

4 REPLIES 4

> But if I choose to trigger this function with another timer,

How? What does that function do?

> then the ADC convertion stops. 

How is ADC set and how do you trigger the conversions?

JW

AMaub
Associate II

Hello,

thanks for your prompt reply. I was iout and could'nt reply before.

I investigated a little more and realized that the function is triggered indeed (DAC and ADC works too), but only on "slow" timer frequencies. I supposed the function was cpu intensive and was blocking the ADC and ADC conversions, so I tried with a simpler function (square wave generator) I changed the code to make a simple square wave on the DAC and realized that it cannot output a sqare wave faster than 100kHz ( and at 100kHz the waveform is very unstable). The DAC is supposed to works up to 1Mhz... so why can't I generate a simple squarewave at more than 100KHz ? Maybe this is a problem of timer configuration. I am stuck...

Here below are some code blocks.

// MAIN FILE
* USER CODE BEGIN 4 */
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
 
	if(htim->Instance == TIM14)
	{analog_out();}
}
 
/* USER CODE END 4 */
 
 
 
uint32_t mydac[1];
int x =0;
 
 
 
//Initialisation of the DMA for The DAc and ADC
extern "C" void  startDMA()
{
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)adc_data,4);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, mydac,1, DAC_ALIGN_12B_R);
 
}
 
 
 
// function to be triggered to make a simple square wave via DAC
 
extern "C" void  analog_out()
 
{ ;	
mydac[0]=  x *4090; 
x=(1-x);
}
 
 
 
 
 
// TIMERS CONGIG
 
TIM_HandleTypeDef htim6;
TIM_HandleTypeDef htim14;
 
/* TIM6 init function */
void MX_TIM6_Init(void)
{
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 54-1;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 10-1;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
 
}
/* TIM14 init function */
void MX_TIM14_Init(void)
{
 
  htim14.Instance = TIM14;
  htim14.Init.Prescaler = 10-1;
  htim14.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim14.Init.Period = 25-1;
  htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim14) != HAL_OK)
  {
    Error_Handler();
  }
 
}
 
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
 
  if(tim_baseHandle->Instance==TIM6)
  {
  /* USER CODE BEGIN TIM6_MspInit 0 */
 
  /* USER CODE END TIM6_MspInit 0 */
    /* TIM6 clock enable */
    __HAL_RCC_TIM6_CLK_ENABLE();
  /* USER CODE BEGIN TIM6_MspInit 1 */
 
  /* USER CODE END TIM6_MspInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM14)
  {
  /* USER CODE BEGIN TIM14_MspInit 0 */
 
  /* USER CODE END TIM14_MspInit 0 */
    /* TIM14 clock enable */
    __HAL_RCC_TIM14_CLK_ENABLE();
 
    /* TIM14 interrupt Init */
    HAL_NVIC_SetPriority(TIM8_TRG_COM_TIM14_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM8_TRG_COM_TIM14_IRQn);
  /* USER CODE BEGIN TIM14_MspInit 1 */
 
  /* USER CODE END TIM14_MspInit 1 */
  }
}
 
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
 
  if(tim_baseHandle->Instance==TIM6)
  {
  /* USER CODE BEGIN TIM6_MspDeInit 0 */
 
  /* USER CODE END TIM6_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM6_CLK_DISABLE();
  /* USER CODE BEGIN TIM6_MspDeInit 1 */
 
  /* USER CODE END TIM6_MspDeInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM14)
  {
  /* USER CODE BEGIN TIM14_MspDeInit 0 */
 
  /* USER CODE END TIM14_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM14_CLK_DISABLE();
 
    /* TIM14 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM8_TRG_COM_TIM14_IRQn);
  /* USER CODE BEGIN TIM14_MspDeInit 1 */
 
  /* USER CODE END TIM14_MspDeInit 1 */
  }
} 
 
/* USER CODE BEGIN 1 */
 
/* USER CODE END 1 */

I don't use Cube and don't understand it.

You don't want to DMA one by one, that doesn't make sense. Use DMA on as long buffers as possible and use interrupts sparingly.

JW

AMaub
Associate II

Thanks for the advise! It now works.