ST意法半导体
直播中

王鹏

7年用户 1342经验值
私信 关注
[问答]

从MX里设置TIM3_CH2为PWM输出,但是输不出是为什么?

从MX里设置tiM3_CH2为PWM输出, PSC为72-1  counter period 1000-1 但是输出不了, PWM start也加了 TIM3->CCR2也加了,不知道为什么,求解答!!

回帖(1)

张坤

2023-8-5 11:09:23
建议使用CubeMx配置下。


static void MX_TIM3_Init(void){


  // USER CODE BEGIN TIM3_Init 0


  // USER CODE END TIM3_Init 0


  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};


  /* USER CODE BEGIN TIM3_Init 1 */


  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 71;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 9999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init( htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource( htim3,  sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init( htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization( htim3,  sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 800;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel( htim3,  sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  // USER CODE BEGIN TIM3_Init 2 /


  //USER CODE END TIM3_Init 2 /
  HAL_TIM_MspPostInit( htim3);


}


然后 加上PWM启动函数:


HAL_TIM_PWM_Start( htim3,TIM_CHANNEL_2);
举报

更多回帖

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