大佬们好,下面的代码只有TIM1_CH1可以输出PWM波,这是为什么呢?其他的TIM9_CH1 TIM4_CH1 TIM5_CH1都不能输出波形,是代码哪里错了吗?谢谢大佬们了。
[mw_shl_code=c,true]
void MOTOR_TIM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9 | RCC_APB2Periph_TIM1,ENABLE);//max 84MHz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 | RCC_APB1Periph_TIM5,ENABLE);//max 42MHz
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE,ENABLE);
TIM_DeInit(TIM1);
TIM_DeInit(TIM4);
TIM_DeInit(TIM5);
TIM_DeInit(TIM9);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_Prescaler = 168-1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 1000-1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM9,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInitStructure.TIM_Prescaler = 84-1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set; //空闲高电平
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC1Init(TIM9, &TIM_OCInitStructure); //TIM9_CH1
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //TIM1_CH1
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //TIM4_CH1
TIM_OC2Init(TIM5, &TIM_OCInitStructure); //TIM5_CH2
TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Enable); //使能TIM9在CCR1上的预装载寄存器
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR1上的预装载寄存器
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR1上的预装载寄存器
TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable); //使能TIM5在CCR2上的预装载寄存器
TIM_ARRPreloadConfig(TIM9,ENABLE);
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_ARRPreloadConfig(TIM4,ENABLE);
TIM_ARRPreloadConfig(TIM5,ENABLE);
TIM_Cmd(TIM9, ENABLE); //使能TIM9
TIM_Cmd(TIM1, ENABLE); //使能TIM1
TIM_Cmd(TIM4, ENABLE); //使能TIM4
TIM_Cmd(TIM5, ENABLE); //使能TIM5
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}[/mw_shl_code]
一周热门 更多>