代码生成
TIM4基本功能配置
/* TIM4 init function */
void MX_TIM4_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim4.Instance = TIM4;//定时器4
htim4.Init.Prescaler = 72-1;//分频系数72分频
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;//向上计数方式
htim4.Init.Period = 300;//重装装载值
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;//无时钟分频
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;//自动重装载预装载使能
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)//初始化定时器
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;//PWM模式1,CNT<CCR为有效电平
sConfigOC.Pulse = 0;//占空比
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;//有效电平时间
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;//快速比较使能
if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)//PWM模式初始化
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim4);//硬件层初始户化
}
|