帮我将代码修改为标准库 void atim_timx_cplm_pwm_init(uint16_t arr, uint16_t psc) { TIM_OC_InitTypeDef sConfigOC ; g_atimx_cplm_pwm_handle.Instance = ATIM_TIMX_CPLM; /* 定时器x / g_atimx_cplm_pwm_handle.Init.Prescaler = psc; / 定时器预分频系数 / g_atimx_cplm_pwm_handle.Init.CounterMode = TIM_COUNTERMODE_UP; / 向上计数模式 / g_atimx_cplm_pwm_handle.Init.Period = arr; / 自动重装载值 / g_atimx_cplm_pwm_handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; / 时钟分频因子 / g_atimx_cplm_pwm_handle.Init.RepetitionCounter = 0; / 重复计数器寄存器为0 / g_atimx_cplm_pwm_handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; / 使能影子寄存器TIMx_ARR / HAL_TIM_PWM_Init(&g_atimx_cplm_pwm_handle) ; / 设置PWM输出 / sConfigOC.OCMode = TIM_OCMODE_PWM1; / PWM模式1 / sConfigOC.Pulse = 0; / 比较值为0 / sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW; / OCy 低电平有效 / sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW; / OCyN 低电平有效 / sConfigOC.OCFastMode = TIM_OCFAST_ENABLE; / 不使用快速模式 / sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; / 主通道的空闲状态 / sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; / 互补通道的空闲状态 / HAL_TIM_PWM_ConfigChannel(&g_atimx_cplm_pwm_handle, &sConfigOC, ATIM_TIMX_CPLM_CHY); / 配置后默认清CCER的互补输出位 / / 设置死区参数,开启死区中断 / sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE; / OSSR设置为1 / sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; / OSSI设置为0 / sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; / 上电只能写一次,需要更新死区时间时只能用此值 / sBreakDeadTimeConfig.DeadTime = 0X0F; / 死区时间 / sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; / BKE = 0, 关闭BKIN检测 / sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW; / BKP = 1, BKIN低电平有效 / sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; / 使能AOE位,允许刹车后自动恢复输出 / HAL_TIMEx_ConfigBreakDeadTime(&g_atimx_cplm_pwm_handle, &sBreakDeadTimeConfig); / 设置BDTR寄存器 */ }
时间: 2024-03-19 12:41:55 浏览: 138
以下是修改后的标准库代码:
```
void atim_timx_cplm_pwm_init(uint16_t arr, uint16_t psc)
{
TIM_HandleTypeDef g_atimx_cplm_pwm_handle;
TIM_OC_InitTypeDef sConfigOC;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
g_atimx_cplm_pwm_handle.Instance = ATIM_TIMX_CPLM;
g_atimx_cplm_pwm_handle.Init.Prescaler = psc;
g_atimx_cplm_pwm_handle.Init.CounterMode = TIM_COUNTERMODE_UP;
g_atimx_cplm_pwm_handle.Init.Period = arr;
g_atimx_cplm_pwm_handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
g_atimx_cplm_pwm_handle.Init.RepetitionCounter = 0;
g_atimx_cplm_pwm_handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
HAL_TIM_PWM_Init(&g_atimx_cplm_pwm_handle);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&g_atimx_cplm_pwm_handle, &sConfigOC, ATIM_TIMX_CPLM_CHY);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0X0F;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
HAL_TIMEx_ConfigBreakDeadTime(&g_atimx_cplm_pwm_handle, &sBreakDeadTimeConfig);
}
```
注意:以上代码是基于您提供的代码进行修改的,没有进行实际测试,可能需要根据实际情况进行调整。
阅读全文