本帖最后由 笑着去斗斗 于 2014-5-5 15:15 编辑
之前用TIM(通用定时器)做了两路PWM,调用TIM_SetCompare1(TIM3,1000); 函数修改TIM3通道1 的占空比没有任何问题。
但现在调用TIM_SetCompare1(TIM1,200); 修改TIM1的占空比时,总是有延迟。就是调用这个函数后,TIM1产生的PWM的占空比并不能立即变化,需要过一点时间才能产生变化。
如:
TIM_SetCompare1(TIM3,1000); TIM_SetCompare2(TIM3,2000);
TIM_SetCompare1(TIM1,200);
Delay(10000);
运行这段程序,正常情况下,TIM3两路PWM 和 TIM1的PWM 的占空比应该同时改变。但实际的现象是TIM1的PWM 的占空比总是晚于TIM3的,这让我很苦恼。
我看了TIM1的控制中有死区时间和刹车功能,没看太懂,不知道跟这个有么有关系。
希望那个大家能给帮我找找问题出在哪里。
我把配置代码贴出来:
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
/* PWM信号电平跳变值 */
u16 CCR1_Val = 1500;
u16 CCR2_Val = 200;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1999; //当定时器从0计数到1999,即为2000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 719; //设置预分频:分频,即为72MHz/720=100KHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4 ; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1999; //当定时器从0计数到1999,即为2000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 719; //设置预分频:分频,即为72MHz/720=100KHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4 ; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //111111111111111111111111
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1500; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //使能通道1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
//死区和刹车功能配置,高级定时器才有的,通用定时器不用配置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;//运行模式下输出选择
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;//空闲模式下输出选择
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //锁定设置
TIM_BDTRInitStructure.TIM_DeadTime = 0X00; //死区时间设置
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; //刹车功能使能
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;//刹车输入极性
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//自动输出使能
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1500; //设置跳变值,当计数器计数到这个值时,电平发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1 11111111111111111111111111111
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //设置通道2的电平跳变值,输出另外一个占空比的PWM
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //使能通道2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重载寄存器ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //使能定时器3
TIM_Cmd(TIM1, ENABLE); //使能定时器1
TIM_CtrlPWMOutputs(TIM1,ENABLE);
|