stm32使用TIM1_CH1和TIM1_CH4 即PA8和PA9输出双路PWM遇到的问题
正在使用stm32的TIM1 打算输出两路可控的pwm波形,要求两路单独控制,占空比可调,频率统一,调试过程中发现,
pa8 的tim1 ch1好使,tim1ch4不好使。tim1共有4路可控的,管脚是PA8 PA9 PA10 PA11,中间两路用于串口调试了。
经过keil软件仿真比较发现,tim1ch4的配置不对。如图
同时提供一种解决方法,当有一个正常使用的时候可以通过,软件仿真查看配置,然后进行比较并打钩后再试运行,可解决类似问题。
pwm的波形中一个是65%,下边是85%。
timer的配置看源码:
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
//????????????,??TIM1 CH1PWM
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //TIM_CH4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc; // TIMx
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //??????:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //
TIM_OCInitStructure.TIM_Pulse = 0; //
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc; //
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //
TIM_OCInitStructure.TIM_Pulse = 0; //
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH4
TIM1->CCER |= TIM_CCER_CC4P;
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE
TIM_ARRPreloadConfig(TIM1, ENABLE); //??TIMxARR
TIM_Cmd(TIM1, ENABLE); //??TIM1
// TIM_SetCompare4(TIM1,1000);
}
main里边如下使用
TIM1_PWM_Init(4799,0);//ƵÂÊPWM=72000/(4799+1)=15 Khz
Motor_forw();
//set_pwm1(15000,5000);//15KHZ 50%
//set_pwm1(15000,6000);
set_pwm1(15000,6500);
set_pwm2(15000,8500);