初中生
最后登录1970-1-1
在线时间 小时
注册时间2023-8-11
|
楼主 |
发表于 2024-3-5 10:33:20
|
显示全部楼层
已解决,代码如下:
- #include "bsp_GeneralTim.h"
- void GENERAL_TIM_Mode_Config(void)
- {
- /*--------------------GPIO初始化-------------------------*/
- GPIO_InitTypeDef GPIO_InitStructure;
- // 输出比较通道1、4的 GPIO 初始化
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PA11
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
- GPIO_Init(GPIOA,&GPIO_InitStructure); //PA8
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
- GPIO_Init(GPIOB,&GPIO_InitStructure);//PB13
-
-
- // 开启定时器时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
- /*--------------------时基结构体初始化-------------------------*/
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Period=1799; //40K HZ
- TIM_TimeBaseStructure.TIM_Prescaler= 0; //72M
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //1分频
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- /*--------------------输出比较结构体初始化-------------------*/
- //将输出通道1、4初始化为PWM模式1
- TIM_OCInitTypeDef TIM_OCInitStruct;
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
- TIM_OCInitStruct.TIM_OutputNState=TIM_OutputNState_Enable ;
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
- TIM_OCInitStruct.TIM_OCNPolarity=TIM_OCNPolarity_High;
- TIM_OCInitStruct.TIM_OCIdleState=TIM_OCIdleState_Set;
- TIM_OCInitStruct.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
- TIM_OCInitStruct.TIM_Pulse=1000;
- TIM_OC4Init(TIM1,&TIM_OCInitStruct);
- TIM_OC1Init(TIM1,&TIM_OCInitStruct);
-
- //使能预装载寄存器
- TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);
- TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
- //死区和刹车功能配置
- TIM_BDTRInitTypeDef TIM_BDTRInitStruct;
- TIM_BDTRInitStruct.TIM_OSSIState=TIM_OSSIState_Disable;
- TIM_BDTRInitStruct.TIM_OSSRState=TIM_OSSRState_Disable;
- TIM_BDTRInitStruct.TIM_LOCKLevel=TIM_LOCKLevel_1;
- TIM_BDTRInitStruct.TIM_DeadTime=216; //
- TIM_BDTRInitStruct.TIM_BreakPolarity=TIM_BreakPolarity_Low;
- TIM_BDTRInitStruct.TIM_Break=TIM_Break_Disable;
- TIM_BDTRInitStruct.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;
- TIM_BDTRConfig(TIM1,&TIM_BDTRInitStruct);
- //使能自动重装载
- TIM_ARRPreloadConfig(TIM1,ENABLE);
- //开启定时器
- TIM_Cmd(TIM1,ENABLE);
- //主输出使能
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void PWM_Pause(void)
- {
- TIM_SetCompare1(TIM1, 1500);
- TIM_SetCompare4(TIM1,500);
- }
- /*********************************************END OF FILE**********************/
复制代码
初始化CH4的GPIO后,输出比较结构体不用改动,加两句TIM_OC4Init(TIM1,&TIM_OCInitStruct);
TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);即可
互补PWM和单独一路PWM的占空比通过TIM_SetCompare1(TIM1, 1500);TIM_SetCompare4(TIM1,500);
单独配置即可。
|
|