先附上代码!我用同样的方式使用TIM2和TIM3产生8路PWM,产生的波形出现问题,我在代码之后第2楼附上波形图!
//TIM2输出4路PWM波
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM信号电平跳变值 */
vu16 CCR1_Val = 1500;
vu16 CCR2_Val = 1600;
vu16 CCR3_Val = 2150;
vu16 CCR4_Val = 1100;
TIM_TimeBaseStructure.TIM_Period = 19999; // 溢出时间20ms
TIM_TimeBaseStructure.TIM_Prescaler = 71; //72分频1000KHz计数
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //时基初始化
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; //电机1不咬紧
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC1Init(TIM2,&TIM_OCInitStructure); //TIM_CH1通道配置
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //CCR1重装配置
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //电机2咬紧
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //设置通道2的电平跳变值,输出另外一个占空比的PWM
TIM_OC2Init(TIM2,&TIM_OCInitStructure); //TIM_CH2通道配置
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //CCR2重装配置
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //电机3咬紧
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //设置通道2的电平跳变值,输出另外一个占空比的PWM
TIM_OC3Init(TIM2,&TIM_OCInitStructure); //TIM_CH3通道配置
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //CCR3重装配置
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //电机4咬紧
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //设置通道2的电平跳变值,输出另外一个占空比的PWM
TIM_OC4Init(TIM2,&TIM_OCInitStructure); //TIM_CH4通道配置
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); //CCR4重装配置
TIM_ARRPreloadConfig(TIM2, ENABLE); // 使能TIM3重载寄存器ARR
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE); //使能定时器2
} |