我的想法是:
使用通用定时器输出PWM,其中tiM2_CH1正常输出PWM,TIM2_CH2作为移相计数器,通过TIM2_CH2来触发TIM3,TIM3动作时刻由TIM2_CH2高电平持续时间决定,那么通过控制TIM2捕获比较2的值来改变TIM2_CH2的脉宽,就可以控制移相角度。实现方法涉及到定时器的“主\从”工作模式,也就是通过一个定时器(TIM2)触发另一个定时器(TIM3)。
问题是:
在程序持续运行的过程中,使用示波器观察,只有第一次给TIM2捕获比较2赋值生效【TIM_SetCompare2(TIM2, TIMCompare2)】,之后再改变TIMCompare2的值就不生效了,是不是哪个标志没有清除掉导致的?
小弟只有8分积分,都贡献出来了,希望高手能指导12,谢谢啦!
以下为相关代码:
1.定时器配置部分
TIM_TimeBaseInitTypeDef TimeBaseStruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
TimeBaseStruct.TIM_Period=90;//200KHz
TimeBaseStruct.TIM_Prescaler=3;//18MHz
TimeBaseStruct.TIM_clockDivision=0;
TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//ÏòÉϼÆÊýģʽ
//TIM_ARRPreloadConfig(TIM2, ENABLE);
//TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_TimeBaseInit(TIM2,&TimeBaseStruct); //³õʼ»¯¶¨Ê±Æ÷
TIM_TimeBaseInit(TIM3,&TimeBaseStruct); //³õʼ»¯¶¨Ê±Æ÷
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//Êä³öģʽ£¨PWM1£©
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//pwmʹÄÜλ
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;//Êä³ö¼«ÐÔ
TIM_OC1Init(TIM2,&TIM_OCInitStructure);//ʹÄÜTIM2µÄͨµÀ1
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//ÖØ×°ÔØ
//TIM2_CH2
TIM_OC2Init(TIM2,&TIM_OCInitStructure);//ʹÄÜTIM3µÄͨµÀ2
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);//ÖØ×°ÔØ
TIM_OC2Init(TIM2,&TIM_OCInitStructure);//ʹÄÜTIM3µÄͨµÀ1
//TIM3_CH1
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//pwmʹÄÜλ
TIM_OC1Init(TIM3,&TIM_OCInitStructure);//ʹÄÜTIM3µÄͨµÀ1
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//ÖØ×°ÔØ
TIM_SelectOutputtrigger(TIM2, TIM_TRGOSource_OC2Ref); //使用TIM2_CH2作为触发源
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1); //当TIM2作为TIM3的触发源的标志位ITR1
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);
TIM_Cmd(TIM2,ENABLE);//´ò¿ªTIM3
2.主函数部分
TIM_SetCompare1(TIM2,45);
TIM_SetCompare2(TIM2,30);
TIM_SetCompare1(TIM3,45);
while(1)
{
TIM_SetCompare2(TIM2,70); //实际上只有这一句生效
Delay_ms(500);
TIM_SetCompare2(TIM2,40); //z这一句不生效
Delay_ms(500);
}
|