帮我将代码修改为标准库 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-02-14 15:14:35 浏览: 142
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定时器
}
阅读全文