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 逻辑分析仪测试结果
实物照片