STM32 HAL库 HAL_TIM_OC_Start函数解读

STM32 HAL库 HAL_TIM_OC_Start函数解读

关键词: STM32; HAL; HAL_TIM_OC_Start

该函数位于文件stm32f4xx_hal_tim.c

HAL_TIM_OC_Start函数

c 复制代码
/**
  * @brief  Starts the TIM Output Compare signal generation.        (开始TIM输出比较信号生成)
  * @param  htim TIM Output Compare handle
  * @param  Channel TIM Channel to be enabled
  *          This parameter can be one of the following values:
  *            @arg TIM_CHANNEL_1: TIM Channel 1 selected
  *            @arg TIM_CHANNEL_2: TIM Channel 2 selected
  *            @arg TIM_CHANNEL_3: TIM Channel 3 selected
  *            @arg TIM_CHANNEL_4: TIM Channel 4 selected
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
{
  uint32_t tmpsmcr;

  /* Check the parameters (检查传入的定时器实例和通道是否有效) */
  assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));

  /* Check the TIM channel state (检查指定的通道是否处于就绪状态, 如果不是, 则返回错误状态) */
  if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  {
    return HAL_ERROR;
  }

  /* Set the TIM channel state (设置通道状态-将通道状态设置为忙碌, 表示通道正在使用中) */
  TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);

  /* Enable the Output compare channel (启用指定的输出比较通道) */
  TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);

    /* 主输出使能, 如果定时器实例支持刹车功能, 则启动主输出 MOE位 置1 */
  if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  {
    /* Enable the main output (TIM1、TIM8主输出使能) */
    __HAL_TIM_MOE_ENABLE(htim);
  }

  /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  /* 外设模式处理, 如果定时器处于从模式, 只有在从模式未启用触发时才需要手动启用定时器 */
  if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  {
    tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
    if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
    {
      __HAL_TIM_ENABLE(htim);
    }
  }
  else
  {
    __HAL_TIM_ENABLE(htim);             /* 启动定时器 CNT开始计数 */
  }

  /* Return function status */
  return HAL_OK;
}

从下图中可以看到, HAL_TIM_OC_Start函数与HAL_TIM_PWM_Start函数一摸一样

相关参考链接

STM32 HAL库 HAL_TIM_OC_Stop函数详细解释_stm32使用hal函数编程-CSDN博客