新手入门
- 积分
- 18
- 金钱
- 18
- 注册时间
- 2016-8-27
- 在线时间
- 2 小时
|
1金钱
原子哥,是这样的。我利用输出比较模式。然后匹配动作设置为翻转。以此来控制CH3和CH4的频率。原则上为了控制的PWM为精确的50%,都会去设置ARR=65535。但是这样做的话,就始终不能将发出的频率调到0.于是我想到将ARR设置为65530.这样在分频系数为8的时候,PWM的误差很小。然后在需要将发出频率为0的时候在中断中强行将CCR=65535.这样OC将不会发生电平反转(频率为0)。但是由于是使用TIM_OCMode_Toggle模式,却没办法控制它在停止翻转时的电平为低。于是我在网上查找,有网友说重新设置匹配动作为强制低电平输出。于是我按照那个例子在需要将频率发为0的时候在中断中使用了TIM_ForcedOC3Config(TIM4,TIM_ForcedAction_InActive);在不需要频率为0的时候,使用TIM_SelectOCxM(TIM4,TIM_Channel_3,TIM_OCMode_Toggle)将匹配动作设置为翻转模式;这样倒是可以将电平设置为低。但是一旦输出变为低,即使执行频率不为0的语句再也不能输出波形了。可能说太多,我的需求是:TIM4 CH3、CH4独立输出不同频率,各自可调且输出为0时必须是低电平。紧急求助。有懂的大神们,请不吝赐教啊。小弟在此拜托啦!以下是我的部分代码:
TIM_TimeBaseStructure.TIM_Period = 65530; //
TIM_TimeBaseStructure.TIM_Prescaler =8; //
TIM_TimeBaseStructure.TIM_ClockDivision = 0; /
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //
TIM_OCInitStructure.TIM_Pulse = CCR3;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Disable); //
TIM_TimeBaseStructure.TIM_Period = 65530;
TIM_TimeBaseStructure.TIM_Prescaler =8;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //
TIM_OCInitStructure.TIM_Pulse = CCR4;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; /
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Disable); //
// TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //
TIM_ITConfig(TIM4,TIM_IT_CC3|TIM_IT_CC4,ENABLE ); //
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //
NVIC_Init(&NVIC_InitStructure); //
TIM_Cmd(TIM4, ENABLE); //
}
void TIM4_IRQHandler(void) //
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC3)!= RESET) //
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); //
Capture = TIM_GetCapture3(TIM4);//
if(CCR3>=65530)TIM_ForcedOC3Config(TIM4,TIM_ForcedAction_InActive);//变量CCR3的值是在主循环中修改
else
{
TIM_SelectOCxM(TIM4,TIM_Channel_3,TIM_OCMode_Toggle);
TIM_SetCompare3(TIM4,Capture+CCR3);
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC4)!= RESET) ////
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); //
Capture = TIM_GetCapture4(TIM4);
if(CCR4>=65530)TIM_ForcedOC4Config(TIM4,TIM_ForcedAction_InActive);//变量CCR4的值是在主循环中修改
else
{
TIM_SelectOCxM(TIM4,TIM_Channel_4,TIM_OCMode_Toggle);
TIM_SetCompare4(TIM4,Capture+CCR4);
}
}
}
|
最佳答案
查看完整内容[请看2#楼]
再设置回去就是了。还有一个办法是设置IO为普通输出模式,直接输出0即可。然后要PWM的时候,切换回复用功能,继续输出PWM
|