新手上路
- 积分
- 34
- 金钱
- 34
- 注册时间
- 2020-8-3
- 在线时间
- 7 小时
|
楼主 |
发表于 2024-3-4 00:24:50
|
显示全部楼层
PWM c文件如下
IM_HandleTypeDef g_atimx_cplm_pwm_handle; /* 定时器x句柄 */
void atim_timx_cplm_pwm_init(uint16_t arr, uint16_t psc)
{
TIM_OC_InitTypeDef tim_oc_cplm_pwm = {0};
TIM_BreakDeadTimeConfigTypeDef sbreak_dead_time_config = {0};
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_CENTERALIGNED1; /* 向上计数模式 */
g_atimx_cplm_pwm_handle.Init.Period = arr; /* 自动重装载值 */
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输出 */
tim_oc_cplm_pwm.OCMode = TIM_OCMODE_PWM1; /* PWM模式1 */
tim_oc_cplm_pwm.Pulse = 0; /* 比较值为0 */
tim_oc_cplm_pwm.OCPolarity = TIM_OCPOLARITY_LOW; /* OCy 低电平有效 */
tim_oc_cplm_pwm.OCNPolarity = TIM_OCNPOLARITY_LOW; /* OCyN 低电平有效 */
tim_oc_cplm_pwm.OCFastMode = TIM_OCFAST_ENABLE; /* 使用快速模式 */
tim_oc_cplm_pwm.OCIdleState = TIM_OCIDLESTATE_RESET; /* 主通道的空闲状态 */
tim_oc_cplm_pwm.OCNIdleState = TIM_OCNIDLESTATE_RESET; /* 互补通道的空闲状态 */
HAL_TIM_PWM_ConfigChannel(&g_atimx_cplm_pwm_handle, &tim_oc_cplm_pwm, TIM_CHANNEL_1); /* 配置后默认清CCER的互补输出位 */
HAL_TIM_PWM_ConfigChannel(&g_atimx_cplm_pwm_handle, &tim_oc_cplm_pwm, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&g_atimx_cplm_pwm_handle, &tim_oc_cplm_pwm, TIM_CHANNEL_3);
/* 设置死区参数,开启死区中断 */
sbreak_dead_time_config.OffStateRunMode = TIM_OSSR_ENABLE; /* OSSR设置为1 */
sbreak_dead_time_config.OffStateIDLEMode = TIM_OSSI_DISABLE; /* OSSI设置为0 */
sbreak_dead_time_config.LockLevel = TIM_LOCKLEVEL_OFF; /* 上电只能写一次,需要更新死区时间时只能用此值 */
sbreak_dead_time_config.DeadTime = 0X04; /* 死区时间 */
sbreak_dead_time_config.BreakState = TIM_BREAK_DISABLE; /* BKE = 0, 关闭BKIN检测 */
sbreak_dead_time_config.BreakPolarity = TIM_BREAKPOLARITY_LOW; /* BKP = 1, BKIN低电平有效 */
sbreak_dead_time_config.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; /* 使能AOE位,允许刹车后自动恢复输出 */
HAL_TIMEx_ConfigBreakDeadTime(&g_atimx_cplm_pwm_handle, &sbreak_dead_time_config); /* 设置BDTR寄存器 */
HAL_TIM_PWM_Start(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_1); /* 开启主通道输出 */
HAL_TIMEx_PWMN_Start(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_1); /* 开启互补通道输出 */
__HAL_TIM_SetCompare(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_1, 1000);
HAL_TIM_PWM_Start(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_2); /* 开启主通道输出 */
HAL_TIMEx_PWMN_Start(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_2); /* 开启互补通道输出 */
__HAL_TIM_SetCompare(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_2, 3000);
HAL_TIM_PWM_Start(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_3); /* 开启主通道输出 */
HAL_TIMEx_PWMN_Start(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_3); /* 开启互补通道输出 */
__HAL_TIM_SetCompare(&g_atimx_cplm_pwm_handle, TIM_CHANNEL_3, 2000);
}
/**
* @brief 定时器底层驱动,时钟使能,引脚配置
此函数会被HAL_TIM_PWM_Init()调用
* @param htim:定时器句柄
* @retval 无
*/
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{
if (htim->Instance == ATIM_TIMX_CPLM)
{
GPIO_InitTypeDef gpio_init_struct;
ATIM_TIMX_CPLM_CHY_GPIO_CLK_ENABLE(); /* 通道y对应IO口时钟使能 */
ATIM_TIMX_CPLM_CHYN_GPIO_CLK_ENABLE(); /* 互补通道对应IO口时钟使能 */
ATIM_TIMX_CPLM_CLK_ENABLE(); /* 定时器x时钟使能 */
/* 配置PWM主通道引脚 */
gpio_init_struct.Pin = ATIM_TIMX_CPLM_CHY_GPIO_PIN|ATIM_TIMX_CPLM_CHY_GPIO_PIN_B|GPIO_PIN_10;
gpio_init_struct.Mode = GPIO_MODE_AF_PP;
gpio_init_struct.Pull = GPIO_NOPULL;
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH ;
gpio_init_struct.Alternate = ATIM_TIMX_CPLM_CHY_GPIO_AF; /* 端口复用 */
HAL_GPIO_Init(ATIM_TIMX_CPLM_CHY_GPIO_PORT, &gpio_init_struct);
/* 配置PWM互补通道引脚 */
gpio_init_struct.Pin = ATIM_TIMX_CPLM_CHYN_GPIO_PIN|ATIM_TIMX_CPLM_CHYN_GPIO_PIN_B|GPIO_PIN_15;
HAL_GPIO_Init(ATIM_TIMX_CPLM_CHYN_GPIO_PORT, &gpio_init_struct);
}
} |
|