首页 > 其他分享 >通用定时器( TIM9 到 TIM14)

通用定时器( TIM9 到 TIM14)

时间:2022-08-23 21:45:59浏览次数:61  
标签:TIM11 定时器 TIM14 TIM9 TIM Init CODE HAL htim11

4.2 通用定时器( TIM9 到 TIM14)

4.2.1 实现PWM输出

系统时钟配置

定时器和PWM参数配置

4.2.2 代码实现

cube 软件自动生成的代码

/* TIM11 init function */
void MX_TIM11_Init(void)
{

  /* USER CODE BEGIN TIM11_Init 0 */

  /* USER CODE END TIM11_Init 0 */

  TIM_OC_InitTypeDef sConfigOC = {0};

  /* USER CODE BEGIN TIM11_Init 1 */

  /* USER CODE END TIM11_Init 1 */
  htim11.Instance = TIM11;
  htim11.Init.Prescaler = 0;
  htim11.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim11.Init.Period = 1000-1;
  htim11.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim11.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim11) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim11) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 250;    //配置占空比
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim11, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM11_Init 2 */

  /* USER CODE END TIM11_Init 2 */
  HAL_TIM_MspPostInit(&htim11);
}

用户自定义代码

void Timer1_Init( void )
{
    HAL_TIM_PWM_Init(&htim11);
    HAL_TIM_OC_Start(&htim11, TIM_CHANNEL_1);
    HAL_TIM_OC_Start_IT(&htim11, TIM_CHANNEL_1);
}

4.2.3 逻辑分析仪测试结果

实物照片

标签:TIM11,定时器,TIM14,TIM9,TIM,Init,CODE,HAL,htim11
From: https://www.cnblogs.com/mftang2018/p/16617901.html

相关文章