TIM_HandleTypeDef g_Atim_PWM_COG_Handle={0};
TIM_BreakDeadTimeConfigTypeDef g_TIM_BreakDeadTimeConfigStruct={0};
/*高级定时器互补输出带死区初始化*/
void Atim_PWM_COG_Init(uint16_t Psc,uint16_t Arr)
{
g_Atim_PWM_COG_Handle.Instance=TIM1;/*基地址*/
g_Atim_PWM_COG_Handle.Init.CounterMode=TIM_COUNTERMODE_UP;/*递增模式*/
g_Atim_PWM_COG_Handle.Init.Prescaler=Psc-1;/*分频系数*/
g_Atim_PWM_COG_Handle.Init.Period=Arr-1;/*重装值*/
g_Atim_PWM_COG_Handle.Init.ClockDivision=TIM_CLOCKDIVISION_DIV4;/*4分频*/
HAL_TIM_PWM_Init(&g_Atim_PWM_COG_Handle);/*PWM初始化*/
TIM_OC_InitTypeDef TIM_OC_PWMCOG_InitStruct={0};
TIM_OC_PWMCOG_InitStruct.OCMode=TIM_OCMODE_PWM1;/*配置输出比较模式*/
TIM_OC_PWMCOG_InitStruct.OCPolarity=TIM_OCPOLARITY_HIGH;/*设置输出比较极性*/
TIM_OC_PWMCOG_InitStruct.OCNPolarity=TIM_OCNPOLARITY_HIGH;/*设置互补输出极性设置*/
TIM_OC_PWMCOG_InitStruct.OCIdleState=TIM_OCIDLESTATE_RESET;/*空闲状态下OC1输出*/
TIM_OC_PWMCOG_InitStruct.OCNIdleState=TIM_OCIDLESTATE_RESET;/*空闲状态下OC1N输出*/
HAL_TIM_PWM_ConfigChannel(&g_Atim_PWM_COG_Handle,&TIM_OC_PWMCOG_InitStruct,TIM_CHANNEL_1);/*配置PWM模式/比较值等*/
g_TIM_BreakDeadTimeConfigStruct.OffStateRunMode=TIM_OSSR_DISABLE;/*运行模式下的关闭状态选择*/
g_TIM_BreakDeadTimeConfigStruct.OffStateIDLEMode=TIM_OSSI_DISABLE;/*空闲模式下的关闭状态选择*/
g_TIM_BreakDeadTimeConfigStruct.LockLevel=TIM_LOCKLEVEL_OFF;/*寄存器锁定设置*/
g_TIM_BreakDeadTimeConfigStruct.BreakState=TIM_BREAK_ENABLE;/*是否使能刹车功能*/
g_TIM_BreakDeadTimeConfigStruct.BreakPolarity=TIM_BREAKPOLARITY_HIGH;/*刹车输入极性*/
g_TIM_BreakDeadTimeConfigStruct.AutomaticOutput=TIM_AUTOMATICOUTPUT_ENABLE;/*自动恢复输出使能,既使能AOE*/
HAL_TIMEx_ConfigBreakDeadTime(&g_Atim_PWM_COG_Handle,&g_TIM_BreakDeadTimeConfigStruct);/*配置刹车功能、死区时间等*/
HAL_TIM_PWM_Start(&g_Atim_PWM_COG_Handle,TIM_CHANNEL_1);/*使能PWM输出*/
HAL_TIMEx_PWMN_Start(&g_Atim_PWM_COG_Handle,TIM_CHANNEL_1);/*使能互补输出*/
}
/*高级定时器PWM.MSP初始化函数*/
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)/*PWM输出初始化函数,NVIC,CLOCK,GPIO等*/
{
if(htim->Instance==TIM1)
{
__HAL_RCC_TIM1_CLK_ENABLE();/*开启TIM时钟*/
__HAL_RCC_GPIOA_CLK_ENABLE();/*开启GPIO时钟*/
GPIO_InitTypeDef GPIO_COGPWM_InitStruct={0};
GPIO_COGPWM_InitStruct.Mode=GPIO_MODE_AF_PP;/*复用推挽*/
GPIO_COGPWM_InitStruct.Pin=GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;/**/
GPIO_COGPWM_InitStruct.Pull=GPIO_NOPULL;/*无上下拉*/
GPIO_COGPWM_InitStruct.Speed=GPIO_SPEED_FREQ_HIGH;/*高速*/
GPIO_COGPWM_InitStruct.Alternate=GPIO_AF1_TIM1;/*复用到TIM1*/
HAL_GPIO_Init(GPIOA,&GPIO_COGPWM_InitStruct);
GPIO_COGPWM_InitStruct.Pin=GPIO_PIN_6;
GPIO_COGPWM_InitStruct.Pull=GPIO_PULLDOWN;/*刹车浮空会不稳定,刹车有效电平为高接入3.3V会触发刹车*/
HAL_GPIO_Init(GPIOA,&GPIO_COGPWM_InitStruct);
}
}
程序下载到F429板子后,可以实现互补输出,刹车也正常,但是刹车后的电平并不是所设置的RESET,两个端口都是一直高电平,本人研究了1天也没找到问题所在。请大佬帮助 |