STM32中TIM1输出两路占空比可调的PWM波,不能同时输出,关闭2通道,3通道正常输出;关闭3通道,2通道正常输出;但是两路没办法同时输出。麻烦香主帮忙看看配置,我估计是配置的问题。
void TIM1_Init2(unsigned int arr,unsigned int psc,unsigned int ccr)
{
uint16_t CCR2_Va1 = ccr;
/* TIM1 Configuration ---------------------------------------------------
Generates 7 PWM signals with 4 different duty cycles:
TIM1CLK = 72 MHz, Prescaler = 0, TIM1 counter clock = 72 MHz
TIM1 frequency = TIM1CLK/(TIM1_Period + 1) = 17.57 KHz
- TIM1 Channel1 & Channel1N duty cycle = TIM1->CCR1 / (TIM1_Period + 1) = 50%
- TIM1 Channel2 & Channel2N duty cycle = TIM1->CCR2 / (TIM1_Period + 1) = 37.5%
- TIM1 Channel3 & Channel3N duty cycle = TIM1->CCR3 / (TIM1_Period + 1) = 25%
- TIM1 Channel4 duty cycle = TIM1->CCR4 / (TIM1_Period + 1) = 12.5%
----------------------------------------------------------------------- */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time Base configuration */
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = psc; //分频器装入的预分频系数,设定计数器时钟
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = arr-1; //计数器向上计数到arr后产生更新事件,计数值归零,
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Va1; //设置了待装入捕获比较寄存器的脉冲值。它的取值必须在0x0000和0xFFFF之间。
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;//需要默认为高 TIM_OCPolarity_Low; //通道1输出比较极性低
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; // 通道1N输出比较极性低
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);//TIM1-CH2
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE); //TIM1总开关:开启
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/***********************定时器1的PB15管脚PWM输出程序 ********
*函数名:void TIM1_Init3(unsigned int arr,unsigned int psc,unsigned int ccr4)
*功能: PB15输出方波,即STEP输入方波,频率为2Khz
*输入: arr,psc, ccr3 单位:无
*输出: 无, 单位:无
*设计: TIM1_Init(38000,0,16000)输出频率为1K
***************************************************************/
void TIM1_Init3(unsigned int arr,unsigned int psc,unsigned int ccr)
{
uint16_t CCR3_Va1 = ccr;
/* TIM1 Configuration ---------------------------------------------------
Generates 7 PWM signals with 4 different duty cycles:
TIM1CLK = 72 MHz, Prescaler = 0, TIM1 counter clock = 72 MHz
TIM1 frequency = TIM1CLK/(TIM1_Period + 1) = 17.57 KHz
- TIM1 Channel1 & Channel1N duty cycle = TIM1->CCR1 / (TIM1_Period + 1) = 50%
- TIM1 Channel2 & Channel2N duty cycle = TIM1->CCR2 / (TIM1_Period + 1) = 37.5%
- TIM1 Channel3 & Channel3N duty cycle = TIM1->CCR3 / (TIM1_Period + 1) = 25%
- TIM1 Channel4 duty cycle = TIM1->CCR4 / (TIM1_Period + 1) = 12.5%
----------------------------------------------------------------------- */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time Base configuration */
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = psc; //分频器装入的预分频系数,设定计数器时钟
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = arr-1; //计数器向上计数到arr后产生更新事件,计数值归零,
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Va1; //设置了待装入捕获比较寄存器的脉冲值。它的取值必须在0x0000和0xFFFF之间。
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;//需要默认为高 TIM_OCPolarity_Low; //通道1输出比较极性低
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; // 通道1N输出比较极性低
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);//TIM1-CH3
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE); //TIM1总开关:开启
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
分别是TIM1 CH2和CH3通道配置!谢谢! |