采用STM32f0的驱动无刷电机,想利用COM事件(没开com中断)同时切换6个通道
我这里抓了CH1 和 CH2的波形,实际测试下来要在下一个PWM开始的时候才生效,如下图所示
照STM32的手册,启用了COM事件应该是立即生效,应该是下面这个手画的部分就生效,就是在CH1关闭的同时CH2应该打开了
我在TIM1初始化的时候已经启用了
TIM_CCPreloadControl(TIM1, ENABLE);
贴上TIM1的设置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_BaseStructure); //初始化TIMBASE结构体
TIM_BaseStructure.TIM_Period = Motor.Period; //自动装载值,累计次数
TIM_BaseStructure.TIM_Prescaler = 1; //计数时钟2分频
TIM_BaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //系统时钟,不分频,48M
TIM_BaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM1中央对齐模式2
TIM_BaseStructure.TIM_RepetitionCounter = 0; //在计数器向上是产生标志,向下时不置位
TIM_TimeBaseInit(TIM1, &TIM_BaseStructure);
TIM_OcStructure.TIM_OCMode = TIM_OCMode_PWM1; //TMR1 输出比较时间模式,初始化的时候不需要输出
TIM_OcStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出比较状态
TIM_OcStructure.TIM_OutputNState = TIM_OutputNState_Enable;//使能输出比较N状态
TIM_OcStructure.TIM_Pulse = Motor.Pulse; //待装入捕获比较寄存器的脉冲值
TIM_OcStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TMR1输出比较极性高
TIM_OcStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //TMR1输出比较N极性高
TIM_OcStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //当MOE=0设置TIM1输出比较空闲状态
TIM_OcStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set; //当MOE=0设置TIM1输出比较N空闲状态
TIM_OC1Init(TIM1, &TIM_OcStructure);
TIM_OC2Init(TIM1, &TIM_OcStructure);
TIM_OC3Init(TIM1, &TIM_OcStructure);
TIM_OcStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OcStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OcStructure.TIM_Pulse = Motor.Oc4_Pulse; //PWM-On ADC采样时刻
TIM_OcStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TMR1输出比较极性高
TIM_OcStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //当MOE=0设置TIM1输出比较空闲状态
TIM_OC4Init(TIM1, &TIM_OcStructure);
TIM_BdtrStructure.TIM_OSSRState = TIM_OSSRState_Enable; //使能TIM1 OSSR状态
TIM_BdtrStructure.TIM_OSSIState = TIM_OSSIState_Enable; //使能TIM1 OSSI状态
TIM_BdtrStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //不锁任何位
TIM_BdtrStructure.TIM_DeadTime = 1; //输出打开和关闭状态之间的延时,死区时间
TIM_BdtrStructure.TIM_Break = TIM_Break_Disable; //失能能TIM1刹车输入
TIM_BdtrStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //TIM1刹车输入管脚极性高
TIM_BdtrStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //自动输出功能使能
TIM_BDTRConfig(TIM1, &TIM_BdtrStructure);
TIM_CCxCmd(TIM1, TIM_Channel_4, TIM_CCx_Enable);
TIM_CCPreloadControl(TIM1, ENABLE); //高级定时器,使用COM事件同步更新寄存器
//需要CCxE,CCxNE,OCxM预装载
//只对互补通道起作用
TIM_Cmd(TIM1, ENABLE); //使能TMR1
TIM_CtrlPWMOutputs(TIM1, ENABLE); //使能TIM1的主输出
}
然后在换相子程序后触发COM
pwm_scheme_Pwm_On(Motor.PhaseState); //这里是6步换相的子程序
TIM_GenerateEvent(TIM1, TIM_EventSource_COM); //这里是启用com事件
另外测试了其他两个方法
1、我换相后不触发com,测试下来,波形一直不变,验证了加了com事件应该是起作用了
2、开com的中断,测试下来,效果一样
有碰到类似的朋友帮我看看吧,实在没招了,上来请教下大家,不胜感激。
|