STM32
直播中

1123127317

8年用户 964经验值
擅长:制造/封装 接口/总线/驱动
私信 关注
[问答]

求助,有关STM32F0 ADC库函数中Timeout用法的问题求解

我用STM32Cubemx生成keil项目,用到了ADC,于是查看stm32f0xx_hal_adc.c.中的HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t timeout)函数,不明白Timeout参数是干什么用的,,Timeout我试过用不同的值,但是效果都一样,没什么区别
该函数如下:


  • /**
  •   * @brief  Wait for regular group conversion to be completed.
  •   * @param  hadc: ADC handle
  •   * @param  Timeout: Timeout value in millisecond.
  •   * @retval HAL status
  •   */
  • HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  • {
  •   uint32_t tickstart;
  •   uint32_t tmp_Flag_EOC;

  •   /* Check the parameters */
  •   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));

  •   /* If end of conversion selected to end of sequence */
  •   if (hadc->Init.EOCSelection == EOC_SEQ_CONV)
  •   {
  •     tmp_Flag_EOC = ADC_FLAG_EOS;
  •   }
  •   /* If end of conversion selected to end of each conversion */
  •   else /* EOC_SINGLE_CONV */
  •   {
  •     tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
  •   }

  •   /* Get timeout */
  •   tickstart = HAL_GetTick();

  •   /* Wait until End of Conversion flag is raised */
  •   while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  •   {
  •     /* Check if timeout is disabled (set to infinite wait) */
  •     if(Timeout != HAL_MAX_DELAY)
  •     {
  •       if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  •       {
  •         /* Update ADC state machine to timeout */
  •         hadc->State = HAL_ADC_STATE_TIMEOUT;

  •         /* Process unlocked */
  •         __HAL_UNLOCK(hadc);

  •         return HAL_ERROR;
  •       }
  •     }
  •   }

  •   /* Clear end of conversion flag of regular group if low power feature       */
  •   /* "LowPowerAutoWait " is disabled, to not interfere with this feature      */
  •   /* until data register is read using function HAL_ADC_GetValue().           */
  •   if (hadc->Init.LowPowerAutoWait == DISABLE)
  •   {
  •     /* Clear regular group conversion flag */
  •     __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  •   }

  •   /* Update state machine on conversion status if not in error state */
  •   if(hadc->State != HAL_ADC_STATE_ERROR)
  •   {
  •     /* Change ADC state */
  •     hadc->State = HAL_ADC_STATE_EOC_REG;
  •   }

  •   /* Return ADC state */
  •   return HAL_OK;
  • }

请问Timeout有什么用?

回帖(1)

袁沁茹

2024-5-16 16:44:19
打断while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC)),退出这个loop的条件,代码更健壮。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分